xsl-list
[Top] [All Lists]

RE: <xsl:sort>

2005-05-07 01:33:34
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(_at_)optonline(_dot_)net>
Reply-To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
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



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--


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


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



<Prev in Thread] Current Thread [Next in Thread>