newbie: xsl:key & generate-id = problem

Discussion in 'XML' started by iliad, Feb 16, 2006.

  1. iliad

    iliad Guest

    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'/>
    iliad, Feb 16, 2006
    #1
    1. Advertising

  2. iliad

    George Bina Guest

    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
    ---------------------------------------------------------------------
    George Cristian Bina
    <oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
    http://www.oxygenxml.com
    George Bina, Feb 16, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    3,600
    A. Bolmarcich
    May 27, 2005
  2. Rob Smegma
    Replies:
    1
    Views:
    1,808
    shaun
    Sep 26, 2005
  3. Replies:
    2
    Views:
    9,265
    Martin Honnen
    Sep 5, 2006
  4. Wizfrog
    Replies:
    1
    Views:
    564
    Martin Honnen
    Jul 22, 2007
  5. M P
    Replies:
    1
    Views:
    466
Loading...

Share This Page