Transforming enumerated list elements

Discussion in 'XML' started by dansherpa@gmail.com, Oct 24, 2005.

  1. Guest

    I need XSLT to transform:

    <?xml version="1.0" encoding="UTF-8"?>
    <LineItems>
    <Quantity>
    <Quantity0000>1</Quantity0000>
    <Quantity0001>2</Quantity0001>
    </Quantity>
    <Description>
    <Description0000>Description1</Description0000>
    <Description0001>Description2</Description0001>
    </Description>
    </LineItems>

    to:

    <?xml version="1.0" encoding="UTF-8"?>
    <LineItems>
    <LineItem>
    <Quantity>1</Quantity>
    <Description>Description1</Description>
    </LineItem>
    <LineItem>
    <Quantity>2</Quantity>
    <Description>Description2</Description>
    </LineItem>
    </LineItems>

    Note that while my example only has 2 line items there may be any
    number of them.

    I realize this source XML is horribly designed but that is out of my
    control (and the reason I want to transform it in the first place).
     
    , Oct 24, 2005
    #1
    1. Advertising

  2. kryptomoon Guest

    Suggestion: Set a variable to hold the new node name, then select new
    node(s) based on this value.
    e.g.: <xsl:varibale name="nQ" select="substring-after(name(),
    'Quantity')" />
    <xsl:value-of select="//*[name()='$nQ']"/>
     
    kryptomoon, Oct 25, 2005
    #2
    1. Advertising

  3. Guest

    Thanks Kryptomoon. I've tried this and still get it to work. This is
    my current XSL:

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:eek:utput method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="LineItems">
    <LineItems>
    <xsl:call-template name="loopMe">
    <xsl:with-param name="counter" select="0"/>
    <xsl:with-param name="max" select="count(Quantity/*)"/>
    </xsl:call-template>
    </LineItems>
    </xsl:template>
    <xsl:template name="loopMe">
    <xsl:param name="counter"/>
    <xsl:param name="max"/>
    <xsl:if test="$counter &lt; $max">
    <xsl:variable name="quantity" select="concat('Quantity',
    format-number($counter, '0000'))"/>
    <xsl:variable name="description" select="concat('Description',
    format-number($counter, '0000'))"/>
    <LineItem>
    <Quantity>
    <xsl:value-of select="//*[name()='$quantity']"/>
    </Quantity>
    <Description>
    <xsl:value-of select="//*[name()='$description']"/>
    </Description>
    </LineItem>
    <xsl:call-template name="loopMe">
    <xsl:with-param name="counter" select="$counter + 1"/>
    <xsl:with-param name="max" select="$max"/>
    </xsl:call-template>
    </xsl:if>
    </xsl:template>
    </xsl:stylesheet>


    The output is formatted correctly but does not contain any of the
    values:

    <?xml version="1.0" encoding="UTF-8"?>
    <LineItems>
    <LineItem>
    <Quantity></Quantity>
    <Description></Description>
    </LineItem>
    <LineItem>
    <Quantity></Quantity>
    <Description></Description>
    </LineItem>
    </LineItems>

    Anyone have any thoughts? Thanks.
     
    , Nov 2, 2005
    #3
  4. Guest

    Nevermind. it was the single-quotes around the variable references. If
    I remove those it works!

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:eek:utput method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="LineItems">
    <LineItems>
    <xsl:call-template name="loopMe">
    <xsl:with-param name="counter" select="0"/>
    <xsl:with-param name="max" select="count(Quantity/*)"/>
    </xsl:call-template>
    </LineItems>
    </xsl:template>
    <xsl:template name="loopMe">
    <xsl:param name="counter"/>
    <xsl:param name="max"/>
    <xsl:if test="$counter &lt; $max">
    <xsl:variable name="quantity" select="concat('Quantity',
    format-number($counter, '0000'))"/>
    <xsl:variable name="description" select="concat('Description',
    format-number($counter, '0000'))"/>
    <LineItem>
    <Quantity>
    <xsl:value-of select="//*[name()=$quantity]"/>
    </Quantity>
    <Description>
    <xsl:value-of select="//*[name()=$description]"/>
    </Description>
    </LineItem>
    <xsl:call-template name="loopMe">
    <xsl:with-param name="counter" select="$counter + 1"/>
    <xsl:with-param name="max" select="$max"/>
    </xsl:call-template>
    </xsl:if>
    </xsl:template>
    </xsl:stylesheet>
     
    , Nov 2, 2005
    #4
    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. Matthew Rees-George
    Replies:
    6
    Views:
    397
    Steven Dilley
    Aug 1, 2003
  2. Jody Greening
    Replies:
    5
    Views:
    683
    Jody Greening
    Jan 6, 2005
  3. Jody Greening
    Replies:
    0
    Views:
    340
    Jody Greening
    Jan 6, 2005
  4. Adam Hartshorne
    Replies:
    2
    Views:
    375
    Nitin Motgi
    Jan 27, 2006
  5. David Mathog

    Find the number of elements in an enumerated type?

    David Mathog, Sep 28, 2007, in forum: C Programming
    Replies:
    11
    Views:
    670
    Roberto Waltman
    Sep 30, 2007
Loading...

Share This Page