Reputation: 17
We have a table called JOURNALARTICLE
with column CONTENT
, which has XML data stored as a CLOB.
<?xml version="1.0"?>
<root available-locales="en_US" default-locale="en_US">
<dynamic-element name="group_html_title" type="text" index-type="keyword" index="0">
<dynamic-content language-id="en_US"><![CDATA[Industrial AC Drives]]></dynamic-content>
</dynamic-element>
<dynamic-element name="group_image" type="document_library" index-type="keyword" index="0">
<dynamic-content language-id="en_US"><![CDATA[/documents/20184/40787/Industrial-AC-Drives--Fan-and-Pump_Drives.png/8a1e761d-1cac-49aa-8212-fa3943204449?t=1437389273031]]></dynamic-content>
</dynamic-element>
<dynamic-element name="product_type" type="list" index-type="keyword" index="0">
<dynamic-content language-id="en_US"><![CDATA[Drives]]></dynamic-content>
</dynamic-element>
<dynamic-element name="page_banner" type="list" index-type="keyword" index="0">
<dynamic-content language-id="en_US"><![CDATA[a2cdb3ca-8872-4d08-a6ee-00b84495e0e0]]></dynamic-content>
</dynamic-element>
<dynamic-element name="access_groups" type="list" index-type="keyword" index="0">
<dynamic-content language-id="en_US"><![CDATA[]]></dynamic-content>
</dynamic-element>
<dynamic-element name="short_copy" type="text_area" index-type="keyword" index="0">
<dynamic-content language-id="en_US"><![CDATA[<p>Drives that offer a broad range of control modes, features, options, and packaging, for use in factory automation processes and industrial OEM machines. <a href="javascript:void(0);" onclick="showDocumentsStaticPopup('eLM.J1000.01')" style="line-height: 1.5;">Test POPUP</a> <a href="javascript:void(0);" onclick="showDocumentsStaticPopup('eLM.J1000.01')" style="line-height: 1.5;">Test POPUP</a></p>]]></dynamic-content>
</dynamic-element>
<dynamic-element name="html_body" type="text_area" index-type="keyword" index="0">
<dynamic-content language-id="en_US"><![CDATA[]]></dynamic-content>
</dynamic-element>
<dynamic-element name="group_active" type="boolean" index-type="keyword" index="0">
<dynamic-content language-id="en_US"><![CDATA[true]]></dynamic-content>
</dynamic-element>
<dynamic-element name="page_meta_description" type="text" index-type="keyword" index="0">
<dynamic-content language-id="en_US"><![CDATA[Yaskawa's Family of AC Drives are designed for industrial automation applications. These adjustable frequency drives are typically capable of network communications, supported by software tools for parameter management and able to accept custom software]]></dynamic-content>
</dynamic-element>
<dynamic-element name="page_meta_keywords" type="text" index-type="keyword" index="0">
<dynamic-content language-id="en_US"><![CDATA[Yaskawa, AC_drive, AC, VFD, industrial_automation, industrial_drives, variable_frequency_drive, AC_drives]]></dynamic-content>
</dynamic-element>
<dynamic-element name="business_weightage" type="ddm-number" index-type="keyword" index="0">
<dynamic-content language-id="en_US"><![CDATA[5000]]></dynamic-content>
</dynamic-element>
<dynamic-element name="clickable_in_left_navigation" type="boolean" index-type="keyword" index="0">
<dynamic-content language-id="en_US"><![CDATA[true]]></dynamic-content>
</dynamic-element>
<dynamic-element name="clickable_in_menu" type="boolean" index-type="keyword" index="0">
<dynamic-content language-id="en_US"><![CDATA[true]]></dynamic-content>
</dynamic-element>
<dynamic-element name="visible_in_left_navigation" type="boolean" index-type="keyword" index="0">
<dynamic-content language-id="en_US"><![CDATA[true]]></dynamic-content>
</dynamic-element>
<dynamic-element name="visible_in_menu" type="boolean" index-type="keyword" index="0">
<dynamic-content language-id="en_US"><![CDATA[true]]></dynamic-content>
</dynamic-element>
<dynamic-element name="external_url" type="text" index-type="keyword" index="0">
<dynamic-content language-id="en_US"><![CDATA[]]></dynamic-content>
</dynamic-element>
<dynamic-element name="shared_with_site" type="list" index-type="keyword" index="0">
<dynamic-content language-id="en_US"><![CDATA[]]></dynamic-content>
</dynamic-element>
</root>
I want to extract text from a dynamic-content
node where the attribute name is "group_html_title"
, so it should return the value "Industrial AC Drives".
How can I do that?
Upvotes: 0
Views: 763
Reputation: 191570
You can use an XMLQuery, or XMLTable:
select x.content
from journalarticle j
cross join xmltable ('/root/dynamic-element[@name="group_html_title"]'
passing xmltype(j.content)
columns content varchar2(4000) path 'dynamic-content'
) x;
CONTENT
--------------------------------------------------------------------------------
Industrial AC Drives
The XPath looks for dynamic-element
nodes under root
, and restricts that to the nodes where the attribute has the name you want with the [@name=...]
section. Then it extracts the node value from the dynamic-content
node under any matching element nodes.
Read more in the documentation.
The question title mentions a where clause. You can extract all the data into columns with XMLTable and then filter based on one of those, or you can use an XMLQuery directly in the where clause:
select *
from journalarticle j
where xmlquery('for $i in /root/dynamic-element[@name="group_html_title"]
where $i/dynamic-content = "Industrial AC Drives"
return $i'
passing xmltype(j.content) returning content) is not null;
Or combine both, and just get the data you want with XMLTable, with an XMLQuery to filter the base table rows first.
Upvotes: 1