Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
201 views
in Technique[技术] by (71.8m points)

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
             )

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...