Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

newbie: xsl:key & generate-id = problem

 
 
iliad
Guest
Posts: n/a
 
      02-16-2006
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'/>

 
Reply With Quote
 
 
 
 
George Bina
Guest
Posts: n/a
 
      02-16-2006
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"
xmlnssl="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
---------------------------------------------------------------------
George Cristian Bina
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com

 
Reply With Quote
 
 
 
Reply

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



Advertisments