[英]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.