-1

For analyzing some data, I am currently trying to select an attribute in XQuery. Unfortunately, for some reason I can't select it.

Considering the example document

<myroot>
 <betweennode>
  <asd name="test">
   <asdasd/>
  </asd>
 </betweennode>
</myroot>

the XPath /myroot/betweennode/asd/@name regulary returns test, the XQuery

for $x in /myroot/betweennode/asd
return $x

returns

<asd name="test">
   <asdasd/>
</asd>

but the XQuery

for $x in /myroot/betweennode/asd
return $x/@name

does not work (it returns 'ERROR - Cannot create an attribute node (name) whose parent is a document node' in http://www.xpathtester.com/xquery and '[SENR0001] Attributes cannot be serialized:attribute name {"testInitialize"}.' when I use BaseX and doc('mydoc.xml')/myroot/betweennode/asd in the for-statement).

Could anyone give me a hint why this doesn't work, and how selecting all attributes works in XQuery?

David Georg Reichelt
  • 963
  • 1
  • 15
  • 36

1 Answers1

3

You can't return attribute in XQuery. If you meant to return value of the attribute, try this way :

for $x in /myroot/betweennode/asd
return data($x/@name)
har07
  • 88,338
  • 12
  • 84
  • 137