[英]How to find XML documents with certain attributes and nested elements?
我正在搜索可以在Oracle数据库中找到行的正则表达式。 这些行包含一个看起来像这样的字符串:
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<characteristics>
<characteristic id="1001" mandatory="true" seq="1">
<wildcard/>
</characteristic>
<characteristic id="10001" mandatory="false" seq="1">
<value negation="false" seq="1">63</value>
</characteristic>
<characteristic id="10002" mandatory="false" seq="1">
<value negation="false" seq="1">64</value>
</characteristic>
<characteristic id="10051" mandatory="false" seq="1">
<value negation="false" seq="1">65</value>
</characteristic>
<characteristic id="10052" mandatory="false" seq="1">
<value negation="false" seq="1">66</value>
</characteristic>
<characteristic id="1010" mandatory="false" seq="100">
<wildcard/>
<value negation="false" seq="1">314</value>
</characteristic>
</characteristics>
正则表达式必须查找所有包含特征的行,这些特征具有包含通配符的特殊ID:
<wildcard/>
例如:我想获取所有包含具有通配符的ID为1000和1001的特征的行。 这意味着必须有两个字符串
<characteristic id="1001" ...
<wildcard/>
...
</characteristic>
和
<characteristic id="1000" ...
<wildcard/>
...
</characteristic>
在里面。 因此,两个字符串的顺序可以不同。
假设数据存储为XMLtype
select *
from t
where EXISTSNODE(doc,'/characteristics/characteristic[@id="1000"]/wildcard') = 1
and EXISTSNODE(doc,'/characteristics/characteristic[@id="1001"]/wildcard') = 1
;
替代语法(如@Tomalak所建议)
select *
from t
where EXISTSNODE(doc,'/characteristics[characteristic[@id="1001"]/wildcard and characteristic[@id="1010"]/wildcard]') = 1
出于学习目的,如果数据存储为字符串
select *
from (select xmltype(doc) as doc from t)
where EXISTSNODE(doc,'/characteristics/characteristic[@id="1000"]/wildcard') = 1
and EXISTSNODE(doc,'/characteristics/characteristic[@id="1001"]/wildcard') = 1
;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.