我发现这个页面描述了Muenchian方法,但我认为我错误地应用了它.
考虑到这将返回一组年龄:
/doc/class/person/descriptive[(@name='age')]/value
Run Code Online (Sandbox Code Playgroud)
1..2..2..2..3..3..4..7
但我希望每个年龄段的节点集只有一个节点.
1..2..3..4..7
这些中的每一个似乎都返回所有值,而不是唯一值:
/doc/class/person/descriptive[(@name='age')][not(value=preceding-sibling::value)]/value
/doc/class/person/descriptive[(@name='age')]/value[not(value=preceding-sibling::value)]
Run Code Online (Sandbox Code Playgroud)
我错过了什么?
BQ.*_*BQ. 23
这是一个例子:
<root>
<item type='test'>A</item>
<item type='test'>B</item>
<item type='test'>C</item>
<item type='test'>A</item>
<item type='other'>A</item>
<item type='test'>B</item>
<item type='other'>D</item>
<item type=''>A</item>
</root>
Run Code Online (Sandbox Code Playgroud)
和XPath:
//preceding::item/preceding::item[not(.=preceding-sibling::item)]/text()
Run Code Online (Sandbox Code Playgroud)
结果:ABCD
编辑:正如mousio评论的那样,如果它出现的唯一时间,则不会捕获列表中的最后一项.考虑到这一点和Fëanor的评论,这是一个更好的解决方案:
/root/item[not(.=preceding-sibling::item)]
Run Code Online (Sandbox Code Playgroud)
Chu*_*ckB 14
以下是使用他的数据的BQ答案的Muenchian版本:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" method="text"/>
<xsl:key name="item-by-value" match="item" use="."/>
<xsl:template match="/">
<xsl:apply-templates select="/root/item"/>
</xsl:template>
<xsl:template match="item">
<xsl:if test="generate-id() = generate-id(key('item-by-value', normalize-space(.)))">
<xsl:value-of select="."/>
<xsl:text>
</xsl:text>
</xsl:if>
</xsl:template>
<xsl:template match="text()">
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
这种转变给出了
A
B
C
D
key()上面的模板中的查找item返回一个节点集,其中包含item与上下文节点具有相同字符串值的所有元素. generate-id()保证所有调用在单次传递文档期间为给定节点生成相同的ID. key()调用返回的第一个节点相同,则测试将为true .小智 5
对于那些仍在 XSLT 中寻找不同选择的人:
使用 XSLT 2.0,您可以使用“distinct-values(/doc/class/person/descriptive[(@name='age')]/value)”
| 归档时间: |
|
| 查看次数: |
48462 次 |
| 最近记录: |