简体   繁体   中英

DB2 Selecting a specific XML content with XMLQUERY

This is a sample XML. In this XML i want to select the name "Marc Intes" but i am having problems with it.

<Person Name="Marc Intes">

Here is how i did my query, this XML document is under the column named GUEST.

XMLQUERY('$GUEST/Person@Name')

But it is giving me an error saying:

SQL16002N  An XQuery expression has an unexpected token "@" following 
"ST/Person". Expected tokens may include: "". Error QName=err:XPST0003.  
SQLSTATE=10505

Where did i go wrong? I am really confused right now.

There are two problems with your query. Firstly, XML attributes are also an XPath dimension, so the attribute reference should be separated by a slash:

XMLQUERY('$GUEST/Person/@Name')

Secondly, XMLQUERY is supposed to return an XML sequence, and a standalone attribute cannot be made into a sequence, so you need to cast it to an SQL type instead:

XMLCAST ( XMLQUERY('$GUEST/Person/@Name') AS VARCHAR(20) )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM