سؤال

Considering the following XML file:

<a>
   <b>
   </b>
   <c>
      <d>content X</d>
   </c>
   <c>
      <d>content Y</d>
   </c>
</a>

is there any easy way to parse the XML to make each element have its xpath expression, so I when I do something like curl http://localhost:5000/home/content?xpath=/a/c/d[2] it recognizes that I'm referring to the second <d> tag?

If there is no easy way, any suggestions on how to achieve this?

هل كانت مفيدة؟

المحلول

Yes, there is. But you actually don't want the second <d/> element, as there is none in the given context. If you apply the position filter (i.e. using [2]) it means you want the second <d/> element within the given <c/> element. But every <c/> only has one <d/>, so you will always yield an empty result.

Hence, the correct XPath would be:

curl http://localhost:5000/home/content?xpath=/a/c[2]/d
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top