简体   繁体   中英

How to find XML documents with certain attributes and nested elements?

I am searching for the regular expression that can find rows in an Oracle database. The rows contain a string that looks like this:

<?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>

The regular expression has to find all rows, that contain characteristics with special ids that contain a wildcard:

<wildcard/>

For Example: I want to get all rows, that contain the characteristics with ids 1000 and 1001 with wildcard. That means there must be the two strings

<characteristic id="1001" ...
  <wildcard/>
  ...
</characteristic>

and

<characteristic id="1000" ...
  <wildcard/>
  ...
</characteristic>

in it. Thereby the two strings can be in different orders.

Assuming the data is stored as XMLtype

select *
from   t
where  EXISTSNODE(doc,'/characteristics/characteristic[@id="1000"]/wildcard') = 1
  and  EXISTSNODE(doc,'/characteristics/characteristic[@id="1001"]/wildcard') = 1
;     

Alternative syntax (as suggested by @Tomalak)

select *
from   t
where  EXISTSNODE(doc,'/characteristics[characteristic[@id="1001"]/wildcard and characteristic[@id="1010"]/wildcard]') = 1

For learning purposes, in case the data is stored as string

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
;        

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