Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Transforming enumerated list elements

Reply
Thread Tools

Transforming enumerated list elements

 
 
dansherpa@gmail.com
Guest
Posts: n/a
 
      10-24-2005
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).

 
Reply With Quote
 
 
 
 
kryptomoon
Guest
Posts: n/a
 
      10-25-2005
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']"/>

 
Reply With Quote
 
 
 
 
dansherpa@gmail.com
Guest
Posts: n/a
 
      11-02-2005
Thanks Kryptomoon. I've tried this and still get it to work. This is
my current XSL:

<xsl:stylesheet version="1.0"
xmlnssl="http://www.w3.org/1999/XSL/Transform">
<xslutput 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">
<xslaram name="counter"/>
<xslaram 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.

 
Reply With Quote
 
dansherpa@gmail.com
Guest
Posts: n/a
 
      11-02-2005
Nevermind. it was the single-quotes around the variable references. If
I remove those it works!

<xsl:stylesheet version="1.0"
xmlnssl="http://www.w3.org/1999/XSL/Transform">
<xslutput 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">
<xslaram name="counter"/>
<xslaram 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>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Find the number of elements in an enumerated type? David Mathog C Programming 11 09-30-2007 09:27 PM
Removing elements from a list that are elements in another list Adam Hartshorne C++ 2 01-27-2006 07:47 AM
Transforming with XSLT, Grouping elements until difference found. Jody Greening XML 5 01-06-2005 06:52 PM
Transforming with XSLT, Grouping elements until difference found. Jody Greening XML 0 01-06-2005 04:07 PM
Preserving text position but transforming sub elements. Matthew Rees-George XML 6 08-01-2003 04:26 PM



Advertisments