是否可以在XSLT中将父元素的属性与子节点内的select XPath进行比较?

时间:2016-04-09 18:32:52

标签: xml xslt xpath

我想比较我的XPath选择表达式中的两个属性值...具体来说,我想将类型的tid属性与person的category属性进行比较。有可能使它有效吗? :)

<xsl:template match="/people/type">
    <div class="type">
        <h3>
            <a name="./{@tid}"><xsl:value-of select="./title"/></a>
        </h3>
        <ul>
            <lh>Persons with this type:</lh>
                <xsl:apply-templates select="../employees/person[@category=@tid]"/> <!-- here I would like to pass attribute tid of an type element -->
        </ul>
    </div>
</xsl:template>

1 个答案:

答案 0 :(得分:1)

是的,如果您考虑表达的上下文,这很容易实现。

表达式的上下文是由/people/type创建的<xsl:template match="/people/type">

如果您尝试应用由/employees/person[@category...]创建的其他上下文<xsl:apply-templates select="../employees/person[@category=@tid]"/>,则您有两个上下文。 @category@tid的背景不同。

解决方案很简单。只需在xsl:variable中修复一个上下文:

<xsl:template match="/people/type">
  <xsl:variable name="typeID" select="@tid" />  <!-- fixing @tid of '/people/type' to $typeID -->
    ...
    <xsl:apply-templates select="/employees/person[@category=$typeID]"/>    <!-- using $typeID -->
    ...
</xsl:template>
相关问题