XQuery unable to select attribute

本小妞迷上赌 提交于 2020-01-07 08:54:10

问题


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?


回答1:


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)


来源:https://stackoverflow.com/questions/25308742/xquery-unable-to-select-attribute

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!