This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: How to compare values between two different looping tags


At 2002-07-25 22:21 +0000, murli bk wrote:
I have an xml file like below.
...
How do you write an xsl to get a text output like below?

C        D         E
=======================
111      222       000
232     3232       777
You've got some requirements that stretch the ordered tree processing characteristic of most XML transformations. You are combining uniqueness and the requirement for the *parallel* sorted processing of the unique values.

The end result I have is not pretty, but it works ... I'm hoping others will find a more elegant solution that what I have below. I'm rushing off to dinner and this was the first approach to come to mind.

I didn't take the time to document it yet, essentially I create variables of the unique values of each element type and precalculate the size of each before walking through the longest of the sets of unique values and then selectively printing the next index value from the sorted set of unique nodes by going through the sorted list entirely each time.

I hope this helps.

........................ Ken

T:\ftemp>type murali.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<G>
<X>
<A>
<C>111</C>
<D>222</D>
</A>
<A>
<C>232</C>
<D>3232</D>
</A>
</X>

<Y>
<B>
<C>232</C>
<E>777</E>
</B>
<B>
<C>111</C>
<E>000</E>
</B>
<B>
<C>232</C>
<E>777</E>
</B>
</Y>
</G>
T:\ftemp>type murali.xsl
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">

<xsl:key name="allC" match="C" use="."/>
<xsl:key name="allD" match="D" use="."/>
<xsl:key name="allE" match="E" use="."/>

<xsl:variable name="uniqC" select="//C[generate-id(.)=generate-id(key('allC',.))]"/>
<xsl:variable name="uniqD" select="//D[generate-id(.)=generate-id(key('allD',.))]"/>
<xsl:variable name="uniqE" select="//E[generate-id(.)=generate-id(key('allE',.))]"/>

<xsl:variable name="countC" select="count($uniqC)"/>
<xsl:variable name="countD" select="count($uniqD)"/>
<xsl:variable name="countE" select="count($uniqE)"/>

<xsl:output method="text"/>

<xsl:template match="/">
<xsl:choose>
<xsl:when test="$countC >= $countD and $countC >= $countE">
<xsl:call-template name="dorows">
<xsl:with-param name="longest" select="$uniqC"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="$countD >= $countC and $countD >= $countE">
<xsl:call-template name="dorows">
<xsl:with-param name="longest" select="$uniqD"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="dorows">
<xsl:with-param name="longest" select="$uniqE"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template name="dorows">
<xsl:param name="longest"/>
C D E
======================
<xsl:for-each select="$longest">
<xsl:variable name="index" select="position()"/>
<xsl:for-each select="$uniqC">
<xsl:sort select="." data-type="number"/>
<xsl:if test="$index = position()">
<xsl:choose>
<xsl:when test="$countC >= $index">
<xsl:value-of select="."/>
</xsl:when>
<xsl:otherwise>
<xsl:text> </xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:for-each>
<xsl:text> </xsl:text>
<xsl:for-each select="$uniqD">
<xsl:sort select="." data-type="number"/>
<xsl:if test="$index = position()">
<xsl:choose>
<xsl:when test="$countD >= $index">
<xsl:value-of select="."/>
</xsl:when>
<xsl:otherwise>
<xsl:text> </xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:for-each>
<xsl:text> </xsl:text>
<xsl:for-each select="$uniqE">
<xsl:sort select="."/>
<xsl:if test="$index = position()">
<xsl:choose>
<xsl:when test="$countE >= $index">
<xsl:value-of select="."/>
</xsl:when>
<xsl:otherwise>
<xsl:text> </xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:for-each>
<xsl:text> </xsl:text>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>

T:\ftemp>saxon -o murali.txt murali.xml murali.xsl

T:\ftemp>type murali.txt

C D E
======================
111 222 000
232 3232 777

T:\ftemp>echo Done!
Done!


--
Upcoming hands-on in-depth 3-days XSLT/XPath and/or 2-days XSL-FO:
- North America: Sep 30-Oct 4,2002
- Japan: Oct 7-Oct 11,2002

G. Ken Holman mailto:gkholman@CraneSoftwrights.com
Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0 +1(613)489-0999 (Fax:-0995)
ISBN 0-13-065196-6 Definitive XSLT and XPath
ISBN 1-894049-08-X Practical Transformation Using XSLT and XPath
ISBN 1-894049-07-1 Practical Formatting Using XSLFO
XSL/XML/DSSSL/SGML/OmniMark services, books (electronic, printed),
articles, training (instructor-live,Internet-live,web/CD,licensed)
Next public training: 2002-08-05,26,27,09-30,10-03,07,10


XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]