Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to select attribute value of a node in XQuery?

Tags:

xml

xquery

In below XML:

<company>
    <customers>
    <customer cno="2222">
            <cname>Charles</cname>
            <street>123 Main St.</street>
            <city>Wichita</city>
            <zip>67226</zip>
            <phone>316-636-5555</phone>
        </customer>
        <customer cno="1000">
            <cname>Bismita</cname>
            <street>Ashford Dunwoody</street>
            <city>Wichita</city>
            <zip>67226-1555</zip>
            <phone>000-000-0000</phone>
        </customer>     
    </customers>
</company>

I need to get the customer's no which is an attribute. In XPath I know it is /company/customers/customer/@cno, in XQuery I've tried below expression but didn't work for me:

for $c in /company/customers/customer
return $c/@cno
like image 586
Itz.Irshad Avatar asked Apr 17 '13 01:04

Itz.Irshad


People also ask

What is a node XQuery?

Nodes. In XQuery, there are seven kinds of nodes: element, attribute, text, namespace, processing-instruction, comment, and document (root) nodes. XML documents are treated as trees of nodes. The root of the tree is called the document node (or root node).

IS NOT NULL in XQuery?

XQuery doesn't have null , so if you are asking what to return to indicate null , then you would want to return an empty sequence () instead of null .


2 Answers

You should use data to pick attribute value:-

for $c in /company/customers/customer return data($c/@cno) 
like image 88
Navin Rawat Avatar answered Sep 21 '22 12:09

Navin Rawat


You can also use string to get attribute value:

for $c in /company/customers/customer     return $c/@cno/string() 
like image 21
Nikunj Vekariya Avatar answered Sep 21 '22 12:09

Nikunj Vekariya