Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

problem getting xpath function ends-with() to work while contains() works fine

Tags:

i am trying to get the tags that have an attribute that end with a certain id.
like <span id="ctl00_ContentPlaceHolder1_Country">
i want to get the spans that have the id ends with 'Country'
i try the following xpath //span[ends-with(@id,'Country')] but i get the following exception
"Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function."

the strange thing is that contains works fine so the following xpath works //span[contains(@id,'Country')]
any ideas why this happens? thanks

like image 862
Karim Avatar asked Aug 12 '10 15:08

Karim


People also ask

How do you write the end of XPath?

XPath Ends-with. Using “OR” Statement. Using “AND” Statement.

What are valid XPath functions?

About XPath Functions. Node Set Functions. number last() number position() number count(node-set)

Does browser engine support xpath2 0 function like ends with?

XPath 2.0 is fully included as a subset in XQuery 1.0, so you will be able to use all XPath 2.0 features (and more) in all browsers with JavaScript support.


2 Answers

The function ends-with() is not defined for XPath 1.0 but only for XPath 2.0 and XQuery.

You are using .NET. .NET at this date does not implement XPath 2.0, XSLT 2.0 or XQuery.

One can easily construct an XPath 1.0 expression, the evaluation of which produces the same result as the function ends-with():

$str2 = substring($str1, string-length($str1)- string-length($str2) +1)

produces the same boolean result (true() or false()) as:

ends-with($str1, $str2)

In your concrete case you just need to substitute the right expressions for $str1 and $str2. They are, accordingly, /myXml/data and 'World'.

So, the XPath 1.0 expression to use, that is equivalent to the XPath 2.0 expression ends-with(/myXml/data, 'World') is:

'World' =     substring(/myXml/data,              string-length(/myXml/data) - string-length('World') +1              ) 
like image 168
Dimitre Novatchev Avatar answered Dec 08 '22 09:12

Dimitre Novatchev


contains() and starts-with() are in XSLT1; ends-with() is in XSLT2 only.

like image 23
Jim Garrison Avatar answered Dec 08 '22 10:12

Jim Garrison