position() problem

Discussion in 'XML' started by iware_ext@hotmail.com, Jan 17, 2006.

  1. Guest

    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
     
    , Jan 17, 2006
    #1
    1. Advertising

  2. 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"
    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.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Jan 17, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Luqman
    Replies:
    1
    Views:
    677
    Luqman
    Feb 7, 2006
  2. James Wong
    Replies:
    4
    Views:
    533
    James Wong
    Jul 14, 2004
  3. Replies:
    3
    Views:
    198
  4. James Black
    Replies:
    0
    Views:
    409
    James Black
    May 28, 2006
  5. brendan
    Replies:
    0
    Views:
    213
    brendan
    Aug 29, 2006
Loading...

Share This Page