This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: sorting nodes in reverse document order
- To: Annmarie dot Rubin at East dot Sun dot COM, Jeni dot Tennison at epistemics dot co dot uk
- Subject: Re: sorting nodes in reverse document order
- From: Ann Marie Rubin - Sun PC Networking Engineering <Annmarie dot Rubin at East dot Sun dot COM>
- Date: Tue, 16 May 2000 12:24:31 -0400 (EDT)
- Cc: xsl-list at mulberrytech dot com
- Reply-To: xsl-list at mulberrytech dot com
Jeni,
Thanks for your suggestions. The <xsl:template match="CLASS" mode="hierarchy">
rule that you suggested seems to select the parent (@SUPERCLASS) of each class
element in the XML source tree.
What I want to do is match each CLASS, output some CLASS data, then print the
ancestors for the matched class (not all classes in the hierarchy). The
following code does output the ancestors for each class, but in document order.
I need to output the ancestors in reverse document order.
<xsl:sort> does not work because I'm calling the hierarchy template recursively
to generate the list of ancestors. Each time the hierarchy template is
processed, it returns a single node, because each class has only one parent.
Therefore, there is no node list to sort.
I was trying to think of a way to save each node in a variable and then output
the variables in reverse order, but haven't figured out how to do it or if it
would work at all.
This works to output ancestors in document order:
<xsl:template match="CLASS">
...
outputs some class data
...
<h2>Class Hierarchy</h2>
<!-- Call hierarchy template to output ancestors of this class -->
<xsl:call-template name="hierarchy"/>
<!-- named template to do the hierarchy tracing -->
<xsl:template name="hierarchy">
<br data="{@NAME} -- {@SUPERCLASS}"><a href="{@NAME}.html"><xsl:value-of
select="@NAME"/></a></br>
<xsl:if test="@SUPERCLASS">
<xsl:param name="parentname" select="@SUPERCLASS"/>
<xsl:for-each select="//CLASS[@NAME=$parentname]">
<xsl:call-template name="hierarchy"/>
</xsl:for-each>
</xsl:if>
</xsl:template>
Ann Marie
X-Sender: JTennison@NTServer
To: Annmarie.Rubin@east.sun.com
From: Jeni Tennison <Jeni.Tennison@epistemics.co.uk>
Subject: Re: sorting nodes in reverse document order
Cc: xsl-list@mulberrytech.com
Mime-Version: 1.0
X-MDaemon-Deliver-To: Annmarie.Rubin@East.Sun.COM
Content-Transfer-Encoding: 8bit
X-MIME-Autoconverted: from quoted-printable to 8bit by
purol.East.Sun.COM id MAA16508
Ann,
>I have a for-each statement inside a template that outputs a list of
the
>ancestors of the current context node. I couldn't use the parent or
>ancestor axes to get this list because these nodes are not contained
>within their parent nodes in the XML source document. Each node has a
>SUPERCLASS attribute whose value is the name of its parent.
[snip]
>How can I sort these nodes and output them in reverse order?
You can use xsl:sort within xsl:for-each for that, but I don't think
that's
the easiest way of going about your problem. As Mike Kay says "Don't
Iterate, Recurse" (p.551).
Assuming an input of a number of CLASS elements, this works:
<!-- define a key into the node NAMEs for efficiency -->
<xsl:key name="classes" match="CLASS" use="@NAME"/>
<xsl:template match="//CLASS[...]"><!-- insert predicate of choice -->
<xsl:apply-templates select="." mode="hierarchy" />
</xsl:template>
<xsl:template match="CLASS" mode="hierarchy">
<!-- do this template on my superclass -->
<xsl:apply-templates select="key('classes', @SUPERCLASS)"
mode="hierarchy" />
<!-- then print my details -->
<br data="{@NAME} -- {@SUPERCLASS}">
<a href="{@NAME}.html">
<xsl:value-of select="@NAME"/>
</a>
</br>
</xsl:template>
Hope this helps,
Jeni
Dr Jeni Tennison
Epistemics Ltd, Strelley Hall, Nottingham, NG8 6PE
Telephone 0115 9061301 • Fax 0115 9061304 • Email
jeni.tennison@epistemics.co.uk
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list