472,127 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,127 software developers and data experts.

Problems getting xsl:key to work....

Folks,

I'm struggling with XSLT key's and stuff.... anybody out there who
*REALLY* understands this stuff???

Okay, my XML file looks something like this (partially):

<?xml version="1.0" encoding="ISO-8859-1"?>
<report>
<meta>
<structure>
<title seq="4" text="Foo" />
<title seq="5" text="Bar" />
<title seq="6" text="Widgets" />
<title seq="16" text="Foo" />
<title seq="17" text="Bar" />
</structure>
</meta>
</report>

What I'd like to achieve is to get a list of comma-separated values of
the "text" attribute, with each entry showing up only once, and
alphabetically sorted, something like:

Defined texts: Bar, Foo, Widgets

I tried to define a xsl:key that would help me achieve this - no luck
so far. I had a sample, but that was based on a XML element, rather
than an attribute - and I just can't seem to make the transition!

So here's my XSL definition of the key:

<xsl:key name="distinct" match="title" use="@text" />

so I was thinking this will keep a list of unique "text" attributes,
no?

I wanted to output the list from my XSL with this fragment:

<xsl:for-each
select="report/meta/structure/title[generate-id()=generate-id(key('distinct',
..))]">
<xsl:sort select="@title" />
<xsl:value-of select="@text">

<xsl:if test="position() != last()">, </xsl:if>
</xsl:for-each>

The extra clause in the xsl:for-each is the one I grabbed from a
sample, based on a XML element being used (rather than the attribute).

I just can't seem to make it work, no matter how I try to change
around the xsl:key and the xsl:for-each clauses.....

Any takers? It's probably really stupid, once you know how to do it -
but I just can't seem to crack this one!!

Any help is highly appreciated !!

Thanks
Marc

================================================== ==============
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
Nov 12 '05 #1
1 3324
Marc Scheuner [MVP ADSI] wrote:
I'm struggling with XSLT key's and stuff.... anybody out there who
*REALLY* understands this stuff???
Basically keys in XSLT are extremely simple. Just think of xsl:key as a
hashtable, where use attribute defines hashtable key and match attribute
defines value. Once defined, you can get values by keys.

I tried to define a xsl:key that would help me achieve this - no luck
so far. I had a sample, but that was based on a XML element, rather
than an attribute - and I just can't seem to make the transition!

So here's my XSL definition of the key:

<xsl:key name="distinct" match="title" use="@text" />

so I was thinking this will keep a list of unique "text" attributes,
no?

I wanted to output the list from my XSL with this fragment:

<xsl:for-each
select="report/meta/structure/title[generate-id()=generate-id(key('distinct',
.))]">


key('distinct', .) returns all stored values in "hahstable" (aka nodes
in xsl:key), by key, which is . - string value of title element, what is
obviously wrong, because your xsl:key defines text attribute value as
key (use="@text"). Try instead
<xsl:for-each
select="report/meta/structure/title[generate-id()=generate-id(key('distinct',@text))]">
--
Oleg Tkachenko
XmlInsider
http://blog.tkachenko.com
Nov 12 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Darren Gilroy | last post: by
reply views Thread by Stefan Slapeta | last post: by
1 post views Thread by Vishnu | last post: by
3 posts views Thread by bjam | last post: by
1 post views Thread by Srinvias Vikram | last post: by
reply views Thread by Nadav | last post: by
3 posts views Thread by Jerry | last post: by
reply views Thread by Leira | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.