[ACCEPTED]-Difference between: child::node() and child::*-xslt

Accepted answer
Score: 41

child::node() matches any node that's not an attribute 23 node, namespace node, or document node. That 22 means that it does match processing instructions, comments, and 21 text nodes.

child::* matches only elements.

See section 5.5.3 of the 20 spec:

The pattern node() matches all nodes selected 19 by the expression root(.)//(child-or-top::node()), that is, all 18 element, text, comment, and processing 17 instruction nodes, whether or not they 16 have a parent. It does not match attribute 15 or namespace nodes because the expression 14 does not select nodes using the attribute 13 or namespace axes. It does not match document 12 nodes because for backwards compatibility reasons 11 the child-or-top axis does not match a 10 document node.

Update: Michael's answer inspired 9 the following stylesheet. Use it to test 8 the types of nodes as they're processed:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/|node()">
        <xsl:call-template name="type" />
        <xsl:text>  [  </xsl:text>
        <xsl:value-of select="." />
        <xsl:apply-templates select="node()" />
        <xsl:text>  ]  </xsl:text>
    <xsl:template name="type">
            <xsl:when test="count(.|/)=1">
            <xsl:when test="self::*">
                <xsl:text>Element </xsl:text>
                <xsl:value-of select="name()" />
            <xsl:when test="self::text()">
            <xsl:when test="self::comment()">
            <xsl:when test="self::processing-instruction()">
            <xsl:when test="count(.|../@*)=count(../@*)">

Modify 7 what's matched/selected to test other patterns. For 6 example, the following input:

<A attr="test" other="val">
  <C>some value</C>
  <!-- a comment -->

Produces the 5 following output:

Root  [  

  some value

Element A  [  

  some value

Text  [  

  ]  Element B  [  
  ]  Text  [  

  ]  Element C  [  some value
Text  [  some value
  ]    ]  Text  [  

  ]  Comment  [   a comment 
  ]  Text  [  

  ]  Element D  [  
  ]  Text  [  

  ]    ]    ]  

Special thanks to this page for 4 getting me started on the node-type tests. (It's 3 especially fitting that one of Michael's 2 answers from over six years ago appears 1 there, too.)

Score: 20

To expand on lwburk's answer, if your XML 4 looks like this:


Then the A element has 7 3 child nodes; three of them are elements, four 2 are text nodes. The expression child::node() matches 1 all 7, whereas child::* only matches the elements.

More Related questions