Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select an attribute using nodesForXPath (NSXMLDocument)

I'd like to select the attribute from an XML element within an XML document using XPath.

My XML document is an instance of NSXMLDocument. Here is an example of the XML Document:

<rootnode>
  <mynode myattrib="getMe"></mynode>
</rootnode>

My XPath is something like:

//mynode@myattrib

This should return the value "getMe" (according to: http://www.bit-101.com/xpath/ ).

When I try to do this using:

[xmlDoc nodesForXpath:@"//mynode@myattrib" error:&error];

I get the following in error:

NSLog(@"%@",error);

Output:

XQueryError:3 - "invalid token (@) - //mynode@myattrib" at line:1

What should I change to get this to work? Is the @ symbol used in some other way?

like image 432
Vamos Avatar asked Dec 18 '25 13:12

Vamos


1 Answers

Add a slash before the @, as in:

//mynode/@myattrib
like image 72
Sherm Pendley Avatar answered Dec 21 '25 05:12

Sherm Pendley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!