使用XPath按包含空格的值定位节点

时间:2021-01-29 22:17:44

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.:

我需要使用XPath通过其值在xml文件中找到节点。当要查找的节点包含内部有空格的值时,问题就会出现问题。 F.e:

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

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

我无法构造定位第二个Child节点的XPath。

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

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

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

我已经尝试使用%20屏蔽空格,, 并使用引号和双引号。

Still no luck.

仍然没有运气。

Does anybody have an idea?

有人有想法吗?

7 个解决方案

#1


Depending on your exact situation, there are different XPath expressions that will select the node, whose value contains some whitespace.

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

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

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

    &#x09; -- the Tab

- 标签

    &#xA; -- newline

- 新队

    &#xD; -- carriage return

&#的xD; - 回车

    ' ' or &#x20; -- the space

''或 - 空间

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

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

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

/顶/ aChild [。 ='Hello World']

will select this node.

将选择此节点。

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.

但是,指定包含空格的值的困难来自于我们将所有空白字符视为......好,空白并且不知道它是一组空格还是单个选项卡。

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:

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

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

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.

选择*节点的任何子节点,其值为字符串“Hello”,后跟空格,后跟字符串“World”。注意使用matches()函数和匹配空格的“\ s”模式。

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

在XPath 1.0中,如果给定的字符串包含任何空格字符,则方便的测试是:

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

not(string-length(。)= stringlength(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.

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

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

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

"Hello    World",

we can safely select this node with the XPath expression:

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

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

/ * / aChild [translate(。,' ','')='HelloWorld']

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函数normalize-space(),它从其字符串参数生成另一个字符串,其中前导和尾部空格的组被剪切,并且字符串中的每个空格都被单个空格替换。

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

在上面的例子中,我们将简单地使用以下XPath表达式:

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

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

#2


Try either this:

试试这个:

/Root/Child[normalize-space(text())=value without spaces]

or

/Root/Child[contains(text(),value without spaces)]

or (since it looks like your test value may be the issue)

或(因为看起来您的测试值可能是问题)

/Root/Child[normalize-space(text())=normalize-space(value with spaces)]

Haven't actually executed any of these so the syntax may be wonky.

实际上没有执行任何这些语法可能会很糟糕。

#3


Locating the Attribute by value containing whitespaces using XPath

使用包含空格的值使用XPath定位属性

I have a input type element with value containing white space.

我有一个输入类型元素,其值包含空格。

eg:

<input type="button"  value="Import&nbsp;Selected&nbsp;File">

I solved this by using this xpath expression.

我通过使用此xpath表达式解决了这个问题。

//input[contains(@value,'Import') and contains(@value ,'Selected')and contains(@value ,'File')]

Hope this will help you guys.

希望这会帮助你们。

#4


did you try #x20 ?

你试过#x20吗?

#5


i've googled this up like on the second link:

我在第二个链接上搜索了这个:

try to replace the space using "x0020"

尝试使用“x0020”替换空间

this seems to work for the guy.

这似乎适合这个家伙。

#6


"x0020" worked for me on a jackrabbit based CQ5/AEM repository in which the property names had spaces. Below would work for a property "Record ID"-

“x0020”在基于长耳兔的CQ5 / AEM存储库中为我工作,其中属性名称具有空格。以下将适用于“记录ID”属性 -

[(jcr:contains(jcr:content/@Record_x0020_ID, 'test'))]

#7


All of the above solutions didn't really work for me. However, there's a much simpler solution.

所有上述解决方案对我来说都不起作用。但是,有一个更简单的解决方案。

When you create the XMLDocument, make sure you set PreserveWhiteSpace property to true;

创建XMLDocument时,请确保将PreserveWhiteSpace属性设置为true;

        XmlDocument xmldoc = new XmlDocument();
        xmldoc.PreserveWhitespace = true;
        xmldoc.Load(xmlCollection);

#1


Depending on your exact situation, there are different XPath expressions that will select the node, whose value contains some whitespace.

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

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

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

    &#x09; -- the Tab

- 标签

    &#xA; -- newline

- 新队

    &#xD; -- carriage return

&#的xD; - 回车

    ' ' or &#x20; -- the space

''或 - 空间

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

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

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

/顶/ aChild [。 ='Hello World']

will select this node.

将选择此节点。

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.

但是,指定包含空格的值的困难来自于我们将所有空白字符视为......好,空白并且不知道它是一组空格还是单个选项卡。

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:

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

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

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.

选择*节点的任何子节点,其值为字符串“Hello”,后跟空格,后跟字符串“World”。注意使用matches()函数和匹配空格的“\ s”模式。

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

在XPath 1.0中,如果给定的字符串包含任何空格字符,则方便的测试是:

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

not(string-length(。)= stringlength(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.

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

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

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

"Hello    World",

we can safely select this node with the XPath expression:

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

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

/ * / aChild [translate(。,' ','')='HelloWorld']

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函数normalize-space(),它从其字符串参数生成另一个字符串,其中前导和尾部空格的组被剪切,并且字符串中的每个空格都被单个空格替换。

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

在上面的例子中,我们将简单地使用以下XPath表达式:

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

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

#2


Try either this:

试试这个:

/Root/Child[normalize-space(text())=value without spaces]

or

/Root/Child[contains(text(),value without spaces)]

or (since it looks like your test value may be the issue)

或(因为看起来您的测试值可能是问题)

/Root/Child[normalize-space(text())=normalize-space(value with spaces)]

Haven't actually executed any of these so the syntax may be wonky.

实际上没有执行任何这些语法可能会很糟糕。

#3


Locating the Attribute by value containing whitespaces using XPath

使用包含空格的值使用XPath定位属性

I have a input type element with value containing white space.

我有一个输入类型元素,其值包含空格。

eg:

<input type="button"  value="Import&nbsp;Selected&nbsp;File">

I solved this by using this xpath expression.

我通过使用此xpath表达式解决了这个问题。

//input[contains(@value,'Import') and contains(@value ,'Selected')and contains(@value ,'File')]

Hope this will help you guys.

希望这会帮助你们。

#4


did you try #x20 ?

你试过#x20吗?

#5


i've googled this up like on the second link:

我在第二个链接上搜索了这个:

try to replace the space using "x0020"

尝试使用“x0020”替换空间

this seems to work for the guy.

这似乎适合这个家伙。

#6


"x0020" worked for me on a jackrabbit based CQ5/AEM repository in which the property names had spaces. Below would work for a property "Record ID"-

“x0020”在基于长耳兔的CQ5 / AEM存储库中为我工作,其中属性名称具有空格。以下将适用于“记录ID”属性 -

[(jcr:contains(jcr:content/@Record_x0020_ID, 'test'))]

#7


All of the above solutions didn't really work for me. However, there's a much simpler solution.

所有上述解决方案对我来说都不起作用。但是,有一个更简单的解决方案。

When you create the XMLDocument, make sure you set PreserveWhiteSpace property to true;

创建XMLDocument时,请确保将PreserveWhiteSpace属性设置为true;

        XmlDocument xmldoc = new XmlDocument();
        xmldoc.PreserveWhitespace = true;
        xmldoc.Load(xmlCollection);