473,394 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

xslt / xpath question

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 Schröder
http://www.brian-schroeder.de/
Jul 20 '05 #1
3 1436
> <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
Jul 20 '05 #2
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/
Jul 20 '05 #3
Brian Schroeder <sp******@bssoftware.de> 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" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output 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:otherwise>
<xsl:value-of select="@categorie"/>
</xsl:otherwise>
</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/
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ramon M. Felciano | last post by:
Helo all -- I'm trying to gain a deeper understand for what type of semi-declarative programming can be done through XML and XPath/XSLT. I'm looking at graph processing problems as a testbed for...
3
by: Justine Hlista | last post by:
I'm using xalan-j_2_6_0 and trying to get an example from Michael Kay's book to work: <xsl:template match="/"> <xsl:variable name="rainbow"> <color>red</color> <color>blue</color>...
4
by: Son KwonNam | last post by:
In XSLT, is this possible to get value from xml using XPath which is in XSLT variable? I mean XPath strings can be dynamic while XSL Transforming. If possible, How?? Because I'm not a...
4
by: Chris Kettenbach | last post by:
Hi Peter, I get error when processing the stylesheet. It errors here. <xsl:for-each select="registration)=1]"> specifically: Expression does not return a DOM node. registration)=1]<--
3
by: Kathy Burke | last post by:
Hi again, I'm using the following xpath (works in visualizer) with a SelectSingleNode("xpath") statement. //Station/(WI])]/@order Problem is I get an error "expression passed to this method...
3
by: Ian Roddis | last post by:
Hello, I want to embed SQL type queries within an XML data record. The XML looks something like this: <DISPLAYPAGE> <FIELD NAME="SERVER" TYPE="DROPDOWN"> <OPTION>1<OPTION> <OPTION>2<OPTION>...
1
by: Sergey Dubinets | last post by:
In effort to prioritize our goals we composed the list of random features each of them may add value to set of XSLT tools offered from Microsoft. 1. XSLTc (Compiler for XSLT...
2
by: 张韡武 | last post by:
We have preffered language set as variable in xslt: <xsl:variable name="preferred_language"> zh </xsl:variable> Data: <name xml:lang="de">Raw Materials (Mining incl.)</name> <name...
2
jkmyoung
by: jkmyoung | last post by:
Here's a short list of useful xslt general tricks that aren't taught at w3schools. Attribute Value Template Official W3C explanation and example This is when you want to put dynamic values...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.