Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > position() problem

Reply
Thread Tools

position() problem

 
 
iware_ext@hotmail.com
Guest
Posts: n/a
 
      01-17-2006
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

 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      01-17-2006


wrote:


> <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"
xmlnssl="http://www.w3.org/1999/XSL/Transform">

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


--

Martin Honnen
http://JavaScript.FAQTs.com/
 
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
Problem problem problem :( Need Help Mike ASP General 2 05-11-2004 08:36 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57