xsl-list
[Top] [All Lists]

Re: Sorted node set and following-sibling axis

2005-08-10 11:42:43
Hi, Nick,

How do you determine which values go in which divs? That is, what is the 
logic behind the grouping? It appears to two to a div, but it's hard to 
tell with a small data sample. If you do just want to a div element around 
every pair of values, that's pretty straightforward:

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

  <xsl:output method="xml" indent="yes"/>

  <xsl:variable name="articles">
    <xsl:for-each select="/articles/article">
      <xsl:sort select="@priority" data-type="number" order="descending"/>
      <p><xsl:value-of select="."/></p>
    </xsl:for-each>
  </xsl:variable>

  <xsl:template match="articles">
    <xsl:for-each select="$articles/p">
      <xsl:if test="position() mod 2 = 1">
        <div>
          <xsl:copy-of select="."/>
          <xsl:if test="following-sibling::*[1]">
            <xsl:copy-of select="following-sibling::*[1]"/>
          </xsl:if>
        </div>
      </xsl:if>
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>

The notion is to first get a list of sorted p elements, which you can then 
stuff into whatever logic you need for div elements. In this case, I just 
grouped them as pairs (as your output showed). Speaking of output, here's 
what Saxon 8.5 gave me:

<div>
   <p>
    first
  </p>
   <p>
    second
  </p>
</div>
<div>
   <p>
    third
  </p>
   <p>
    last
  </p>
</div>

Jay Bryant
 Bryant Communication Services
(presently consulting at Synergistic Solution Technologies)





"Nick Fitzsimons" <nick(_at_)nickfitz(_dot_)co(_dot_)uk> 
08/10/2005 12:11 PM
Please respond to
xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com


To
xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
cc

Subject
[xsl] Sorted node set and following-sibling axis






Hi,

I've got a problem which I think may be beyond the powers of XSLT 1, but
maybe somebody can help me out.

The simplified structure of the data is as follows:

<articles>
  <article priority="3">
    <title>second</title>
  </article>
  <article priority="9">
    <title>first</title>
  </article>
  <article priority="1">
    <title>last</title>
  </article>
  <article priority="3">
    <title>third</title>
  </article>
</articles>

and I need to output it sorted on the "priority" attribute. Note that
a) a higher value of priority is sorted to the top of the output
(descending sort);
b) some articles have the same priority, in which case they are output in
source order;
c) values for priority are not necessarily contiguous.

There isn't a problem in the simple case:

<xsl:template match="articles">
  <xsl:apply-templates select="article">
    <xsl:sort select="@priority" data-type="number" order="descending" />
  </xsl:apply-templates>
</xsl:template>

<xsl:template match="article>
  <p><xsl:value-of select="title" /></p>
</xsl:template>

will give

<p>first</p>
<p>second</p>
<p>third</p>
<p>last</p>

However, what I require is the following:

<div>
  <p>first</p>
  <p>second</p>
</div>
<div>
  <p>third</p>
  <p>last</p>
</div>

The problem is that something like the following won't work:

<xsl:template match="article>
  <div>
    <p><xsl:value-of select="title" /></p>
    <p><xsl:value-of select="following-sibling::article/title" /></p>
  </div>
</xsl:template>

as the following-sibling axis returns the nodes in original document
order, not sorted order.

Any suggestions as to how I can achieve the desired output would be
appreciated.

(Unfortunately, the obvious solution of converting the data so that the
article elements are sorted before I get my hands on it won't work, as
this is legacy data, and the idea is precisely to avoid having to do
that... something to do with saving money on the back end.)

Thanks in advance for any suggestions,

Nick.
-- 
Nick Fitzsimons
http://www.nickfitz.co.uk/


--~------------------------------------------------------------------
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>
--~--




--~------------------------------------------------------------------
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>
--~--