views:

2560

answers:

4

Hi there,

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

value value with spaces

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?

A: 

did you try #x20 ?

Scott Evernden
A: 

Try either this:

/Root/Child[normalize(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(text())=normalize(value with spaces)]

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

kdgregory
A: 

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

try to replace the space using "x0020"

this seems to work for the guy.

melaos
A: 

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

    	 -- the Tab

    
 -- newline

    
 -- carriage return

    ' ' or   -- 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(., ' 	

','')))

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(., ' 	

','') = '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']

Dimitre Novatchev