xml - XSLT and temporary documents -


i trying process xml file has several different state groups like

<root> <childgroup>16</childgroup> <setstate>init</setstate> <child1>...</child1> <child2>...</child2> <setstate>process<setstate> <child2>...</child2> <child3>...</child3> ..... <childgroup>17</childgroup> ... 

what need

<childgroup no="16">   <state statename="init">     <child1>...</child1>     <child2>...</child2>   </state>   <state statename="process">     <child2>...</child2>     <child3>...</child3>   </state> </childgroup> <childgroup no="17"> ... 

i've done simple part going , adding "chgrpno" attribute , stateid attribute childs (it makes copy-of of elements childgroup , state, adding attribute two.

<xsl:template match="/">   <xsl:apply-templates mode="numb"/> </xsl:template> 

this works , in result childs have attribute regroup them in next pass , states have numbers later make same thing. trying follow example of m.kay "temporary documents" when try do

<xsl:variable name="nmb">   <xsl:apply-templates mode="numb"/> </xsl:variable>  <xsl:template match="/">   <xsl:copy-of select="$nmb"/> </xsl:template> 

then returns original me, , changes made in first pass gone. doing wrong here?

i use xslt 1.0, not xslt 2.0 explicitly.

(edit: surely named variable, forgot copy here).

here example how approach grouping xslt 1.0 in 1 step; stylesheet

<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) , 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> 

transforms input sample

<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> 

into

<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> 

Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

java - Output of Eclipse is rubbish -

jquery - Confused with JSON data and normal data in Django ajax request -