Transforming enumerated list elements

D

dansherpa

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).
 
K

kryptomoon

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

dansherpa

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

dansherpa

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>
 

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
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top