Reputation: 5
I have an XML like this:
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="sandbox.xsl"?>
<root>
<base PV="a">
<element1 PV="b">
<Item PV="c" />
</element1>
<element2 PV="d">
<element3 PV="e">
<Item PV="f" />
</element3>
</element2>
</base>
</root>
Now i am trying to concatenate a PV name of an Item with all PV names of the parent elements and this recursiv.
For example for the Element Item with the PV name c I wanna get:
a.b.c
, and for the Item f I wanna get a.d.e.f
I have tried with
<table border="1">
<xsl:for-each select="//Item">
<tr>
<td>
<xsl:value-of select="../../../@PV"/>.
<xsl:value-of select="../../@PV"/>.
<xsl:value-of select="../@PV"/>.
<xsl:value-of select="@PV"/>
</td>
</tr>
</xsl:for-each>
</table>
With te result
. a. b. c
a. d. e. f
The problem is, that i do not know how many "parent" elements I have in my XML.
Then i have tried with
<xsl:variable name="curr" select="//Item[@PV = 'f']"></xsl:variable>
<xsl:variable name="test" select="$curr/ancestor-or-self::*[@PV]"></xsl:variable>
<xsl:for-each select="$test">
<xsl:value-of select="@PV"></xsl:value-of>.
</xsl:for-each>
Here i get the correct answer but only for the Item f and with some spaces inside, which i do not want a. d. e. f.
How can i create a list for al Items?
Upvotes: 0
Views: 42
Reputation: 117165
In XSLT 2.0 you can do simply:
<xsl:template match="/">
<table border="1">
<xsl:for-each select="//Item">
<tr>
<td>
<xsl:value-of select="ancestor-or-self::*/@PV" separator="."/>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
In XSLT 1.0 this needs to be expressed as:
<xsl:template match="/">
<table border="1">
<xsl:for-each select="//Item">
<tr>
<td>
<xsl:for-each select="ancestor-or-self::*/@PV">
<xsl:value-of select="."/>
<xsl:if test="position()!=last()">.</xsl:if>
</xsl:for-each>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
Upvotes: 1