Subject: Re: Node list operations From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx> Date: Sun, 09 May 1999 21:17:55 -0400 |
At 99/05/09 15:23 +0700, James Clark wrote: >You can do it (albeit inefficiently) like this: Thank you James ... I hadn't considered walking over the entire list each time and only looking at a single entry of the list when doing so. I think your approach exemplifies an approach to a class of problems that people will come across in their work. I've taken the time to add some comments to your code to illustrate for others how your solution works: <xsl:stylesheet xmlns:xsl="http://www.w3.org/XSL/Transform/1.0"> <!--show 'x' only when first in group of all 'x's--> <xsl:template match="v"> <xsl:text>X Y Z </xsl:text> <xsl:for-each select="w"> <!--walk the items in sorted order--> <xsl:sort select="x"/> <!--context node list becomes sorted--> <xsl:sort select="y"/> <xsl:choose> <xsl:when test="position()=1"> <xsl:value-of select="x"/><!--first in list is first in group--> </xsl:when> <xsl:otherwise> <!--get value for current position--> <xsl:variable name="pos" expr="position()"/> <xsl:variable name="x" expr="x"/> <xsl:for-each select="../w"> <!--re-walk the list again--> <!--(causing us to go over the 'n' items in the list a total of n times; hence the inefficiency)--> <xsl:sort select="x"/> <!--must do it in the same order--> <xsl:sort select="y"/> <!--stop at the position in list before current position; skipping processing for every other position in list--> <xsl:if test="position() = $pos - 1"> <xsl:choose> <!--examine position before current--> <xsl:when test="not(x=$x)"> <!--not same, so is first--> <xsl:value-of select="$x"/> <!--show value--> </xsl:when> <xsl:otherwise> <!--is same, so is not first in group--> <xsl:text> </xsl:text> </xsl:otherwise> </xsl:choose> </xsl:if> </xsl:for-each> <!--finish the re-walk of the list--> </xsl:otherwise> </xsl:choose> <xsl:text> </xsl:text> <!--display other associated values--> <xsl:value-of select="y"/> <xsl:text> </xsl:text> <xsl:value-of select="z"/> <xsl:text>
</xsl:text> <!--go to a new line--> </xsl:for-each> </xsl:template> </xsl:stylesheet> >"G. Ken Holman" wrote: >> Produce: >> >> X Y Z >> 1 1 a >> 2 b >> 3 c >> 2 1 d >> 2 e >> 3 1 f >> >> ..... suppressing redundant values for X. > >> Now, consider the problem where I want the same output from an unsorted >> collection: >> >> <?xml version="1.0"?> >> <v> >> <w><x>1</x><y>2</y><z>b</z></w> >> <w><x>3</x><y>1</y><z>f</z></w> >> <w><x>1</x><y>3</y><z>c</z></w> >> <w><x>2</x><y>2</y><z>e</z></w> >> <w><x>2</x><y>1</y><z>d</z></w> >> <w><x>1</x><y>1</y><z>a</z></w> >> </v> > >> So, my question is, can this problem be solved with the proposed working >> draft in a single pass, and if not, is it important enough a problem that >> it should be solved with version 1.0? Thank you again, James, for your tutelage. ........... Ken -- G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ Box 266, Kars, Ontario CANADA K0A-2E0 +1(613)489-0999 (Fax:-0995) Website: XSL/XML/DSSSL/SGML services outline, XSL/DSSSL shareware, stylesheet resource library, conference training schedule, commercial stylesheet training materials, on-line XSL CBT. Next instructor-led XSLT Training: WWW8:1999-05-11 XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: Node list operations, James Clark | Thread | Re: Simple suggestion for XSLT, James Clark |
Re: Simple suggestion for XSLT, James Clark | Date | stylesheets for links, Catherine Goodall |
Month |