position() problem

I

iware_ext

Hi,
I've the following xml

<A>
<B>
<C>111</C>
<C>222</C>
<C>333</C>
</B>
<B>
<C>444</C>
<C>555</C>
<C>666</C>
</B>
</A>

The number of A is known but not the number of B. The number of B is
equal in each A.
The xml format is fixed and can't be changed.

I'd like to have the following result

1 111-444
2 222-555
3 333-666

For this I'm using the following script

<xsl:template match="/">
<xsl:for-each select="/A/B[1]/C">
<xsl:value-of select="position()"/> <xsl:value-of
select="/A/B[1]/C[position()]"/>-<xsl:value-of
select="/A/B[2]/C[position()]"/>
</xsl:for-each>
</xsl:template>

But the result given is

1 111-444
2 111-444
3 111-444

As if the position isn't working inside A[1]/B[position()]
Any idea?
Thanks for your help

Michel
 
M

Martin Honnen

<A>
<B>
<C>111</C>
<C>222</C>
<C>333</C>
</B>
<B>
<C>444</C>
<C>555</C>
<C>666</C>
</B>
</A>

The number of A is known but not the number of B. The number of B is
equal in each A.
The xml format is fixed and can't be changed.

I'd like to have the following result

1 111-444
2 222-555
3 333-666

I am not sure why you could have several A elements (as A in your
example is the root element and there can be only one root element) but
the following stylesheet is one way to get the output

<?xml version="1.0" encoding="utf-8"?>

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

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

<xsl:template match="A">
<xsl:apply-templates select="B[1]" />
</xsl:template>

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

<xsl:template match="C">
<xsl:variable name="currentPos" select="position()" />
<xsl:value-of select="$currentPos" />
<xsl:text> </xsl:text>
<xsl:value-of select="." />
<xsl:apply-templates
select="../following-sibling::B/C[$currentPos]" mode="lineUp" />
<xsl:text>
</xsl:text>
</xsl:template>

<xsl:template match="C" mode="lineUp">
<xsl:text>-</xsl:text>
<xsl:value-of select="." />
</xsl:template>

</xsl:stylesheet>

even if there are more B elements as long as the C elements match.
 

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

Forum statistics

Threads
474,001
Messages
2,570,254
Members
46,851
Latest member
CliftonCor

Latest Threads

Top