Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > newbie: xsl:key & generate-id = problem

Thread Tools

newbie: xsl:key & generate-id = problem

Posts: n/a

i have following xml snippet. for every par-element i would like to
create a key which value is the ID of its related* pardef-element. but
the xsl-key element seems to be incorrect and i can't figure out why or
how to achieve this.

do you have any ideas?


<pardef id='1'/>
<par def='1'>

<pardef id='2' list='bullet'/>
<par def='2'>

<par def='1'>


<xsl:key name="par" match="par"

every <par def='1'> should have a reference to <pardef id='1'/>
every <par def='2'> should have a reference to <pardef id='2'/>

Reply With Quote
George Bina
Posts: n/a
When you define a key you need to specify two thinks along with the key
name: what nodes the key matches (that is what the key funtion will
return when you will use it) and what is the value you want to look up
those nodes with.
In your case if you want to have a reference to pardef then pardef is
what the key should match. Now what you know is the id attribute so
that is the value you want to look up the pardef with. The key should
be defined as:

<xsl:key name="pd" match="pardef" use="@id"/>

Here it is a sample stylesheet that matches on par elements and shows
the associated pardef using the above key:

<xsl:stylesheet version="1.0"
<xsl:key name="pd" match="pardef" use="@id"/>
<xsl:template match="/">
<xsl:template match="par">
<xsl:text>For </xsl:text>
<xsl:value-of select="@def"/>
<xsl:text> we got </xsl:text>
<xsl:copy-of select="key('pd', @def)"/>

The result on your sample input file is:
<?xml version="1.0" encoding="UTF-16"?><result>
<pair>For 1 we got <pardef id="1" /></pair>
<pair>For 2 we got <pardef id="2" list="bullet" /></pair>
<pair>For 1 we got <pardef id="1" /></pair>

Best Regards,
George Cristian Bina
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem problem problem :( Need Help Mike ASP General 2 05-11-2004 08:36 AM