newbie: xsl:key & generate-id = problem

I

iliad

hi!

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?


XML
=======================
<richtext>

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

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

<par def='1'>
textafter
</par>

</richtext>

XSL-KEY
=======================
<xsl:key name="par" match="par"
use="generate-id(pardef[@id=self::node[@def]])"/>

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

George Bina

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"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="pd" match="pardef" use="@id"/>
<xsl:template match="/">
<result>
<xsl:apply-templates/>
</result>
</xsl:template>
<xsl:template match="par">
<pair>
<xsl:text>For </xsl:text>
<xsl:value-of select="@def"/>
<xsl:text> we got </xsl:text>
<xsl:copy-of select="key('pd', @def)"/>
</pair>
</xsl:template>
</xsl:stylesheet>

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>
</result>

Best Regards,
George
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top