Pairing two elements within the same parent.... in sequence?

Here should go questions about transforming XML with XSLT and FOP.
undrmoons
Posts: 11
Joined: Sat Jan 14, 2006 7:17 pm
Contact:

Pairing two elements within the same parent.... in sequence?

Post by undrmoons » Wed Jan 18, 2006 5:23 pm

Im having trouble trying to pair up 2 different elements in sequence within the same parent element. The number of <Serial_No> and <Price> elements is always consistent within each <onepage> tag (2 <Serial_No> tags means there are 2 <Price> tags, and so on).

However, the total number of those paired elements could be as high as 20 pairs (20 instances of Serial No and Price) within the parent element <onepage>..

My example of the desired result XML should be pretty clear, but the syntax is
really stumping me!

Thanks in advance for any help!
-lmm

------------------------------------------------
XML Input file (Price would vary)
------------------------------------------------

<document>
<onepage>
<Serial_No>614907</Serial_No>
<Serial_No>603360</Serial_No>
<Serial_No>620058</Serial_No>
<Serial_No>603361</Serial_No>
<Price>00.00</Price>
<Price>00.00</Price>
<Price>00.00</Price>
<Price>00.00</Price>
</onepage>
<onepage>
<Serial_No>621600</Serial_No>
<Serial_No>596690</Serial_No>
<Serial_No>619881</Serial_No>
<Serial_No>621622</Serial_No>
<Serial_No>621600</Serial_No>
<Serial_No>596690</Serial_No>
<Serial_No>619881</Serial_No>
<Serial_No>621622</Serial_No>
<Price>00.00</Price>
<Price>00.00</Price>
<Price>00.00</Price>
<Price>00.00</Price>
<Price>00.00</Price>
<Price>00.00</Price>
<Price>00.00</Price>
<Price>00.00</Price>
</onepage>
<onepage>
<Serial_No>614906</Serial_No>
<Serial_No>616369</Serial_No>
<Serial_No>580544</Serial_No>
<Price>00.00</Price>
<Price>00.00</Price>
<Price>00.00</Price>
</onepage>
</document>

------------------------------------------------
Desired result, with paired up elements
------------------------------------------------

<document>
<onepage>
<group>
<Serial_No>614907</Serial_No>
<Price>00.00</Price>
</group>
<group>
<Serial_No>603360</Serial_No>
<Price>00.00</Price>
</group>
<group>
<Serial_No>620058</Serial_No>
<Price>00.00</Price>
</group>
<group>
<Serial_No>603361</Serial_No>
<Price>00.00</Price>
</group>
</onepage>
<onepage>
<group>
<Serial_No>621600</Serial_No>
<Price>00.00</Price>
</group>
<group>
<Serial_No>596690</Serial_No>
<Price>00.00</Price>
</group>
<group>
<Serial_No>619881</Serial_No>
<Price>00.00</Price>
</group>
<group>
<Serial_No>621622</Serial_No>
<Price>00.00</Price>
</group>
<group>
<Serial_No>621600</Serial_No>
<Price>00.00</Price>
</group>
<group>
<Serial_No>596690</Serial_No>
<Price>00.00</Price>
</group>
<group>
<Serial_No>619881</Serial_No>
<Price>00.00</Price>
</group>
<group>
<Serial_No>621622</Serial_No>
<Price>00.00</Price>
</group>
</onepage>
<onepage>
<group>
<Serial_No>614906</Serial_No>
<Price>00.00</Price>
</group>
<group>
<Serial_No>616369</Serial_No>
<Price>00.00</Price>
</group>
<group>
<Serial_No>580544</Serial_No>
<Price>00.00</Price>
</group>
</onepage>
</document>

Radu
Posts: 6453
Joined: Fri Jul 09, 2004 5:18 pm

Post by Radu » Thu Jan 19, 2006 11:54 am

Hi,

I think this stylesheet would work

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output indent="yes"/>
<xsl:template match="document">
<document>
<xsl:for-each select="onepage">
<onepage>
<xsl:variable name="serialsCounter" select="count(Serial_No)"/>
<xsl:for-each select="Serial_No">
<group>
<Serial_No>
<xsl:value-of select="text()"/>
</Serial_No>
<Price>
<xsl:variable name="Price_Position" select="$serialsCounter +
position()"/>
<xsl:variable name="Price_Value" select="../*[$Price_Position]/text()"/>
<xsl:value-of select="$Price_Value"/>
</Price>
</group>
</xsl:for-each>
</onepage>
</xsl:for-each>
</document>
</xsl:template>
</xsl:stylesheet>
As you can see, for each Serial_No I find the position in the children list of the right Price and then list it. If the number of serial numbers always equals the number of prices, the stylesheet will generate correct data.

Regards, Radu

Post Reply