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
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']"/>
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 < $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.
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 < $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>
|
|
|
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.
Top Community Contributors
|