我已经在Db2中创建了这样的表:
create table xml_file(data xml not null)
这是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>
我想为姓名为Ben且出生地为Chicago的所有学生选择id,姓名,姓氏和分数。
我写了这样的东西:
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"]');
[我所得到的就是以下消息:“ FETCHED 0 RECORDS,0 RECORDS SHOWN”(在IBM Data Studio中)。
有人可以告诉我我做错了什么吗?
尝试用一个替换两个where xmlexists
(这与问题中的示例xml而不是您的代码一起使用:]
出生地元素包含双引号,这会导致XPath评估错误。为避免这种情况,请用以下XPath表达式之一替换where xmlexists('$DATA/student[birth_place = "Chicago"]')
: