this is a FAQ about XSLT. You find the solution nearly everywhere (and below), e.g. at http://www.jenitennison.com/xslt/grouping/muenchian.xml. If not, there are specific XSLT list, the best probably at http://www.mulberrytech.com/xsl/xsl-list/
Enough admonished :)
Now the answer ...
On 16.04.2004 17:32, [EMAIL PROTECTED] wrote:
Hello,
I have a problem with an XPath. My XML-Document that i want to format with my XSLT-stylesheet is a storylist which contains a number of stories which contains a title and a body-element. The body element contains an attribute called language. Here is a excerpt of my DTD:
<!ELEMENT storylist (story*) >
<!ELEMENT story (body, title) >
<!ATTLIST story
id ID #REQUIRED xreftext CDATA #IMPLIED author CDATA #IMPLIED category CDATA #IMPLIED>
<!ELEMENT body (#PCDATA | xref | seealso | url)* >
<!ATTLIST body
language (en|de|es|it|jp) "de"> ...
I'm using a key 'language-index' to get all messages of the same languages: <xsl:key name="language-index" match="body" use="@language"/>
I want to iterate through those stories after i sort the by the
author-attribute that is in the body of the story i do this inside
<xsl:template match="/">: <xsl:for-each select="key('language-index', $targetLanguage)">
<xsl:sort select="ancestor::story/@author"/>
Now i want to print some text only if the author of the story has
changed from the preceding story.
Stating your problem is of course important for finding solutions. With this the Mulberry XSL list mentioned above can help you a lot as there are many mind-readers subscribed ;-) What you want to do is grouping the stories by authors. The Muenchian Grouping is the common approach:
<xsl:key name="author-index" match="story" use="@author"/>
<xsl:template match="/storylist">
<table>
<xsl:apply-templates select="story[generate-id() = generate-id(key('author-index', @author))]" mode="group">
<xsl:sort select="@author"/>
</xsl:apply-templates>
</table>
</xsl:template>
<xsl:template match="story" mode="group">
<!-- first story of this author => group starter -->
<tr>
<th><xsl:value-of select="@author"/></th>
</tr>
<!-- select all stories of this author -->
<xsl:apply-templates select="key('author-index', @author)"/>
</xsl:template><xsl:template match="story"> <tr><td><xsl:value-of select="title"/></td></tr> </xsl:template>
This misses now the handling of language, but I think you can add it easily. From you original code I assume you only need onle language at a time, so you should add this to the key. If you need instead an additional grouping by language it gets a bit more complicated. You have to setup a second key concatenated from the author and the language.
My problem is that i don't know how to access the author-attribute of the preceding sibling. I have tried a couple of different syntaxes but none of them worked what i want is someting like this: preceding-sibling::story[1]/@author but that returns NULL.
The problem is, this can not work. Even if you sort the nodes the order of them when using axes like preceding-sibling is the original one.
i can access the author associate with the current body-node like this: ancestor::story[1]/@author
You should not use ancestor, but parent axis or its short form: parent::story/@author or ../@author
Joerg
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
