xml - 试图理解多个谓词
问题描述
我遇到了http://xpather.com,它可以让您编写 XPath 表达式。它带有一个与此类似的示例 XML 文档:
<app>
<abstract>a</abstract>
<description>
<subject>s1</subject>
<subject>s2</subject>
</description>
<extra-notes>
<note>n1</note>
<note>n2</note>
<note>n3</note>
<note>n4</note>
</extra-notes>
</app>
并有一个示例 XPath 表达式:.//*[self::abstract or self::subject or self::note][position() <= 2]
结果序列为
<abstract>a</abstract>
<subject>s1</subject>
<subject>s2</subject>
<note>n1</note>
<note>n2</note>
现在我试图理解为什么结果是这样的:第一个谓词选择所有抽象、主题和注释元素,第二个谓词将其限制为position() <= 2
. 我的直觉期望是 XPath 表达式只选择两个节点(abstract 和 subject1),但它实际上从每个选定节点中选择前两个元素。到底发生了什么?
仅用于测试:这是我的简单样式表:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output indent="yes"/>
<xsl:template match="/">
<e>
<xsl:copy-of select=".//*[self::abstract or self::subject or self::note][position() <= 2]"/>
</e>
</xsl:template>
</xsl:stylesheet>
解决方案
使用:
(//*[self::abstract or self::subject or self::note])[not(position() > 2)]
有关解释,请参阅此答案:
请记住:
[]
运算符的优先级(优先级)高于//
缩写。
推荐阅读
- json - 将两个相同文件的json文件合二为一
- java - 如何在 Android 10 中创建自定义目录“/storage/emulated/0/TestDir”?
- html - 如何通过自定义 CSS 更改阿拉伯语 RTL 下拉子菜单的方向?
- deployment - 管理代理产品上的就绪探测失败
- python - 如果我有中点、角度和长度,则获取一条线的端点
- mongodb - 猫鼬集成问题(又名 insertMany)
- swiftui - 似乎无法在 text() 和 textfield() 之间使用 Spacer()
- python - 如何不在“for循环”中打印重复字符?
- browser - PuppeteerSharp 向本地主机请求?
- reactjs - redux 减速器类型“从不”