5

Could I use XPath to select country node whose code containing UK?

<country-list>
  <Country code="TW,UK,MY" />
  <Country code="US,CA,MX" />
  <Country code="IN,PR,VI,IR" />
  <Country code="Others" /> 
</country-list>

Thanks.

1
  • Just curious why you accepted the answer when the answer didn't actually solve your problem? Commented Nov 24, 2009 at 3:21

4 Answers 4

13

Try the contains() XPath function.

Something like:

/Country[fn:contains(@code, "UK")]

A quick Google search turns up details on XPath functions: http://www.w3schools.com/xpath/xpath_functions.asp

Sign up to request clarification or add additional context in comments.

3 Comments

Hello, I meet this error: [System.Xml.XPath.XPathException] = {"Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function."} How could I get around it?
It's the 'fn:' namespace prefix. Try removing that.
3

You need write it this way:

/country-list/Country[contains(@code,'UK')]

Comments

1

You could use Linq to XML - just as an idea

Something like this:

var countryElement = from country in countryElement.GetAttribute("code")
  where country.Value.Contains("UK")
  select countryElement;

Comments

0

Yes, do something like

//Country[contains(@code, 'UK')]

which selected the first Country element

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.