Newbie xsl:sort and numbering question

Newbie xsl:sort and numbering question

Post by Tob » Sat, 27 Jul 2002 00:52:30



Hi there,

I have an XML document that I'm applying an XSL transformation to.
I'm trying to apply a sequential numerical attribute to every element
of the resulting XML document and THEN alphabetize it.  My problem is
that when I apply the <xsl:sort select"term"> element, the XML
document is first sorted, then the numberical attribute applied.  This
is a hard thing to explain in words, so below are samples of the XML
documents and stylesheets.

Original XML doc (Snippet):
<entry id="ENG0451" cl="u">
  <keyForm type="word" lang="eng" reg="modern written">
    <term scr="en" orth="normal">cat</term>
  </keyForm>
</entry>
<entry id="ENG0452" cl="u">
  <keyForm type="word" lang="eng" reg="modern written">
    <term scr="en" orth="normal">dog</term>
  </keyForm>
</entry>
<entry id="ENG0454" cl="u">
  <keyForm type="word" lang="eng" reg="modern written">
    <term scr="en" orth="normal">bird</term>
  </keyForm>
</entry>
<entry id="ENG0453" cl="u">
  <keyForm type="word" lang="eng" reg="modern written">
    <term scr="en" orth="normal">fruitfly</term>
  </keyForm>
</entry>
<entry id="ENG0456" cl="u">
  <keyForm type="word" lang="eng" reg="modern written">
    <term scr="en" orth="normal">fish</term>
  </keyForm>
</entry>

Stylesheet:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
  <xsl:output method="xml" encoding="utf-8"/>
  <xsl:template match="/">
    <xsl:element name="indexlist">
    <xsl:attribute name="count"><xsl:value-of
select="count(//entry)"/></xsl:attribute>
    <xsl:for-each select="//keyForm">
      <xsl:sort select="term"/>
      <xsl:element name="headword">
        <xsl:attribute name="index"><xsl:value-of
select="position()"/></xsl:attribute>

      </xsl:element>
      </xsl:for-each>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

The resulting document would look something like this:
<indexlist count=5>
  <headword index=1>bird</headword>
  <headword index=2>cat</headword>
  <headword index=3>dog</headword>
  <headword index=4>fish</headword>
  <headword index=5>fruitfly</headword>
</indexlist>
This isn't what I want.  I want the index attribute to be applied, and
then the elements to be sorted.

This is what I'm hoping to achieve:
<indexlist count=5>
  <headword index=3>bird</headword>
  <headword index=1>cat</headword>
  <headword index=2>dog</headword>
  <headword index=5>fish</headword>
  <headword index=4>fruitfly</headword>
</indexlist>

Notice that the index attribute in this example corresponds to the
position of the element in the original document.  Is this possible?
I'm brand new to XSL so please forgive me if this is a silly question.
 Thanks much in advance!

Toby

 
 
 

Newbie xsl:sort and numbering question

Post by Marro » Sat, 27 Jul 2002 01:19:15


Hi Toby,

Something like?...

== XSL ====================================
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.veryComputer.com/;
xmlns:fo="http://www.veryComputer.com/;>
  <xsl:output method="xml" />
  <xsl:template match="/">
    <xsl:element name="indexlist">
      <xsl:attribute name="count">
        <xsl:value-of select="count(//entry)"/>
      </xsl:attribute>
      <xsl:for-each select="//keyForm">
        <xsl:sort select="term"/>
        <xsl:element name="headword">
          <xsl:attribute name="index">
            <xsl:number count="entry"/>
<!--            <xsl:value-of select="position()"/> -->
          </xsl:attribute>

        </xsl:element>
      </xsl:for-each>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>
== end of XSL ===============================

Hope this helps
Marrow
http://www.veryComputer.com/ - home of Xselerator (XSLT IDE and de*)
http://www.veryComputer.com/


>Hi there,

>I have an XML document that I'm applying an XSL transformation to.
>I'm trying to apply a sequential numerical attribute to every element
>of the resulting XML document and THEN alphabetize it.  My problem is
>that when I apply the <xsl:sort select"term"> element, the XML
>document is first sorted, then the numberical attribute applied.  This
>is a hard thing to explain in words, so below are samples of the XML
>documents and stylesheets.

