使用 XPath,如何根据节点的文本内容和属性值选择节点? [英] Using XPath, How do I select a node based on its text content and value of an attribute?

查看:36
本文介绍了使用 XPath,如何根据节点的文本内容和属性值选择节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于此 XML:

<DocText>
<WithQuads>
    <Page pageNumber="3">
        <Word>
            July
            <Quad>
                <P1 X="84" Y="711.25" />
                <P2 X="102.062" Y="711.25" />
                <P3 X="102.062" Y="723.658" />
                <P4 X="84.0" Y="723.658" />
            </Quad>
        </Word>
        <Word>
        </Word>
        <Word>
            30,
            <Quad>
                <P1 X="104.812" Y="711.25" />
                <P2 X="118.562" Y="711.25" />
                <P3 X="118.562" Y="723.658" />
                <P4 X="104.812" Y="723.658" />
            </Quad>
        </Word>
    </Page>
</WithQuads>

我想找到文本为July"且 Quad/P1/X 属性大于 90 的节点.因此,在这种情况下,它不应返回任何匹配项.但是,如果我使用 GT (>) 或 LT (<),我会在第一个 Word 元素上获得匹配项.如果我使用 eq (=),则没有匹配项.

I'd like to find the nodes that have text of 'July' and a Quad/P1/X attribute Greater than 90. Thus, in this case, it should not return any matches. However, if I use GT (>) or LT (<), I get a match on the first Word element. If I use eq (=), I get no match.

所以:

//Word[text()='July' and //P1[@X < 90]]

将返回真,也将

//Word[text()='July' and //P1[@X > 90]]

如何在 P1@X 属性上正确地限制它?

How do I constrain this properly on the P1@X attribute?

此外,假设我有多个 Page 元素,用于不同的页码.我将如何另外限制上述搜索以查找具有 text()='July', P1@X < 的节点?90 和 Page@pageNumber=3?

In addition, imagine I have multiple Page elements, for different page numbers. How would I additionally constrain the above search to find Nodes with text()='July', P1@X < 90, and Page@pageNumber=3?

推荐答案

一般来说,我会认为在 XPath 中使用不带前缀的//是一种糟糕的气味.

Generally I would consider the use of an unprefixed // as a bad smell in an XPath.

试试这个:-

/DocText/WithQuads/Page/Word[text()='July' and Quad/P1/@X > 90]

你的问题是你使用了 //P1[@X <90] 它从文档的开头开始并开始搜索任何 P1 因此它总是正确的.同样//P1[@X>90] 总是正确的.

Your problem is that you use the //P1[@X < 90] which starts back at the beginning of the document and starts hunting any P1 hence it will always be true. Similarly //P1[@X > 90] is always true.

这篇关于使用 XPath,如何根据节点的文本内容和属性值选择节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