RE: [xsl] <xsl:sort>

Subject: RE: [xsl] <xsl:sort>
From: aspsa <aspsa@xxxxxxxxxxxxx>
Date: Sat, 07 May 2005 14:52:44 -0400
Thanks for the feedback, Aron.

I had actually approached the problem this way originally, and it worked.
However, providing the precise order of the narrative portion of the
document would work for that document alone. If the XSLT were to be applied
to a different XML document, whereby the narrative portion is ordered
differently, I believe that approach would fail.

My goal is to build the stylesheet to accommodate this contingency.

Once again, any feedback is most appreciated.


Respectfully,

ASP

-----Original Message-----
From: Aron Bock [mailto:aronbock@xxxxxxxxxxx]
Sent: Saturday, May 07, 2005 4:34 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: RE: [xsl] <xsl:sort>


aspsa, it looks like your document initially has some descriptive elements
(header, paragraph(s), subheader, etc), then a list of product elements.  I
assume you want your output to be of similar structure: first descriptive
text, then a list of products.  Only, the products should be sorted.

So do something like this:

<xsl:template match="document">
    <xsl:value-of: ...header
    <xsl:value-of: ...subheader

    <!-- now the list

    <xsl:apply-templates select="product">
        <xsl:sort select="."/>
    /end-product
/end-document

Then an product template would get products in the right order:

<xsl:template match="product"
  ...
/end-product


As a matter of style it may be better specifically to select/process nodes
you want, than iterate all child nodes with child::, then branch based on a
subsequent test.  In XSL the presence of a specifically-matching template is
an implicit suggestion that you're interested in corresponding applicable
nodes, and you'd be best of taking advantage of this built-in mechanism for
selecting-and-branching rather than recreating it with <xsl:if>

Regards,

--A


>From: aspsa <aspsa@xxxxxxxxxxxxx>
>Reply-To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
>To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
>Subject: [xsl] <xsl:sort>
>Date: Sat, 07 May 2005 03:37:44 -0400
>
>Hi, folks.
>
>I have an XML document which structure is the following:
>
><document>
>   <header>
>   <paragraph>
>   <subheader>
>   <paragraph>
>   .
>   .
>   .
>   <product>
>     <partname>
>     <category>
>     .
>     .
>     .
>   </product>
>   <product>
>     <partname>
>     <category>
>     .
>     .
>     .
>   </product>
>   .
>   .
>   .
></document>
>
>In a template that matches on "document", I have the following:
>
><xsl:template match="document">
>   <xsl:for-each select="child::node()">
>     // etc...
>   </xsl:for-each>
></xsl:template>
>
>The XSLT stylesheet works fine, but now I need to sort the "product"
>elements based upon the "category" element and in descending order. The
>category content is numeric.
>
>How would I execute this from within the "document" template?
>
>Here's what I've tried.
>
><xsl:template match="document">
>   <xsl:for-each select="child::node()">
>     <xsl:if test="product">
>       <xsl:apply-templates select=".">
>         <xsl:sort select="category" data-type="number" order="descending"
>/>
>       </xsl:apply-templates>
>     </xsl:if>
>   </xsl:for-each>
></xsl:template>
>
>Where am I going wrong? Thanks for your feedback.
>
>
>Respectfully,
>
>ASP
>

_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today - it's FREE!
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/

Current Thread