Getting value of tag referenced by id ref

212 Views Asked by At

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?

2

There are 2 best solutions below

0
On BEST ANSWER

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

0
On

Ok, I figured out how to do it.

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

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

and then

    <xsl:apply-templates select="wydawnictwo"/>