首页 > 解决方案 > 如何从键函数(XSLT)中选择要返回的元素

问题描述

我的 XML 文件如下所示:

<Geometry>
   <Shapes>
    <Shape id="b1" author-id="a1">
        <width>First</width>
        <height>Shape</height>
        <color>Color</color>
     <Shape id="b2" author-id="a2">
        <width>Second</width>
        <height>Shape</height>
        <color>Color</color>
    </Shape>
</Shapes>
<Authors>
    <Author id="b1">
        <name>Andrew</name>
        <secondname>Coldwater</secondname>
        <tel>978-3-16-148410-0</tel>
    </Author>
    <Author id="b2">
        <name>Andrew</name>
        <secondname>Coolwater</secondname>
        <tel>9781-140-201</tel>
    </Author>
</Author>

当我使用

<xsl:key name="exampleName" match="Author" use="@id"/> 

  <xsl:apply-templates select="key('exampleName', @author-id)" />

在返回时,我拥有作者的所有属性。我需要做的是只接收名字和第二个名字。也许这是一个微不足道的问题,但我找不到答案......或者我不知道如何问:)

标签: xmlxsltkeyfunc

解决方案


使用模板匹配Author来自<xsl:apply-templates select="key('exampleName', @author-id)" />.

<xsl:template match="Author">
    <xsl:value-of select="name" />
    <xsl:value-of select="secondname" />
</xsl:template> 

推荐阅读