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

XPath expression

 
Vasudha Deepak
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do these two expressions differ?
child: ara[position()=5][attribute::type="warning"]

child: ara[attribute::type="warning"][position()=5]
Thanks,
Vasudha
 
Jayadev Pulaparty
Ranch Hand
Posts: 662
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please look at this following example --
XML document -
<root>
<para type="one"> first paragraph </para>
<para type="two"> second paragraph </para>
<para type="three"> third paragraph </para>
<para type="four"> fourth paragraph </para>
<para type="warning"> fifth paragraph </para>
<para type="six"> sixth paragraph </para>
</root>
===============================================
XSLT stylesheet -

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="root">
<xsl:apply-templates select="child: ara[attribute::type='warning'][position()=5]"/>
</xsl:template>
<xsl:template match="para">
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>
Result - (empty)
<?xml version="1.0" encoding="UTF-8"?>
I guess this is how it works -- First get all the para children with the warning attribute and then get the fifth element in the resulting list (here we have only one para with the type=warning and hence the size of the resulting list is only 1)
===============================================
XSLT stylesheet -

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="root">
<xsl:apply-templates select="child: ara[position()=5][attribute::type='warning']"/>
</xsl:template>
<xsl:template match="para">
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>
Result -
<?xml version="1.0" encoding="UTF-8"?>
fifth paragraph
I guess this is how it works (two levels of filters) -- First get the fifth para child and then check to see if it has warning attribute

Hence it boils down to the order in which the predicates are applied to the node sets.
More interesting to see is -
<xsl:template match="root">
<xsl:apply-templates select="child: ara[attribute::type='warning' and position()=5]"/>
</xsl:template>
will give you
<?xml version="1.0" encoding="UTF-8"?>
fifth paragraph
as the result. Try changing the location of the warning attribute in the xml document to verify things.
 
Vasudha Deepak
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the explanation.
Vasudha
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic