我想比较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>