限制 XPath 谓词:以以下开头的谓词

发布于 2024-09-25 21:45:21 字数 1249 浏览 6 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

往事随风而去 2024-10-02 21:45:21

从技术上讲,我需要找到第一个
包含一个后代::t
[ 开头,以 ] 结束。

/sheetData/row[c/is/t[starts-with(.,'[')]
                     [substring(.,string-length(.))=']']]
              [1]

/sheetData/row[.//t[starts-with(.,'[') and
                    substring(.,string-length(.))=']']][1]

(//row[.//t[starts-with(.,'[') and
            substring(.,string-length(.))=']']])[1]

Technically, I need to find the first
containing a descendant::t that
starts with [ and closes with ].

/sheetData/row[c/is/t[starts-with(.,'[')]
                     [substring(.,string-length(.))=']']]
              [1]

or

/sheetData/row[.//t[starts-with(.,'[') and
                    substring(.,string-length(.))=']']][1]

or

(//row[.//t[starts-with(.,'[') and
            substring(.,string-length(.))=']']])[1]
戏蝶舞 2024-10-02 21:45:21

一种选择:

.//row[starts-with(descendant::t/text(),'[') and substring(descendant::t/text(), string-length(descendant::t/text())) = ']' ]

这将为您提供行,但是一个重大问题可能是,如果您的 row 有两个满足不同条件的 t 元素,但不能同时满足这两个条件。例如一个t以[开头,另一个以]结尾

显然,你所拥有的没有这个问题

另一种选择:使用translate

.//row[translate(descendant::t/text(),"0123456789","") = "[]"]

这将去除数字字符,然后与[进行简单的比较] 人物

One option:

.//row[starts-with(descendant::t/text(),'[') and substring(descendant::t/text(), string-length(descendant::t/text())) = ']' ]

This will give you the row, however one significant problem could be if your row has two t elements that would satisfy different conditions, but not both conditions. e.g. one t starts with [, and another ends with ]

Obvsiously, what you have doesn't have this problem

Another option: use translate

.//row[translate(descendant::t/text(),"0123456789","") = "[]"]

That will strip the numeric characters and then it's a simple comparison to the [] characters

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文