xslt processing on select attribute on apply-templates -
i have xslt below , apply xslt input xml[pasted below], working fine except 1 thing need clarify.
this input xml
<test> <experiment id='1'> <dish1> <conditions pressure='x' temp='y'/> <measurement timestamp='8am' reading='y'/> </dish1> <dish2> <conditions pressure='x' temp='y'/> <measurement timestamp='8am' reading='y'/> </dish2> <dish1> <conditions pressure='x' temp='y'/> <measurement timestamp='2pm' reading='y'/> </dish1> <dish2> <conditions pressure='x' temp='y'/> <measurement timestamp='2pm' reading='y'/> </dish2> </experiment> <experiment id='2'> <dish1> <conditions pressure='x' temp='y'/> <measurement timestamp='9am' reading='y'/> </dish1> </experiment> </test>
this xslt
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="xml" indent="yes"/> <xsl:template match="experiment"> <xsl:copy> <xsl:apply-templates select="@*" /> <xsl:for-each-group select="*" group-by="local-name()"> <xsl:copy> <xsl:apply-templates select="current-group()" /> </xsl:copy> </xsl:for-each-group> </xsl:copy> </xsl:template> <xsl:template match="experiment/*"> <observation> <xsl:apply-templates select="*/@*" /> </observation> </xsl:template> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> </xsl:stylesheet>
this transformation working fine. but, if change below on xslt, then, getting error. idea?
<xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="."/> </xsl:copy> </xsl:template>
i.e., on last match, have changed <xsl:apply-templates select="@*|node()"/>
<xsl:apply-templates select="."/>
.
means current node. when apply-templates .
, you're applying them current context.
in case, template match test
, output copy of it, , apply-templates itself. match again putting endless loop.
by doing apply-templates on node()|@*
, you're applying templates child nodes , attributes.
Comments
Post a Comment