我有以下xml:
<Books>
<Book author="John" country="Norway"/>
<Book author="Paul" />
<Book author="Steve" country="England"/>
</Books>
<Books>
<Book author="George" />
<Book author="Thomas" country="Germany"/>
</Books>
我想在每个“书籍”元素中找到属性“国家”的位置。
<Books>
<Book author="John" country="Norway"/> --1
<Book author="Paul" />
<Book author="Steve" country="England"/> --2
<Book author="Bob" country="Denmark"/> --3
</Books>
<Books>
<Book author="George" />
<Book author="Thomas" country="Germany"/> --1
</Books>
我们可以为此使用哪个 XPath 函数?
最佳答案
您可以计算具有该属性的先前 sibling 的数量,这是一个模板,显示:
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="Books/Book[@country]">
<xsl:value-of select="count(preceding-sibling::Book[@country]) + 1"/>
</xsl:template>
</xsl:stylesheet>
关于xml - XSLT - 属性的出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13458279/