使用 XPath 按包含空格的值定位节点 [英] Locating the node by value containing whitespaces using XPath

查看:64
本文介绍了使用 XPath 按包含空格的值定位节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 XPath 通过其值来定位 xml 文件中的节点.当要查找的节点包含内部有空格的值时,就会出现问题.例如:

I need to locate the node within an xml file by its value using XPath. The problem araises when the node to find contains value with whitespaces inside. F.e.:

<Root>
  <Child>value</Child>
  <Child>value with spaces</Child>
</Root>

我无法构建定位第二个子节点的 XPath.

I can not construct the XPath locating the second Child node.

简单的 XPath/Root/Child 完美适用于两个孩子,但/Root[Child=value with space] 返回一个空集合.

Simple XPath /Root/Child perfectly works for both children, but /Root[Child=value with spaces] returns an empty collection.

我已经尝试过使用 %20& 来屏蔽空格.#20;, &nbsp; 并使用引号和双引号.

I have already tried masking spaces with %20, & #20;, & nbsp; and using quotes and double quotes.

仍然没有运气.

有人有想法吗?

推荐答案

根据您的具体情况,有不同的 XPath 表达式将选择节点,其值包含一些空格.

首先,让我们回忆一下这些字符中的任何一个都是空白":

First, let us recall that any one of these characters is "whitespace":

   &#x09; -- 标签

    &#x09; -- the Tab

   &#xA; -- 换行

    &#xA; -- newline

   &#xD; -- 回车

    &#xD; -- carriage return

   ' '&#x20; --空间

    ' ' or &#x20; -- the space

如果你知道节点的确切值,说它是带有空格的Hello World",那么最直接的XPath表达式:

If you know the exact value of the node, say it is "Hello World" with a space, then a most direct XPath expression:

   <代码>/top/aChild[.= 'Hello World']

     /top/aChild[. = 'Hello World']

将选择这个节点.

然而,指定包含空格的值的困难来自这样一个事实,即我们看到所有空格字符都一样......好吧,空格并且不知道它是否是一个一组空格或单个制表符.

The difficulties with specifying a value that contains whitespace, however, come from the fact that we see all whitespace characters just as ... well, whitespace and don't know if a it is a group of spaces or a single tab.

XPath 2.0 中可以使用 正则表达式,它们提供了一个简单方便的解决方案.因此,我们可以使用 XPath 2.0 表达式,如下所示:

In XPath 2.0 one may use regular expressions and they provide a simple and convenient solution. Thus we can use an XPath 2.0 expression as the one below:

   /*/aChild[matches(., "Hello\sWorld")]

    /*/aChild[matches(., "Hello\sWorld")]

选择顶部节点的任何子节点,其值为字符串Hello"后跟空格后跟字符串World".注意的使用match() 函数和匹配空格的\s"模式.

to select any child of the top node, whose value is the string "Hello" followed by whitespace followed by the string "World". Note the use of the matches() function and of the "\s" pattern that matches whitespace.

XPath 1.0 中方便测试给定字符串是否包含任何空白字符是:

In XPath 1.0 a convenient test if a given string contains any whitespace characters is:

not(string-length(.)= stringlength(translate(., ' &#9;&#xA;&#xD;','')))

这里我们使用 translate() 函数消除四个空白字符中的任何一个,并将结果字符串的长度与原始字符串的长度进行比较.

Here we use the translate() function to eliminate any of the four whitespace characters, and compare the length of the resulting string to that of the original string.

因此,如果在文本编辑器中节点的值显示为

So, if in a text editor a node's value is displayed as

你好   世界",

"Hello    World",

我们可以用 XPath 表达式安全地选择这个节点:

we can safely select this node with the XPath expression:

/*/aChild[translate(., ' &#9;&#xA;&#xD;','') = 'HelloWorld']

在很多情况下我们也可以使用XPath函数normalize-space(),它从它的字符串参数产生另一个字符串,其中前导和尾随空格组被切割,并且字符串中的每个空格都被一个空格替换.

In many cases we can also use the XPath function normalize-space(), which from its string argument produces another string in which the groups of leading and trailing whitespace is cut, and every whitespace within the string is replaced by a single space.

在上述情况下,我们将简单地使用以下 XPath 表达式:

In the above case, we will simply use the following XPath expression:

/*/aChild[normalize-space() = 'Hello World']

这篇关于使用 XPath 按包含空格的值定位节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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