开发者

Limiting an XPath predicate: predicate starting 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开发者_如何学JAVA 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.


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]


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