xsl-list
[Top] [All Lists]

Re: [xsl] Sorting using helper structure, position(), xsl:key

2008-04-10 07:12:57
David Carlisle schrieb:
First question: Is it possible, in XSLT 1.0, to sort my items
based on a helper structure like the following,

yes

if $cat stores <Categories) then

<xsl:sort
select="count($cat/Cat[.=current()/@cat]/preceding-sibling::Cat)"

Prompt and terrific response! Thanks, David! I had zeroed in on
position() to solve the problem and didn't think of count(), which
does just what I want.

$cat neets to be a node set (of one node) not an RTF so you need to
put the maping in a source file, perhaps loaded by document() or if
you put it into a RTF variable in the stylesheet use an x;node-set()
extension to make it into a node set. Or use xslt2 where this RTF
nonsense goes away.

Have to use 1.0. When I have to access an RTF more than once, I declare
a wrapper variable doing the node-set conversion.

Finally, for a huge set of categories, I'd want to use an
xsl:key. How would that fit into the picture? Is it possible
to have the key return the context position? Or would this be
difficult as maybe there is no context when an xsl:key is
built?

The above question was probably inintelligible. I was thinking of
position() when I should have been thinking in terms of count().

in xslt2 you koul just use <xsl:sort select="key('cat',@cat,$cat)" with
the new 3rd argument telling teh system which document to use for the
key, but in xslt1 you have to be in the right document already, which
means that in teh restricted context of xsl:sort you can only use a key
if your mappig element is in teh same document as the nodes over which
you are iterating.

Thanks once more! For completeness and posterity, here's your solution
applied to my problem and sample data.

Michael

<xsl:transform version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
  <xsl:output indent="yes"/>
  <xsl:key name="kcat" match="Cat" use="."/>
  <xsl:template match="XPG">
    <Items>
      <xsl:for-each select="Item">
        <xsl:sort select="count(
          key('kcat', @cat)/preceding-sibling::Cat )"/>
        <xsl:copy-of select="."/>
      </xsl:for-each>
    </Items>
  </xsl:template>
</xsl:transform>

<XPG>
  <Categories>
    <Cat>film</Cat>
    <Cat>spor</Cat>
    <Cat>kult</Cat>
    <Cat>info</Cat>
    <Cat>kind</Cat>
  </Categories>
  <Item cat="spor">Fußball</Item>
  <Item cat="spor">Laufen</Item>
  <Item cat="info">Tagesschau</Item>
  <Item cat="spor">Tennis</Item>
  <Item cat="info">Wochenschau</Item>
  <Item cat="kult">Theater</Item>
  <Item cat="kind">Heini</Item>
  <Item cat="kult">Kunst</Item>
  <Item cat="kind">Seppel</Item>
  <Item cat="film">Casablanca</Item>
</XPG>

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