Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 7th, 2007, 09:24 AM
Registered User
 
Join Date: Nov 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default Sorting Elements

I have written an XSL script and everything works great except the sorting. I need the data to sort by <prtnbr> but cannot accomplish this unless I remove 2 columns of required data that is generated by attributes.

Here is a sample of the XML file:

<subject chapnbr="23" id="sj232000" sectnbr="20" subjnbr="00">
<dpfigure chg="u" figcnt="10" id="sj23200010">
<prtlist>
<prtrow><item>005</item><prtnbr>822-1071-001</prtnbr><qty>1</qty></prtrow>
<prtrow><item>010</item><prtnbr>372254-91</prtnbr><qty>1</qty></prtrow>
<prtrow><item>015</item><prtnbr>372254-93</prtnbr><qty>1</qty></prtrow>
<prtrow><item>020</item><prtnbr>622-4957-001</prtnbr><qty>1</qty></prtrow>
</prtlist>
</dpfigure>
</subject>

Here is part of the XSL file:

<xsl:apply-templates select="//subject">
    <xsl:sort select="prtnbr">
</xsl:apply-templates>



<xsl:template match="subject">
        <xsl:variable name="chsecsub">
            <xsl:value-of select="@chapnbr"/>
            <xsl:text>-</xsl:text>
            <xsl:value-of select="@sectnbr"/>
            <xsl:text>-</xsl:text>
            <xsl:value-of select="@subjnbr"/>
        </xsl:variable>
        <xsl:for-each select="dpfigure">
            <xsl:variable name="figcnt" select="@figcnt"/>
            <xsl:for-each select="prtlist/prtrow">
                <fo:table-row>
                    <fo:table-cell>
                        <fo:block>
                            <xsl:value-of select="prtnbr"/>
                        </fo:block>
                    </fo:table-cell>
                    <fo:table-cell>
                        <fo:block>
                            <xsl:value-of select="$chsecsub"/>
                        </fo:block>
                    </fo:table-cell>
                    <fo:table-cell>
                        <fo:block>
                            <xsl:value-of select="$figcnt"/>
                        </fo:block>
                    </fo:table-cell>
                    <fo:table-cell text-align="center">
                        <fo:block>
                            <xsl:value-of select="item"/>
                        </fo:block>
                    </fo:table-cell>
                    <fo:table-cell>
                        <fo:block>
                            <xsl:value-of select="qty"/>
                        </fo:block>
                    </fo:table-cell>
                </fo:table-row>
            </xsl:for-each>
        </xsl:for-each>
    </xsl:template>

Any help would be greatly appreciated as I have been struggling with this for several days now.
 
Old November 7th, 2007, 09:42 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Your problem is here:

<xsl:apply-templates select="//subject">
    <xsl:sort select="prtnbr">
</xsl:apply-templates>

You are sorting the subject elements only - in your sample there is only 1 subject element, so the order is insignificant.

Add <xsl:sort select="." /> after the <xsl:for-each select="prtlist/prtrow"> (the context of the sort is the collection of nodes to sort, in this case the prtrows).

/- Sam Judson : Wrox Technical Editor -/
 
Old November 7th, 2007, 10:26 AM
Registered User
 
Join Date: Nov 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you so much for your quick response. I should have been a little more detailed on the input file. There are multiple subjects that I am combining into 1 master index and the combination of them all need to be sorted by part number.

Here is a more detailed XML input file:

<chapter chapnbr="23" id="ch23">
<section chapnbr="23" id="sc2320" sectnbr="20">
<subject chapnbr="23" id="sj232000" sectnbr="20" subjnbr="00">
<dpfigure chg="u" figcnt="10" id="sj23200010">
<prtlist>
<prtrow><item>005</item><prtnbr>822-1071-001</prtnbr><qty>1</qty></prtrow>
<prtrow><item>010</item><prtnbr>372254-91</prtnbr><qty>1</qty></prtrow>
<prtrow><item>015</item><prtnbr>372254-93</prtnbr><qty>1</qty></prtrow>
<prtrow><item>020</item><prtnbr>622-4957-001</prtnbr><qty>1</qty></prtrow>
</prtlist>
</dpfigure>
</subject>
<subject chapnbr="23" id="sj232000" sectnbr="20" subjnbr="10">
<dpfigure chg="u" figcnt="20" id="sj23200010">
<prtlist>
<prtrow><item>005</item><prtnbr>1071-001</prtnbr><qty>1</qty></prtrow>
<prtrow><item>010</item><prtnbr>72254-91</prtnbr><qty>1</qty></prtrow>
<prtrow><item>015</item><prtnbr>3254-93</prtnbr><qty>1</qty></prtrow>
<prtrow><item>020</item><prtnbr>62-4957-001</prtnbr><qty>1</qty></prtrow>
</prtlist>
</dpfigure>
</subject>
<subject chapnbr="23" id="sj232000" sectnbr="20" subjnbr="30">
<dpfigure chg="u" figcnt="30" id="sj23200010">
<prtlist>
<prtrow><item>005</item><prtnbr>MK1071-001</prtnbr><qty>1</qty></prtrow>
<prtrow><item>010</item><prtnbr>54AB-91</prtnbr><qty>1</qty></prtrow>
<prtrow><item>015</item><prtnbr>3254-LO</prtnbr><qty>1</qty></prtrow>
<prtrow><item>020</item><prtnbr>JT-4957-001</prtnbr><qty>1</qty></prtrow>
</prtlist>
</dpfigure>
</subject>
</section>
</chapter>

The desired output would be:

Part Number Ch/Sec/Sub Fig. Item Qty.

1071-001 23-20-10 20 105 1
3254-93 23-20-10 20 025 1
3254-LO 23-20-20 30 015 1
372254-91 23-20-00 10 010 1
372254-93 23-20-00 10 015 1
54AB-91 23-20-20 30 010 1
622-4957-001 23-20-00 10 020 1
623-4957-001 23-20-10 20 030 1
72254-91 23-20-10 20 015 1
822-1071-001 23-20-00 10 005 1
JT-4957-001 23-20-20 30 020 1
MK1071-001 23-20-20 30 005 1



Thanks so much in advance for all your help.
 
Old November 7th, 2007, 10:38 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

This is just to give you an idea of how you might do this (I'm not reproducing all your FO code):

<xsl:template match="chapter">

   <xsl:for-each select="descendant::prtrow">
     <xsl:sort select="prtnbr" />
     <row>
       <cell><xsl:value-of select="ancestor::chapter/@chapnbr"/></cell>
       <cell><xsl:value-of select="item"/></cell>
       <cell><xsl:value-of select="prtnbr"/></cell>
     <row>
   </xsl:for-each>

</xsl:template>

/- Sam Judson : Wrox Technical Editor -/
 
Old November 7th, 2007, 11:14 AM
Registered User
 
Join Date: Nov 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have to say your the BOMB!!! Just a few code changes and it works great!! Thanks so much!!:D





Similar Threads
Thread Thread Starter Forum Replies Last Post
sorting nested elements again stekker XSLT 1 June 5th, 2006 03:38 AM
sorting nested elements stekker XSLT 5 June 5th, 2006 01:19 AM
elements with same name kfir XML 8 May 1st, 2006 07:42 AM
sorting elements with composed characters muki XSLT 6 February 13th, 2006 04:29 AM
Datagrid sorting by non alphabetical sorting? LLAndy VS.NET 2002/2003 1 July 15th, 2004 01:20 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.