我正在尝试处理一个xml文件,它有几个不同的状态组,如 rootchildgroup16/childgroupsetstateinit/setstatechild1.../child1child2.../child2setstateprocesssetstatechild2.../child2child3.../child3.....childgroup17/childgroup... 我
          <root> <childgroup>16</childgroup> <setstate>init</setstate> <child1>...</child1> <child2>...</child2> <setstate>process<setstate> <child2>...</child2> <child3>...</child3> ..... <childgroup>17</childgroup> ...
我需要的是实际得到的东西
<childgroup no="16">
  <state statename="init">
    <child1>...</child1>
    <child2>...</child2>
  </state>
  <state statename="process">
    <child2>...</child2>
    <child3>...</child3>
  </state>
</childgroup>
<childgroup no="17">
... 
 我已经完成了简单的部分,并将“chgrpno”属性和stateid属性添加到所有子节点(它使所有元素的副本除了子组和状态,将属性添加到这两个.
<xsl:template match="/"> <xsl:apply-templates mode="numb"/> </xsl:template>
这是有效的,结果所有的孩子都有属性,所以我可以在下一次传递中重新组合它们,状态有数字,所以我以后可以做同样的事情.但是当我尝试做时,试图用“临时文件”来跟随M.Kay的例子
<xsl:variable name="nmb"> <xsl:apply-templates mode="numb"/> </xsl:variable> <xsl:template match="/"> <xsl:copy-of select="$nmb"/> </xsl:template>
然后它只是将原件归还给我,而我在第一遍中所做的所有更改都消失了.那么我在这里做错了什么?
我使用的是XSLT 1.0,而不是XSLT 2.0.
(编辑:我当然命名变量,忘了把它复制到这里).
下面是一个如何在一个步骤中使用XSLT 1.0进行分组的示例;样式表<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output indent="yes"/>
  <xsl:key name="k1" match="root/*[not(self::childgroup)]" 
    use="generate-id(preceding-sibling::childgroup[1])"/>
  <xsl:key name="k2" match="root/*[not(self::childgroup) and not(self::setstate)]"
    use="concat(generate-id(preceding-sibling::childgroup[1]), '|', generate-id(preceding-sibling::setstate[1]))"/>
  <xsl:template match="root">
    <xsl:copy>
      <xsl:apply-templates select="childgroup"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="childgroup">
    <childgroup no="{.}">
      <xsl:apply-templates select="key('k1', generate-id())[self::setstate]"/>
    </childgroup>
  </xsl:template>
  <xsl:template match="setstate">
    <state statename="{.}">
      <xsl:copy-of select="key('k2', concat(generate-id(preceding-sibling::childgroup[1]), '|', generate-id()))"/>
    </state>
  </xsl:template>
</xsl:stylesheet> 
 转换输入样本
<root> <childgroup>16</childgroup> <setstate>init</setstate> <child1>...</child1> <child2>...</child2> <setstate>process</setstate> <child2>...</child2> <child3>...</child3> <childgroup>17</childgroup> <setstate>init</setstate> <child1>...</child1> <child2>...</child2> <setstate>process</setstate> <child2>...</child2> <child3>...</child3> </root>
成
<root>
   <childgroup no="16">
      <state statename="init">
         <child1>...</child1>
         <child2>...</child2>
      </state>
      <state statename="process">
         <child2>...</child2>
         <child3>...</child3>
      </state>
   </childgroup>
   <childgroup no="17">
      <state statename="init">
         <child1>...</child1>
         <child2>...</child2>
      </state>
      <state statename="process">
         <child2>...</child2>
         <child3>...</child3>
      </state>
   </childgroup>
</root>
        
             