This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: Seeking help on Grouping distingt sub-elements
- From: "G. Ken Holman" <gkholman at CraneSoftwrights dot com>
- To: XSL-List at lists dot mulberrytech dot com
- Date: Mon, 06 May 2002 11:54:37 -0400
- Subject: Re: [xsl] Seeking help on Grouping distingt sub-elements
- Reply-to: xsl-list at lists dot mulberrytech dot com
At 2002-05-06 11:00 -0400, jestoll@crtinc.com wrote:
>I have tried various implemenations of 'Meunchian' groupings,
When doing subgrouping, sometimes the Meunchian technique is more awkward
than using simple variables.
>In the end, I think its coming down to a lack of sufficient understanding
>of the <xsl:key> tag on my part.
Don't always jump to the conclusion that <xsl:key> must be used. Though
there are times when that technique is superior to what is below, I think
what is below will be the best technique for subgrouping for you given your
needs.
Attached below is a working solution. Note that this revealed to me that
you have in your input a duplicate specific element value of "271" instead
of one of them being "272" to get the answer you were looking for.
You'll have to polish up the other elements as you need, I've used a
shortcut to get the copied nodes copied.
The essence of the algorithm is to only create a variable of the chassis
elements being grouped, and then only act on the first member of the
variable for each unique commonid value. Getting access to the last of the
specific to use for the commonelement value is very straightforward.
I hope this helps.
.................. Ken
T:\ftemp>type stoll.xml
<order>
<company>
<name>company1</name>
<shipment>
<shiptoid>1</shiptoid>
<chassis>
<commonid>19</commonid>
<commonelement>c191</commonelement>
<specificelement>s191</specificelement>
</chassis>
<chassis>
<commonid>17</commonid>
<commonelement>c171</commonelement>
<specificelement>s171</specificelement>
</chassis>
<chassis>
<commonid>19</commonid>
<commonelement>c192</commonelement>
<specificelement>s192</specificelement>
</chassis>
<chassis>
<commonid>17</commonid>
<commonelement>c172</commonelement>
<specificelement>s172</specificelement>
</chassis>
</shipment>
<shipment>
<shiptoid>2</shiptoid>
<chassis>
<commonid>18</commonid>
<commonelement>c181</commonelement>
<specificelement>s181</specificelement>
</chassis>
<chassis>
<commonid>16</commonid>
<commonelement>c161</commonelement>
<specificelement>s161</specificelement>
</chassis>
<chassis>
<commonid>18</commonid>
<commonelement>c182</commonelement>
<specificelement>s182</specificelement>
</chassis>
<chassis>
<commonid>16</commonid>
<commonelement>c162</commonelement>
<specificelement>s162</specificelement>
</chassis>
</shipment>
</company>
<company>
<name>company2</name>
<shipment>
<shiptoid>3</shiptoid>
<chassis>
<commonid>29</commonid>
<commonelement>c291</commonelement>
<specificelement>s291</specificelement>
</chassis>
<chassis>
<commonid>27</commonid>
<commonelement>c271</commonelement>
<specificelement>s271</specificelement>
</chassis>
<chassis>
<commonid>29</commonid>
<commonelement>c292</commonelement>
<specificelement>s292</specificelement>
</chassis>
<chassis>
<commonid>27</commonid>
<commonelement>c271</commonelement>
<specificelement>s271</specificelement>
</chassis>
</shipment>
<shipment>
<shiptoid>4</shiptoid>
<chassis>
<commonid>28</commonid>
<commonelement>c281</commonelement>
<specificelement>s281</specificelement>
</chassis>
<chassis>
<commonid>26</commonid>
<commonelement>c261</commonelement>
<specificelement>s261</specificelement>
</chassis>
<chassis>
<commonid>28</commonid>
<commonelement>c282</commonelement>
<specificelement>s282</specificelement>
</chassis>
<chassis>
<commonid>26</commonid>
<commonelement>c262</commonelement>
<specificelement>s262</specificelement>
</chassis>
</shipment>
</company>
</order>
T:\ftemp>type stoll.xsl
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output indent="yes"/>
<xsl:template match="order|company|name|shiptoid">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="shipment">
<shipment>
<xsl:apply-templates select="shiptoid"/>
<xsl:variable name="chassis" select="chassis"/>
<xsl:for-each select="chassis">
<xsl:if test="generate-id(.)=
generate-id($chassis[commonid=current()/commonid])">
<xsl:variable name="specifics"
select="$chassis[commonid=current()/commonid]"/>
<chassis>
<commonid><xsl:value-of select="commonid"/></commonid>
<commonelement>
<xsl:value-of select="$specifics[last()]/commonelement"/>
</commonelement>
<newtag>
<xsl:for-each select="$specifics">
<specificelement>
<xsl:value-of select="specificelement"/>
</specificelement>
</xsl:for-each>
</newtag>
</chassis>
</xsl:if>
</xsl:for-each>
</shipment>
</xsl:template>
</xsl:stylesheet>
T:\ftemp>saxon -o stoll.out stoll.xml stoll.xsl
T:\ftemp>type stoll.out
<?xml version="1.0" encoding="utf-8"?>
<order>
<company>
<name>company1</name>
<shipment>
<shiptoid>1</shiptoid>
<chassis>
<commonid>19</commonid>
<commonelement>c192</commonelement>
<newtag>
<specificelement>s191</specificelement>
<specificelement>s192</specificelement>
</newtag>
</chassis>
<chassis>
<commonid>17</commonid>
<commonelement>c172</commonelement>
<newtag>
<specificelement>s171</specificelement>
<specificelement>s172</specificelement>
</newtag>
</chassis>
</shipment>
<shipment>
<shiptoid>2</shiptoid>
<chassis>
<commonid>18</commonid>
<commonelement>c182</commonelement>
<newtag>
<specificelement>s181</specificelement>
<specificelement>s182</specificelement>
</newtag>
</chassis>
<chassis>
<commonid>16</commonid>
<commonelement>c162</commonelement>
<newtag>
<specificelement>s161</specificelement>
<specificelement>s162</specificelement>
</newtag>
</chassis>
</shipment>
</company>
<company>
<name>company2</name>
<shipment>
<shiptoid>3</shiptoid>
<chassis>
<commonid>29</commonid>
<commonelement>c292</commonelement>
<newtag>
<specificelement>s291</specificelement>
<specificelement>s292</specificelement>
</newtag>
</chassis>
<chassis>
<commonid>27</commonid>
<commonelement>c271</commonelement>
<newtag>
<specificelement>s271</specificelement>
<specificelement>s271</specificelement>
</newtag>
</chassis>
</shipment>
<shipment>
<shiptoid>4</shiptoid>
<chassis>
<commonid>28</commonid>
<commonelement>c282</commonelement>
<newtag>
<specificelement>s281</specificelement>
<specificelement>s282</specificelement>
</newtag>
</chassis>
<chassis>
<commonid>26</commonid>
<commonelement>c262</commonelement>
<newtag>
<specificelement>s261</specificelement>
<specificelement>s262</specificelement>
</newtag>
</chassis>
</shipment>
</company>
</order>
--
Upcoming: 3-days XSLT/XPath and/or 2-days XSLFO: June 17-21, 2002
- : 3-days XML Information Modeling: July 31-August 2, 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-05-06,07,09,10,13,15,20,
- 06-04,07,10,11,13,14,17,20,07-31,08-05,27,30
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list