Marc Intes
Marc Intes

Reputation: 737

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.

Upvotes: 1

Views: 13569

Answers (1)

mustaccio
mustaccio

Reputation: 18955

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) )

Upvotes: 4

Related Questions