sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
dansherpa@gmail.com's Avatar

Transforming enumerated list elements


Question posted by: dansherpa@gmail.com (Guest) on October 24th, 2005 04:15 PM
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).

3 Answers Posted
kryptomoon's Avatar
Guest - n/a Posts
#2: Re: Transforming enumerated list elements

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']"/>

dansherpa@gmail.com's Avatar
dansherpa@gmail.com November 2nd, 2005 07:55 PM
Guest - n/a Posts
#3: Re: Transforming enumerated list elements

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:output 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.

dansherpa@gmail.com's Avatar
dansherpa@gmail.com November 2nd, 2005 07:55 PM
Guest - n/a Posts
#4: Re: Transforming enumerated list elements

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:output 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>

 
Not the answer you were looking for? Post your question . . .
196,811 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 196,811 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top Community Contributors