Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

newbie xsl:sort and indexing question.

Status
Not open for further replies.

MobyToby

Programmer
Jul 24, 2002
2
US
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
Code:
<xsl:sort select&quot;term&quot;>
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):
Code:
<entry id=&quot;ENG0451&quot; cl=&quot;u&quot;>
  <keyForm type=&quot;word&quot; lang=&quot;eng&quot; reg=&quot;modern written&quot;>
    <term scr=&quot;en&quot; orth=&quot;normal&quot;>cat</term>
  </keyForm>
</entry>
<entry id=&quot;ENG0452&quot; cl=&quot;u&quot;>
  <keyForm type=&quot;word&quot; lang=&quot;eng&quot; reg=&quot;modern written&quot;>
    <term scr=&quot;en&quot; orth=&quot;normal&quot;>dog</term>
  </keyForm>
</entry>
<entry id=&quot;ENG0454&quot; cl=&quot;u&quot;>
  <keyForm type=&quot;word&quot; lang=&quot;eng&quot; reg=&quot;modern written&quot;>
    <term scr=&quot;en&quot; orth=&quot;normal&quot;>bird</term>
  </keyForm>
</entry>
<entry id=&quot;ENG0453&quot; cl=&quot;u&quot;>
  <keyForm type=&quot;word&quot; lang=&quot;eng&quot; reg=&quot;modern written&quot;>
    <term scr=&quot;en&quot; orth=&quot;normal&quot;>fruitfly</term>
  </keyForm>
</entry>
<entry id=&quot;ENG0456&quot; cl=&quot;u&quot;>
  <keyForm type=&quot;word&quot; lang=&quot;eng&quot; reg=&quot;modern written&quot;>
    <term scr=&quot;en&quot; orth=&quot;normal&quot;>fish</term>
  </keyForm>
</entry>

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

The resulting document would look something like this:
Code:
<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 the the elements to be sorted.

This is what I'm hoping to achieve:
Code:
<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
Code:
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
 
it looks like xsl sort causes the parser to sort the items and then apply the for-each.

its not a silly question at all, and looks like it needs some serious lateral thinking.

The way to do it is to create the nodes as part of the working document, unordered, and then use xsl value-of on the with sort on to output them to the document proper. in real terms I beleive you need to use xsl:variable containing a xsl:copy of the the nodes you want to pre-identify with the position, and then apply a template to sort them and display them -- however I cant get my version to work!! I found this on ms website, where you need to use msxml extensions (but i think other parsers tread variables as nodesets anyway):


have a look, it may give you some ideas.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top