SeyoS
SeyoS

Reputation: 681

Get all attributes with the same name

I'm using XDocument and I need to parse my XML file to retrieve all attribute with the same name event if its node's name is different from the other. For example, for this XML :

<document>
    <person name='jame'/>
    <animals>
          <dog name='robert'/>
    </animals>
</document>

I want to retrieve all attributes named 'name'.

Can I do that with one request XPath or do I need to parse every node to find thos attributes ?

Thanks for your help !

Upvotes: 1

Views: 406

Answers (1)

LarsH
LarsH

Reputation: 28004

The XPath expression

//@name

will select all attributes called name, regardless of where they appear.

By the way, 'parsing' is something that happens to the XML document before XPath ever enters the picture. So when you say "do I need to parse every node", I think this isn't really what you mean. The entire document is typically already parsed before you run an XPath query. However, I'm not sure what you do mean instead of 'parse'. Probably something like "do I need to visit every element" to find those attributes? In which case the answer is no, unless in some vague implementation-dependent sense that doesn't make any difference to you.

Upvotes: 3

Related Questions