Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Locating the node by value containing whitespaces using XPath

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>

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

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

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

Still no luck.

Does anybody have an idea?

like image 872
user15108 Avatar asked Dec 26 '08 14:12

user15108


4 Answers

Locating the Attribute by value containing whitespaces using 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.

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

Hope this will help you guys.

like image 74
gihan-maduranga Avatar answered Nov 20 '22 00:11

gihan-maduranga


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

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

    &#x09; -- the Tab

    &#xA; -- newline

    &#xD; -- carriage return

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

     /top/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:

    /*/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.

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

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

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",

we can safely select this node with the XPath expression:

/*/aChild[translate(., ' &#9;&#xA;&#xD;','') = '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.

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

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

like image 34
Dimitre Novatchev Avatar answered Nov 19 '22 23:11

Dimitre Novatchev


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.

like image 10
kdgregory Avatar answered Nov 20 '22 00:11

kdgregory


"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"-

[(jcr:contains(jcr:content/@Record_x0020_ID, 'test'))]
like image 1
Manish Paul Avatar answered Nov 20 '22 00:11

Manish Paul