>Original XML doc (Snippet):
><entry id="ENG0451" cl="u">
>  <keyForm type="word" lang="eng" reg="modern written">
>    <term scr="en" orth="normal">cat</term>
>  </keyForm>
></entry>
><entry id="ENG0452" cl="u">
>  <keyForm type="word" lang="eng" reg="modern written">
>    <term scr="en" orth="normal">dog</term>
>  </keyForm>
></entry>
><entry id="ENG0454" cl="u">
>  <keyForm type="word" lang="eng" reg="modern written">
>    <term scr="en" orth="normal">bird</term>
>  </keyForm>
></entry>
><entry id="ENG0453" cl="u">
>  <keyForm type="word" lang="eng" reg="modern written">
>    <term scr="en" orth="normal">fruitfly</term>
>  </keyForm>
></entry>
><entry id="ENG0456" cl="u">
>  <keyForm type="word" lang="eng" reg="modern written">
>    <term scr="en" orth="normal">fish</term>
>  </keyForm>
></entry>

>Stylesheet:
><?xml version="1.0" encoding="UTF-8"?>
><xsl:stylesheet version="1.0"
>xmlns:xsl="http://www.veryComputer.com/;
>xmlns:fo="http://www.veryComputer.com/;>
>  <xsl:output method="xml" encoding="utf-8"/>
>  <xsl:template match="/">
>    <xsl:element name="indexlist">
>    <xsl:attribute name="count"><xsl:value-of
>select="count(//entry)"/></xsl:attribute>
>    <xsl:for-each select="//keyForm">
>      <xsl:sort select="term"/>
>      <xsl:element name="headword">
>        <xsl:attribute name="index"><xsl:value-of
>select="position()"/></xsl:attribute>

>      </xsl:element>
>      </xsl:for-each>
>    </xsl:element>
>  </xsl:template>
></xsl:stylesheet>

>The resulting document would look something like this:
><indexlist count=5>
>  <headword index=1>bird</headword>
>  <headword index=2>cat</headword>
>  <headword index=3>dog</headword>
>  <headword index=4>fish</headword>
>  <headword index=5>fruitfly</headword>
></indexlist>
>This isn't what I want.  I want the index attribute to be applied, and
>then the elements to be sorted.

>This is what I'm hoping to achieve:
><indexlist count=5>
>  <headword index=3>bird</headword>
>  <headword index=1>cat</headword>
>  <headword index=2>dog</headword>
>  <headword index=5>fish</headword>
>  <headword index=4>fruitfly</headword>
></indexlist>

>Notice that the index attribute in this example corresponds to the
>position of the element in the original document.  Is this possible?
>I'm brand new to XSL so please forgive me if this is a silly question.
> Thanks much in advance!

>Toby


 
 
 

1. [xsl:number] Newbie question

Hi

Sorry for my bad english.

I've this XML document (part of-)

<tableau>
   <line>
      <champ><value>titi</value></champ>
<champ><value>titi</value><champ-nota>Commentaire</champ-nota></champ>

<champ><value>titi</value><champ-nota>Commentaire</champ-nota></champ>
      <champ><value>titi</value></champ>
      <champ><value>titi</value></champ>
      </line>
   <line>
        ......
   </line>
</tableau>

I want to tranform this document in HTML with a table. But in this table
I want all Commentaires (in <champ-nota>) is out of table and have  a
numerotation.

For this I use <xsl:number> but my XSLT processor (Saxon) don't want
count from the first noeud. He put alway a empty result and count from
the second noeud.

Anybody can help me ? Please...;-))

Maybe it's no go method for this.

Thanks for advance

Z.

*******************************************

<xsl:template match="tableau">
   <center>
    <table border="1">
     <xsl:apply-templates select="line"/>
    </table>
   </center>
<xsl:if test="line/champ/champ-nota">
          <ul><ol style="text-align: left;"><xsl:apply-templates
select="line/champ/champ-nota"/></ol>
          </ul>
   </xsl:if>
</xsl:template>

<xsl:template match="line">
          <tr><xsl:apply-templates select="champ"/></tr>
</xsl:template>

<xsl:template match="champ">
<xsl:choose>
   <xsl:when test="champ-nota">
<td>
    <xsl:number level="any"  from="tableau" count="champ-nota"/>
    <a href="#{generate-id(champ-nota)}">
      <xsl:value-of select="value"/>
     </a>
</td>
</xsl:when>
   <xsl:otherwise>
    <td><xsl:value-of select="value"/></td>
   </xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template match="champ-nota">
          <li><a name="{generate-id(.)}"/><xsl:apply-templates/></li>
</xsl:template>

***************************

2. Question on suported TRAPS.

3. Newbie: xsl:sort question

4. Pointer: Foiling spam and other procmail email-filter tips

5. question regarding "sort key" with xsl:sort

6. Xemplar NC help needed

7. XSL/XPATH and sorting abs(number)

8. Question for a "sort of" newbie

9. Sorting Numbers using XSL.

10. Number() sorting in XSL

11. xsl:sort when mix of numbers and null values

12. xsl:sort problems with large number