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
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):
Stylesheet:
The resulting document would look something like this:
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:
Notice that the
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
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"term">
Original XML doc (Snippet):
Code:
<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:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform"[/URL]
xmlns:fo="[URL unfurl="true"]http://www.w3.org/1999/XSL/Format">[/URL]
<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:value-of select='term[@orth="normal"]'/>
</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 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
Toby