Joe Essey
Joe Essey

Reputation: 3536

How can I use XPath to resolve the following 'tags' values?

The xml file is :

<xml-fragment xmlns:xyz="http://someurl">
   <xyz:xyzcontent>
     <contentattribute>
       <key>tags</key>
       <value>tag1, tag2</value>
     </contentattribute>
   </xyz:xyzcontent>
...

I've tried the following:

XPathExpression createdDateExpression = xpath.compile("/contentattribute/key/attribute::tags/value");

Upvotes: 0

Views: 88

Answers (1)

Jens Erat
Jens Erat

Reputation: 38732

There are several problems with your query.

  • The XML is broken (root tag not closed) -- probably just a copy/paste mistake
  • You're starting somewhere right in the middle of the XML tree, but actually try to query from the root node. Use the descendant-or-self-axis // in the beginning.
  • Which attribute are you querying using the attribute-axis? There is none.
  • Where did you register the namespaces? What namespace is xyz, anyway? I guess it's actually vp, but you obfuscated incompletely (or are not giving all relevant parts of the document).
  • Use predicates and string comparison to filter at axis steps.

Try following:

  • Make sure to register the namespace, have a look at the reference for that (or give more information).
  • Use the XPath query //contentattribute[key='tags']/value

Upvotes: 1

Related Questions