Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > xslt / xpath question

Reply
Thread Tools

xslt / xpath question

 
 
Brian Schroeder
Guest
Posts: n/a
 
      08-04-2004
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" xmlnssl="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 Schröder
http://www.brian-schroeder.de/
 
Reply With Quote
 
 
 
 
=?ISO-8859-15?Q?R=E9mi_Peyronnet?=
Guest
Posts: n/a
 
      08-04-2004
> <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

--
Rémi Peyronnet
 
Reply With Quote
 
 
 
 
Brian Schroeder
Guest
Posts: n/a
 
      08-04-2004
On Wed, 04 Aug 2004 20:39:04 +0200, Rémi Peyronnet wrote:

>> <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

--
Brian Schröder
http://www.brian-schroeder.de/
 
Reply With Quote
 
Ben Edgington
Guest
Posts: n/a
 
      08-05-2004
Brian Schroeder <(E-Mail Removed)> writes:
> 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" xmlnssl="http://www.w3.org/1999/XSL/Transform">

<xslutput method="text"/>

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

<xsl:template match="n">

<xslaram name="prefix"/>

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

<!-- output the new prefix -->
<xsl:value-of disable-output-escaping="yes" select="$current"/>
<xsl:text>&#x0a;</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>

--
Ben Edgington
Mail to the address above is discarded.
Mail to ben at that address might be read.
http://www.edginet.org/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
xslt question: using variable in xpath not allowed? =?UTF-8?b?5byg6Z+h5q2m?= XML 2 10-22-2006 11:39 AM
Removing child nodes via XSLT or Xpath - newbie question jhoge123@yahoo.com XML 3 10-04-2005 03:03 PM
XSLT performance question regarding XPATH V2 key() feature David Blickstein XML 1 09-14-2005 08:17 PM
"Memory leak" in javax.xml.xpath.XPath Marvin_123456 Java 4 07-29-2005 03:49 PM
Problem selecting a node with XPATH if attribute value contains backslashes - how to force XPATH string to be treated as literal? Alastair Cameron XML 1 07-08-2003 07:24 PM



Advertisments