BMitrano825
BMitrano825

Reputation: 123

Need XPath ends-with function

I am having trouble getting an XPath function to work. I am supposed to pull only the entries in the XML file that the name of them ends with the letter 'z' and I cannot get it to work. I am xmllint to manipulate the file with the cat command.

ends-with() only works on XPath 2.0 and I'm pretty sure the server only has XPath 1.0 so I have tried entering this command:

cat //country/city[substring(@name, string-length(@name) - string-length('z') +1)]/name

but I keep getting an error, and I am not sure if I am writing it improperly or if it is not the correct way to do it.

Upvotes: 5

Views: 986

Answers (2)

har07
har07

Reputation: 89325

Your XPath is missing the step of checking return value of substring() whether or not it is equals to the expected string ending value "z". So the correct XPath would be :

//country[substring(@name, string-length(@name) - string-length('z') +1) = 'z']/@name

And since -string-length('z') +1 equals to 0, that part can be omitted to get a simpler XPath, similar to the one posted by @Kirill Polishchuk in the other answer.

Upvotes: 3

Kirill Polishchuk
Kirill Polishchuk

Reputation: 56212

This should work:

//country[substring(@name, string-length(@name)) = 'z']

Upvotes: 3

Related Questions