我的 XML 代码(包含酒吧和人员):
<document>
<pubs>
<pub id="pub1">
<open>true</open>
</pub>
<pub id="pub2">
<open>false</open>
</pub>
</pubs>
<people>
<person>
<name>John</name>
<pubId>pub1</pubId>
</person>
<person>
<name>Paul</name>
<pubId>pub2</pubId>
</person>
</people>
</document>
使用来自<person>
to <pub>
( person/pubId
references pub[@id]
) 的引用。酒吧本身有一个布尔值<open>
(说明酒吧是打开还是关闭)。
使用 XSLT,我希望能够count()
有多少人可以去酒吧。为了去酒吧,酒吧的<open>
值必须为true
。
我无法完全编码的两种可能的解决方案:
遍历人员并跟踪人数:
<xsl:for-each select="person"> <xsl:variable name="pId" select="pubId" /> <xsl:if test="root()/document/pubs/pub[@pubId=$pId]/open = 'true'" > <!-- any way to keep track of this number? --> </xsl:if> </xsl:for-each>
直接使用 count() 函数:
<count> <xsl:value-of select="count(//person[root()/document/pubs/pub[@id = pubId]/open = 'true'])" /> </count>
第二个选项似乎更简洁和正确,但我无法让它工作。
有任何想法吗?