简体   繁体   中英

Selecting element from xml using xmlquery in Db2 (SQL/XML)

I have created table in Db2 like this:

create table xml_file(data xml not null)

This is the exact structure of xml:

<?xml version="1.0" encoding="UTF-8" ?>
     <student id="20140021">
          <name>Tom</name>
          <surname>Johnson</surname>
          <birth_date>"05/11/1995"</birth_date>
          <birth_place>"Miami"</birth_place>
          <points>9.45</points>
     </student>

I want to select id, name, surname, and points for all students whose names are Ben and birth places are Chicago.

I wrote something like this:

select xmlquery('$DATA/student/data(@id)') as ID,
       xmlquery('$DATA/student/name/text()') as NAME,
       xmlquery('$DATA/student/surname/text()') as SURNAME,
       xmlquery('$DATA/student/points/text()') as POINTS
from xml_file
where xmlexists('$DATA/student[birth_place = "Chicago"]')
and xmlexists('$DATA/student[name = "Ben"]');

All I got is this message: "FETCHED 0 RECORDS, 0 RECORDS SHOWN" (this is in IBM Data Studio).

Can someone tell me what did I do wrong?

Try this:

/*
WITH xml_file (data) AS 
(
VALUES
XMLPARSE
(DOCUMENT '<?xml version="1.0" encoding="UTF-8" ?>
     <student id="20140021">
          <name>Tom</name>
          <surname>Johnson</surname>
          <birth_date>"05/11/1995"</birth_date>
          <birth_place>"Miami"</birth_place>
          <points>9.45</points>
     </student>'
)
)
*/
SELECT X.*
FROM 
  xml_file V
, XMLTABLE
  ('$doc/student' PASSING V.data AS "doc"
   COLUMNS
     ID      INT         PATH '@id'
   , NAME    VARCHAR(20) PATH 'name'
   , SURNAME VARCHAR(20) PATH 'surname'
   , POINTS  DEC(5, 2)   PATH 'points'
  ) X
WHERE XMLEXISTS('$doc/student[birth_place = """Miami""" and name = "Tom"]' PASSING V.data AS "doc");

Try replacing your two where xmlexists s with one (this is used with your sample xml in the question, not your code):

where xmlexists('$DATA//student[birth_place/text()['Miami']][name/text()["Tom"]]');

or, two are required:

where xmlexists('$DATA/student[birth_place/text()['Miami']]')
and xmlexists('$DATA/student[name/text()["Tom"]]');

and see if either works.

The birth-place element contains double quotations marks which lead to an XPath evaluation error. To avoid this, replace where xmlexists('$DATA/student[birth_place = "Chicago"]') with one the following XPath expression:

XPath 1.0 friendly:

where xmlexists('$DATA/student/birth_place[substring(.,2,string-length(/student/birth_place)-2)="Chicago"]')

XPath 2.0 friendly:

where xmlexists('$DATA/student/birth_place[translate(.,codepoints-to-string(34),"")="Chicago"]')

XPath used to test on your sample data:

/student/birth_place[substring(.,2,string-length(/student/birth_place)-2)="Miami"]
/student/birth_place[translate(.,codepoints-to-string(34),"")="Miami"]

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