XML I:(例如购物车、图片库)
<list1>
<entry>
...
<items>
<item id="1"></item>
<item id="2"></item>
<item id="3"></item>
</items>
</entry>
</list1>
XML II:(=关联项)
<list2>
<entry id="1">
...
<visibility>
<item value="public">Public</item>
</visibility>
</entry>
<entry id="3">
...
<visibility>
<item value="private">Private</item>
</visibility>
</entry>
<entry id="5">
...
<visibility>
<item value="public">Public</item>
</visibility>
</entry>
</list2>
注意:列表2中的ID可能匹配或不匹配。。。
如何获取list1中ID为list2且/visibility/item/@value='public'的项的计数?
XSLT1.0可以使用count(/list1/entry/item[@id = document('xml2.xml')/list2/entry[visibility/item/@value = 'public']/@id])
。
XSLT2.0是关键
<xsl:template name="k1" match="list2/entry[visibility/item/@value = 'public']" use="@id"/>
然后
count(/list1/entry/item[key('k1', @id, document('xml2.xml'))])
使得查找更加高效。