Extracting XML data from NCLOB from XML with attributes in Oracle db - sql

I am trying to extract values from XML data stored as NCLOB column in Oracle db table.
xml structure
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="http://example.com/FAS/DOC/2011/v3.0b" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<record xmlns="http://example.com/FAS/DOC/2011/v3.0b">
<pdate xmlns="http://example.com/FAS/DOC/2011/v3.0b">2014-05-15</pdate>
</record>
</root>
Query
select EXTRACTVALUE(XMLTYPE(nclob_column),'/root/record/pdate','xmlns="http://example.com/FAS/DOC/2011/v3.0b"') pdate1,
EXTRACTVALUE(XMLTYPE(nclob_column),'/root/record/pdate') pdate2
from nclob_table
Problem
The pdate1 does return the value, but pdate2 returns null. I
cannot use the third parameter of EXTRACTVALUE() to specify the xmlns
attribute value as that changes on every row/record. So I get the
value for one row but null for all others.
How do I extract the value without specifying the attribute?
Thanks.

If you can guarantee that the namespace doesn't matter for selecting an element in this case, you can use local-name() to match element only by it's local name, ignoring the namespace :
select EXTRACTVALUE(
XMLTYPE(nclob_column),
'/*[local-name()="root"]/*[local-name()="record"]/*[local-name()="pdate"]'
) pdate
from nclob_table
SQL Fiddle

Related

Querying XML tag in SQL server

I have a table Student with a column studentStateinfo which consist of XML value as below.
<params xmlns="">
<OldStudentID>1aedghe1d8ef</OldStudentID>
</params>
Now when I query this table Student I only want to check whether studentStateinfo column have an XML data with tag <OldStudentID>
Use the exist() Method (xml Data Type)
Example using a variable, you should change that to a column instead.
declare #X xml = '
<params xmlns="">
<OldStudentID>1aedghe1d8ef</OldStudentID>
</params>';
select #X.exist('/params/OldStudentID');

Parse XML in SQL Server resulting in NULLs

I'm trying to parse an XML file in a SQL Server 2014 stored procedure.
The XML file looks like this:
<PROJECTS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<row>
<APPLICATION_ID>9204797</APPLICATION_ID>
<ACTIVITY>R44</ACTIVITY>
<ADMINISTERING_IC>AI</ADMINISTERING_IC>
<APPLICATION_TYPE>5</APPLICATION_TYPE>
<ARRA_FUNDED>N</ARRA_FUNDED>
<AWARD_NOTICE_DATE>01/11/2017</AWARD_NOTICE_DATE>
<BUDGET_START>01/01/2017</BUDGET_START>
<BUDGET_END>12/31/2017</BUDGET_END>
</row>
</PROJECT>
And my code is
SELECT
nref.value('#APPLICATION_ID[1]','varchar(max)') APPLICATION_ID,
nref.value('#ACTIVITY[1]','varchar(max)') ACTIVITY
FROM
[ADMIN_Grant_Exporter_Files_XML]
CROSS APPLY
XMLData.nodes('//PROJECT/row') as R(nref)
WHERE
APPLICATION_ID = '9204797'
APPLICATION_ID is stored as a separate column in the table.
I have tried
XMLData.nodes('//PROJECT/row')
and all combinations such as
XMLData.nodes('//PROJECT[1]')
XMLData.nodes('//row[1]')
Any help is appreciated. All I get back are nulls even if I remove the WHERE because I only have one record in the table at this point.
Root tag(PROJECTS) spelling is wrong in nodes method. Then to extract APPLICATION_ID you need to use value method like this.
nref.value('(APPLICATION_ID)[1]', 'Int')
# is used in value method to read attributes but APPLICATION_ID is a element in your xml
also you cannot use the alias name in same select queries Where clause
DECLARE #xml XML ='<PROJECTS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<row>
<APPLICATION_ID>9204797</APPLICATION_ID>
<ACTIVITY>R44</ACTIVITY>
<ADMINISTERING_IC>AI</ADMINISTERING_IC>
<APPLICATION_TYPE>5</APPLICATION_TYPE>
<ARRA_FUNDED>N</ARRA_FUNDED>
<AWARD_NOTICE_DATE>01/11/2017</AWARD_NOTICE_DATE>
<BUDGET_START>01/01/2017</BUDGET_START>
<BUDGET_END>12/31/2017</BUDGET_END>
</row>
</PROJECTS>' -- Here PROJECT should be PROJECTS
SELECT nref.value('(APPLICATION_ID)[1]', 'Int') APPLICATION_ID,
nref.value('(ACTIVITY)[1]', 'varchar(max)') ACTIVITY
FROM #xml.nodes('//PROJECTS/row') AS R(nref) -- Here PROJECT should be PROJECTS
WHERE nref.value('(APPLICATION_ID)[1]', 'Int') = 9204797

