xml - xslt writing to uncles of a context in a for-each -
i have following input:
<element1> <elelment2/> </element1>
and i'm grabbing node set bunch of files , inserting many elements find in node set using:
<xsl:variable name="root" select="/" /> <xsl:variable name="id" saxon:assignable="yes" select="0"/> <xsl:variable name="views" select="collection('file:/c:/temp/?select=*.xml;recurse=yes')"/> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="/element1/element2"> <xsl:copy> <xsl:apply-templates select="@* | *"/> <xsl:for-each select="$views/element3[somefilter]"> <element3 name="{@name}" xmi.id="{$id}"/> <saxon:assign name="id"><xsl:value-of select="$id+1"/></saxon:assign> </xsl:for-each> </xsl:copy> </xsl:template>
this works great getting following:
<element1> <elelment2> <element3 name="view1" id="0"/> <element3 name="view2" id="1"/> <element3 name="view3" id="2"/> </element2> </element1>
but i'd use same node set information create parent elements final output is:
<element1> <elelment2> <element3 name="view1" id="0"/> <element3 name="view2" id="1"/> <element3 name="view3" id="2"/> </element2> <element2 name="view1" id="0"/> <element2 name="view2" id="1"/> <element2 name="view3" id="2"/> </element1>
thus, generated id's , names have line , in different levels of tree not ancestors or children of each other.
element2 , element3 quite different i'm possibly attempting generate 2 node sets same data @ same time because of id value , apply node sets in different locations.
any insight appreciated!
i don't quite see creating "parent elements". why don't try like:
xslt 2.0
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:variable name="views" select="collection('file:/c:/temp/?select=*.xml;recurse=yes')"/> <!-- identity transform --> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="element1"> <xsl:copy> <xsl:apply-templates select="@* | *"/> <xsl:for-each select="$views/element3"> <element2 name="{@name}" id="{position()}"/> </xsl:for-each> </xsl:copy> </xsl:template> <xsl:template match="element2"> <xsl:copy> <xsl:apply-templates select="@* | *"/> <xsl:for-each select="$views/element3"> <element3 name="{@name}" xmi.id="{position()}"/> </xsl:for-each> </xsl:copy> </xsl:template> </xsl:stylesheet>
Comments
Post a Comment