是否可以将父元素的属性与 XSLT 中选择 XPath 中的子元素的属性进行比较?



我想比较XPath选择表达式中的两个属性值。。。具体来说,我想比较type的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>

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

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

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

最新更新