Fetch Tag\Element value of XML from CLOB in DB2 database

i have an xml of the following structue...
Structure of XML:
<Persons>
<PersonID>12345</Person>
<PersonName>Larissa</Person>
<PersonAge>28</Person>
<Persons>
<Persons>
<PersonID>12345</Person>
<PersonName>Larissa</Person>
<PersonAge>28</Person>
<Persons>
The xml is in the CLOB datatype column of IBM DB2 Database.I want to fire an select query to extract the value of PersonID field and get the value 12345 in return.
Is there any functions in DB2 for xml by using which i can extract the value of the PersonID???
(Assuming DB2 Linux/Unix/Window)
You can use xquery to get the results you want:
xquery
db2-fn:xmlcolumn('YOUR_TABLE.YOUR_COLUMN')/Persons/PersonID/text()
Since this query invokes xquery directly, you have to instruct DB2 to use the xquery parser (instead of the SQL parser) by using the xquery keyword.

XML data type and Sql server 2005

i have one table where one field type is xml and there data is saved in xml format. my xml is
<Record xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<DELETED>
<STOCK_CODE>111111</STOCK_CODE>
<MakeID>GB00000001</MakeID>
<ModelID>GB00000001</ModelID>
<EngineSize />
<YearMakeFrom>0</YearMakeFrom>
<YearMakeTo>0</YearMakeTo>
<Automatic>1</Automatic>
<SemiAutomatic>1</SemiAutomatic>
<Manual>0</Manual>
<OtherInfo />
<Status>UPDATED</Status>
</DELETED>
</Record>
so please tell me how could i query the above xml document in sql server 2005. please help. thanks.
You're not saying what you're looking for exactly - so here's just a guess.
Assume you have a table full of rows, each row has a XML column XmlData which contains the above structure, and you want to get the Stock_Code and ModelID from that XML.
In that case, you'd use something like this:
SELECT
ID,
XmlData.value('(/Record/DELETED/STOCK_CODE)[1]', 'BIGINT') AS 'StockCode',
XmlData.value('(/Record/DELETED/ModelID)[1]', 'VARCHAR(25)') AS 'ModelID'
FROM
dbo.YourTable
WHERE
(some condition)
Is that what you're looking for?? If not: please clarify your question!

How to select a particular Node name in XML using Oracle SQL query?

I need to select a particular Node name in the XML file using sql string?
Sample XML structure
<Root>
<Body>
<Car> // This can be "Bike", "ship", "Train"... ect
<name value="figo"/>
</Car>
</Body>
</Root>
I want to run a query which which will fetch what Node name is present in XML "car" or "Train" or "Bike".. etc.
Select * from TableA where.....?
TableA has column "Message" of type CLOB which stores the XML.
-Praveen
You can cast the CLOB type to XMLTYPE (or consider using an XMLTYPE column on the table). When dealing with XMLTYPEs you can then run XPATHs e.g:
SELECT extractvalue(xml_col, '/*/Body/*/name/#value')
FROM TableA
or extract for xml fragments.
EDIT: changed the 'Car' to * in XPath, having re-read the question.