获取 id ref 引用的标签的值

Getting value of tag referenced by id ref

我正在尝试获取 idref 引用的元素的标签值。

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

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

要获得的头衔,我所要做的就是

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

但是如何让脱离呢?有什么建议吗?

XSLT 有一个 build-in mechanism 来解析 cross-references。首先在样式表的顶层定义一个 key 作为:

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

那么你可以使用:

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

ksiazka的上下文中获取对应的wyd/name


在此处查看演示:http://xsltransform.net/94AbWBE

好的,我知道怎么做了。

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

然后

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