• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

XPath expression

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 662
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the explanation.
Vasudha
 
We noticed he had no friends. So we gave him this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic