Search code examples
xmlxslttransformationidref

Getting value of tag referenced by id ref


I'm trying to get a value of a tag of an element referenced by idref.

<ksiazka id="k2">
    <title> title 1 </title>
    <wydawnictwo idref="wyd1"/>
</ksiazka>

<wyd id="wyd1">
    <name>Zielona Sowa</name>
</wyd>

To get title of < ksiazka > all I have to do is

<xsl:template match="ksiazka">
    <xsl:value-of select "./title"/>

But how to get < name > out of < wyd >? Any suggestions?


Solution

  • XSLT has a build-in mechanism to resolve cross-references. Start by defining a key at the top level of your stylesheet as:

    <xsl:key name="pub" match="wyd" use="@id" />
    

    Then you can use:

    <xsl:value-of select="key('pub', wydawnictwo/@idref)/name"/>
    

    to get the corresponding wyd/name from the context of ksiazka.


    See a demo here: http://xsltransform.net/94AbWBE