How to reference an XML attribute using XPath?

My XML:

<root>
  <cars>
    <makes>
      <honda year="1995">
        <model />
        <!-- ... -->
      </honda>
      <honda year="2000">
        <!-- ... -->
      </honda>
    </makes>
  </cars>
</root>

I need XPath, which will get me all the models for <honda>since 1995.

So:

/ root / cars / makes / honda

But how to refer to an attribute?

+3
source share
2 answers

Try /root/cars/makes/honda/@year

UPDATE : read your question again:

   /root/cars/makes/honda[@year = '1995']

Bottom line: use the @ symbol to refer to xml attributes.

+6
source

"I need XPath, which will give me all the models for <honda>since 1995."

This will:

/ root / cars / makes / honda [@year = '1995'] / model
+7
source

Source: https://habr.com/ru/post/1719472/


All Articles