xslt / xpath question

Brian Schroeder

Usenet Archive
Joined
May 8, 2004
Messages
44
Reaction score
0
Hello Group,

I'm quite new to xml, xpath and xslt so I hope I've not overlooked a resource where the answer to my question is.

I'll try to give a short example, but in XML everything gets bloated really fast.

I've got the following situation:

=== a.xml ===
<?xml version="1.0" encoding="ISO-8859-1"?>
<n categorie="A">
<n categorie="B">
<n categorie="C"/>
</n>
<n categorie="D"/>
</n>
===

I want to transform this via xslt to the following

A
A->B
A->B->C
A->D

I came up with the following stylesheet

=== test.xsl ===
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="n"> <xsl:value-of select="ancestor::*[@categorie]/attribute::categorie"/>
-&gt; <xsl:value-of select="@categorie"/><xsl:text>
</xsl:text>
<xsl:apply-templates select="n"/>
</xsl:template>
</xsl:stylesheet>
===

I guess, that "ancestor::*[@categorie]/attribute::categorie" should select all ancestor, having an attribute categorie, and from those nodes the attribute. My problem is, that only the first attribute "A" will result from this question. It seems, that I have to concatenate the values, but concat did not help.

I would really appreciate help and even more pointers to some online resource where I can learn about this kind of things. (So far, the best I found were the w3 recommendations. Most of the other stuff seemed quite useless.)

Thanks a lot,

Brian
 

Brian Schroeder

Usenet Archive
Joined
May 8, 2004
Messages
44
Reaction score
0
<xsl:value-of
select="ancestor::*[@categorie]/attribute::categorie"/> -&gt; <xsl:value-of select="@categorie"/><xsl:text>

You should try this instead :

<xsl:for-each select="ancestor::*[@categorie]">
<xsl:value-of select="@categorie" /><xsl:text>-&gt;</xsl:text> </xsl:for-each>

Hth

Indeed, you helped a lot. Seems like I have been searching in the wrong direction. Quite interesting that xslt needs a looping construct for joining a list of results. I would have thought something more data-centric more in the spirit of a data transformation language.

Thank you a lot,

Brian
 

Ben Edgington

Usenet Archive
Joined
Mar 18, 2004
Messages
45
Reaction score
0
Brian Schroeder said:
I've got the following situation:

=== a.xml ===
<?xml version="1.0" encoding="ISO-8859-1"?>
<n categorie="A">
<n categorie="B">
<n categorie="C"/>
</n>
<n categorie="D"/>
</n>
===

I want to transform this via xslt to the following

A
A->B
A->B->C
A->D

Further to your other replies, rather than use the ancestor axis this kind of thing is generally done more naturally in XSLT with recursion.

This stylesheet (I know it doesn't look more elegant at first sight, but recursion is much nicer, honestly) produces the output you desire:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:eek:utput method="text"/>

<xsl:template match="/">
<xsl:apply-templates select="n"/>
</xsl:template>

<xsl:template match="n">

<xsl:param name="prefix"/>

<!-- update the prefix -->
<xsl:variable name="current">
<xsl:choose>
<xsl:when test="$prefix">
<xsl:value-of select="concat($prefix,'->',@categorie)"/> </xsl:when> <xsl:eek:therwise> <xsl:value-of select="@categorie"/> </xsl:eek:therwise> </xsl:choose> </xsl:variable>

<!-- output the new prefix -->
<xsl:value-of disable-output-escaping="yes" select="$current"/> <xsl:text> </xsl:text> <!-- a newline -->

<!-- recursively treat my child-nodes -->
<xsl:apply-templates select="n">
<xsl:with-param name="prefix">
<xsl:value-of select="$current"/>
</xsl:with-param>
</xsl:apply-templates>

</xsl:template>

</xsl:stylesheet>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
548,208
Messages
3,104,799
Members
249,594
Latest member
henrywalsh

Latest Threads

Top