Xquery - Selecting value from XML using XMLtable
I want to use XQuery to display the value if FrequencyCdvalue="01"
and city type="first"
in the following XML.
Can you pls help me here
<Envelope>
<Arrangement>
<FrequencyCd value="01">first first</FrequencyCd>
<FrequencyCd value="02">first second</FrequencyCd>
<contactinfo> <Address>
<street>234 Rolling Lane</street>
<city type="first">Rockport</city>
</Address>
<email>love2fish@finmail.com</email>
</contactinfo>
&开发者_Go百科lt;/Arrangement>
<Arrangement>
<FrequencyCd value="03">second first</FrequencyCd>
<FrequencyCd value="04">second second</FrequencyCd>
<contactinfo>
<Address>
<street>234 Straight Lane</street>
<city type="first">Crackport</city>
</Address>
<email>hate2fish@finmail.com</email>
</contactinfo>
</Arrangement>
</Envelope>
This approach shreds more rows internally, but allows the compound filtering criteria (the conditions for FrequencyCd and City) to be implemented as a simple WHERE clause on the SQL representation of the shredded XML data (the output of the XMLTABLE function).
with origxml(xdoc) AS (VALUES XMLPARSE( DOCUMENT
'<Envelope>
<Arrangement>
<FrequencyCd value="01">first first</FrequencyCd>
<FrequencyCd value="02">first second</FrequencyCd>
<contactinfo> <Address>
<street>234 Rolling Lane</street>
<city type="first">Rockport</city>
</Address>
<email>love2fish@finmail.com</email>
</contactinfo>
</Arrangement>
<Arrangement>
<FrequencyCd value="03">second first</FrequencyCd>
<FrequencyCd value="04">second second</FrequencyCd>
<contactinfo>
<Address>
<street>234 Straight Lane</street>
<city type="first">Crackport</city>
</Address>
<email>hate2fish@finmail.com</email>
</contactinfo>
</Arrangement>
</Envelope>'
)
)
SELECT filteredxml.FrequencyCd, filteredxml.City FROM origxml,
XMLTABLE ('$d/Envelope/Arrangement' PASSING origxml.xdoc AS "d"
COLUMNS
FrequencyCd VARCHAR(20) PATH 'FrequencyCd[@value="01"]/text()',
City VARCHAR(30) PATH 'contactinfo/Address/city[@type="first"]/text()'
) as filteredxml
WHERE filteredxml.FrequencyCd IS NOT NULL
AND filteredxml.City IS NOT NULL
;
精彩评论