Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

node() test condition

 
Rakesh Gudur
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can any cone give the answer to the question:
<abc att1="xyz"/>
What will the test evaluate to if the following expression is applied on the above element?
<if test="node()"> </if>
A. True
B. False
C. Syntax error
D. None of the above
Thanks,
Rakesh.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anyone explain the question posed by rakesh???
its not clear!!!
 
Jayadev Pulaparty
Ranch Hand
Posts: 662
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
node() is a wild card selector like "*" and "@*". "*" selects only element nodes and "@*" selects only attribute nodes. node() selects all other node types like PIs, namespaces, etc.
I guess the question means something like shown below -
===============================================
<!-- XML FILE -->
<abc att1="xyz"</abc>
===============================================
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="abc">
<xsl:if test="node()">
<xsl:text>TRUE</xsl:text>
</xsl:if>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
=================================================
I guess this is what is meant by testing the node() condition on the element. Here it looks if the element abc has any node() type children. Since there are none here, the if condition will evaluate to false.
The above thing is equivalent to the one shown below with the child:: being the default axis -
<xsl:template match="abc">
<xsl:if test="child::node()">
<xsl:text>TRUE</xsl:text>
</xsl:if>
</xsl:template>
You can try introducing some text like shown here in the xml element and see what the test condition evaluates to.
===============================================
<!-- XML FILE -->
<abc att1="xyz"> hi there </abc>
===============================================
Now you can change this line -
<xsl:if test="node()">
as
<xsl:if test="*">
and look at the result. Since "hi there" is a text node child of abc, the * wild-card will ignore it whereas node() will take it into account;
We have a very good tutorial on XPath available from OReilly. Dan and me gave it to Jim sometime back. Hope that he has the link so that he can provide it to you guys. I missed it somewhere after downloading the tutorial pdf file.
Anyone, please correct me for any mistakes in the above explanation.
Thanks.
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to be precise -
The node() wild card matches all nodes: element nodes, text nodes, attribute nodes, processing instruction nodes, namespace nodes and comment nodes.
The tutorial on XPath from O'Reilly is - http://www.oreilly.com/catalog/xmlnut/chapter/ch09.html
Cheers and have a nice weekend!
Dan
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With this data/code -

Can anyone tell me why 1 is not the output?
After all abc is an element - just an empty one.
Cheers,
Dan
 
Jayadev Pulaparty
Ranch Hand
Posts: 662
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dan,
The xsl sylesheet given here -
================================================
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="abc">
<xsl:if test="node()">
1
</xsl:if>
</xsl:template>
</xsl:stylesheet>
================================================
looks for any child::nodes for the element "abc". Since there are none (we get an empty nodeset returned to the test() condition and hence false) we donot see any output. The match attribute for the element abc is definitely not its child node.
If you change the stuff as shown below, you will see 1 in the output.
===============================================
<abc att1="xyz"><someElement/>hi there</abc>
This makes the element abc to have two child nodes . Please donot use any "enters" while keying in the stuff in a notepad to test as that will get in some more text nodes at the same depth of someElement and "hi there nodes". You can also verify this by running this little stylesheet -
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="abc">
<xsl:value-of select="count(node())"/>
</xsl:template>
</xsl:stylesheet>
ALSO
================================================
If you run the following stuff, (node() replaced by *) you see the count to be 1.
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="abc">
<xsl:value-of select="count(*)"/>
</xsl:template>
</xsl:stylesheet>
================================================
ALSO
Please try the following on the xml file and try to interpret the result -
<abc att1="xyz">
<someElement>
hi there
<inner/>
</someElement>
</abc>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="abc">
<xsl:value-of select="count(descendant::node())"/>
</xsl:template>
</xsl:stylesheet>
================================================
Hope this helps. Please get back with any mistakes in the explanation.
Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic