• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

XPath/XSLT: Reverse axis really isn't?

 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am feeding the following XML file and XSL stylesheet
to Xalan.
XML input:

XSL styelsheet:

Here's the result I get:

Since ancestor:: is a reverse axis, why am I getting the ancestor nodes in document order? How can I fix my template so that I get them in reverse order?
[ October 18, 2002: Message edited by: Ron Newman ]
 
Ranch Hand
Posts: 662
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ron,
Please try the following stylesheet. Here the sort criterion is base on the fact that the inner most node has more no. of ancestors and hence the descending order of the ancestor count is used to print the nodes in the reverse order.
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="text()" />
<xsl:template match="text()[.='Roxbury']">
<xsl:apply-templates select="ancestor::*" mode="anc">
<xsl:sort order="descending" select="count(ancestor::*)"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="node()" mode="anc">
<p>I am in <xsl:value-of select="normalize-space(text())"/></p>
</xsl:template>
</xsl:stylesheet>
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Ron's original template is a bit confusing, since it behaves like the ancestor-or-self axis.

By modifying it to -

We get true ancestors.
Cheers,
Dan
 
reply
    Bookmark Topic Watch Topic
  • New Topic