Iterate through every node in a XML

Posted by Rachel on Stack Overflow See other posts from Stack Overflow or by Rachel
Published on 2011-01-04T22:36:46Z Indexed on 2011/01/05 20:53 UTC
Read the original article Hit count: 227

Filed under:
|
|

Hi I am trying to iterate through every node in a xml, be it the element node, text node or comment. With the below XSL in the very first statement prints the complete xml. How do i copy the very first node in $nodes and call the template process-nodes again removing teh first node in my next iteration?

<?xml version='1.0'?>
<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<xsl:call-template name="process-nodes">
<xsl:with-param name="nodes" select="//node()" as="node()*"/>
</xsl:call-template>
</xsl:template>

<xsl:template name="process-nodes">
<xsl:param name="nodes" as="node()*" />
<xsl:copy-of select="$nodes[1]"/>
<xsl:if test="$nodes">
    <xsl:call-template name="process-nodes">
      <xsl:with-param name="nodes"
        select="remove($nodes, 1)" />
    </xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>

Note: I am looking for fixing the issue in this kind of implementation rather than changing the template match to <xsl:template match="@* | node()"> as I need to have some processing which requires this approach.

Thanks.

© Stack Overflow or respective owner

Related posts about Xml

Related posts about xslt