限制 XPath 谓词:以以下开头的谓词
我正在使用 XPath 1.0 (摘录)浏览这个办公室打开的 xml 文件:
<sheetData ref="A1:XFD108">
<row spans="1:3" r="1">
<c t="s" r="A1">
<is>
<t>FirstCell</t>
</is>
</c>
<c t="s" r="C1">
<is>
<t>SecondCell</t>
</is>
</c>
</row>
<row spans="1:3" r="2">
<c t="s" r="A2">
<is>
<t>ThirdCell</t>
</is>
</c>
<c t="s" r="C2">
<is>
<t>[persons.ID]</t>
</is>
</c>
</row>
</sheetData>
我需要找到显示“[persons.ID]”的单元格,它是一个变量。从技术上讲,我需要找到第一个包含以 [
开头并以 ] 结束的
。我目前有:descendant::t
的
.//row//t[starts-with(text(), '[') and
substring(text(), string-length(text())) = ']']/ancestor::row
所以我过滤然后再次上升。它有效,但我想在这里更好地理解 XPath - 我发现无法过滤谓词。您能否向我指出类似于 .//row[descendant::t[starts-with()...]]
之类的有效等效操作。
非常感谢任何帮助。
I am navigating this office open xml file using XPath 1.0 (extract):
<sheetData ref="A1:XFD108">
<row spans="1:3" r="1">
<c t="s" r="A1">
<is>
<t>FirstCell</t>
</is>
</c>
<c t="s" r="C1">
<is>
<t>SecondCell</t>
</is>
</c>
</row>
<row spans="1:3" r="2">
<c t="s" r="A2">
<is>
<t>ThirdCell</t>
</is>
</c>
<c t="s" r="C2">
<is>
<t>[persons.ID]</t>
</is>
</c>
</row>
</sheetData>
I need to find the cell that says "[persons.ID]", which is a variable. Technically, I need to find the first <row>
containing a descendant::t
that starts with [
and closes with ]
. I currently have:
.//row//t[starts-with(text(), '[') and
substring(text(), string-length(text())) = ']']/ancestor::row
So I filter and then go up again. It works, but I'd like to understand XPath better here - I found no way filter the predicate. Can you point me to a valid equivalent of doing something like .//row[descendant::t[starts-with()...]]
.
Any help is greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
或
或
or
or
一种选择:
这将为您提供行,但是一个重大问题可能是,如果您的
row
有两个满足不同条件的t
元素,但不能同时满足这两个条件。例如一个t
以[开头,另一个以]结尾显然,你所拥有的没有这个问题
另一种选择:使用translate
这将去除数字字符,然后与[进行简单的比较] 人物
One option:
This will give you the row, however one significant problem could be if your
row
has twot
elements that would satisfy different conditions, but not both conditions. e.g. onet
starts with [, and another ends with ]Obvsiously, what you have doesn't have this problem
Another option: use translate
That will strip the numeric characters and then it's a simple comparison to the [] characters