How to extract this XML - sql

I want to extract a xml file (XML_DATA):
The XML:
-<XP6>
+<INFO_1>
+<INFO_2>
+<INFO_3>
-<Prdct>
-<Prdct_row>
.....
<LILBFLO>Samsung,corp. </LILBFLO> <--value
I tried this, but it's not working:
EXTRACTVALUE(XML_DATA,'/Prdct/Prdct_row/LILBFLO/text()')
How to use extractvalue correctly?

Assuming the +/- symbols indicate collapsed nodes and your XML actually looks something like the sample in this CTE, you just need to include the root node in the path:
with your_table (xml_data) as (
select xmltype('<XP6>
<INFO_1/>
<INFO_2/>
<INFO_3/>
<Prdct>
<Prdct_row>
<LILBFLO>Samsung,corp. </LILBFLO>
</Prdct_row>
</Prdct>
</XP6>') from dual
)
select EXTRACTVALUE(XML_DATA,'/XP6/Prdct/Prdct_row/LILBFLO/text()')
from your_table;
EXTRACTVALUE(XML_DATA,'/XP6/PRDCT/PRDCT_ROW/LILBFLO/TEXT()')
------------------------------------------------------------
Samsung,corp.
But the extractvalue() function is deprecated, so you should use an XMLQuery instead:
select XMLQuery('/XP6/Prdct/Prdct_row/LILBFLO/text()' passing XML_DATA returning content)
from your_table;
XMLQUERY('/XP6/PRDCT/PRDCT_ROW/LILBFLO/TEXT()'PASSINGXML_DATARETURNINGCONTENT)
--------------------------------------------------------------------------------
Samsung,corp.

Related

PostgreSQL11 xpath query not working properly

When I execute below query in Postgres 10.12, it works properly.
SELECT (xpath('./sid/text()', c.node))[1]::text::bigint AS STUDENT_ID,
(xpath('./name/text()', c.node))[1]::text AS STUDENT_NAME
from (
select unnest(xpath('/data', '<data><sid>112233</sid><name>John</name></data>'::xml)) AS node
) c;
Output:
But When I execute same query in Postgres 11.7, it is not working.
What is the solution to fix this issue?
This is caused by this change:
Correctly handle relative path expressions in xmltable(), xpath(), and other XML-handling functions (Markus Winand)
Per the SQL standard, relative paths start from the document node of the XML input document, not the root node as these functions previously did.
so you need to change it to:
SELECT (xpath('/data/sid/text()', c.node))[1]::text::bigint AS STUDENT_ID,
(xpath('/data/name/text()', c.node))[1]::text AS STUDENT_NAME
from (
select unnest(xpath('/data', '<data><sid>112233</sid><name>John</name></data>'::xml)) AS node
) c;
because the inner xpath will return the <data> tag as well:
select unnest(xpath('/data', '<data><sid>112233</sid><name>John</name></data>'::xml)) AS node
results in:
<data>
<sid>112233</sid>
<name>John</name>
</data>
However, I would use xmltable for this:
select *
from xmltable('/data'
passing xml('<data><sid>112233</sid><name>John</name></data>')
columns
student_id bigint path 'sid',
student_name text path 'name')

Sum of values extracted using SQL

I have an xml like the below.
<LPNDetail>
<ItemName>5054807025389</ItemName>
<DistroNbr/>
<DistributionNbr>TR001000002514</DistributionNbr>
<OrderLine>2</OrderLine>
<RefField2/>
<RefField3>OU01180705</RefField3>
<RefField4>0002</RefField4>
<RefField5>Retail</RefField5>
<Qty>4</Qty>
<QtyUom>Unit</QtyUom>
</LPNDetail>
<LPNDetail>
<ItemName>5054807025563</ItemName>
<DistroNbr/>
<DistributionNbr>TR001000002514</DistributionNbr>
<OrderLine>4</OrderLine>
<RefField2/>
<RefField3>OU01180705</RefField3>
<RefField4>0004</RefField4>
<RefField5>Retail</RefField5>
<Qty>2</Qty>
<QtyUom>Unit</QtyUom>
</LPNDetail>
I have extracted the xml field using extract.xmltype and now i am getting the below result.
42
But i need to sum the quantity values i.e i need to get result as 6 (4+2).
Any help will be appreciated.
Thanks,
Shihaj
It is not clear what you mean by "an xml". If it's supposed to be an XML document, you are missing the outermost tags, perhaps something like <Document> ..... </Document>
If your text value is EXACTLY as you have shown it (which would be pretty bad), you can wrap within such outermost tags manually, and then use standard Oracle XML tools. For the illustration below I assume you simply have a string (VARCHAR2 or CLOB), not converted to XML type; in that case, I concatenate the beginning and end tags, and then convert to XMLtype, in the query.
with t ( str ) as (
select '<LPNDetail>
<ItemName>5054807025389</ItemName>
<DistroNbr/>
<DistributionNbr>TR001000002514</DistributionNbr>
<OrderLine>2</OrderLine>
<RefField2/>
<RefField3>OU01180705</RefField3>
<RefField4>0002</RefField4>
<RefField5>Retail</RefField5>
<Qty>4</Qty>
<QtyUom>Unit</QtyUom>
</LPNDetail>
<LPNDetail>
<ItemName>5054807025563</ItemName>
<DistroNbr/>
<DistributionNbr>TR001000002514</DistributionNbr>
<OrderLine>4</OrderLine>
<RefField2/>
<RefField3>OU01180705</RefField3>
<RefField4>0004</RefField4>
<RefField5>Retail</RefField5>
<Qty>2</Qty>
<QtyUom>Unit</QtyUom>
</LPNDetail>'
from dual
)
-- End of SIMULATED table (for testing purposes only, not part of the solution)
-- Query begins below this line
select sum(x.qty) as total_quantity
from t,
xmltable('/Document/LPNDetail'
passing xmltype('<Document>' || t.str || '</Document>')
columns qty number path 'Qty') x
;
Output:
TOTAL_QUANTITY
--------------
6

Parse XML value with Name Space in SQl

I have following XML :
<ProductionSchedule xmlns:inp2="http://www.wbf.org/xml/B2MML-V0401" xmlns="http://www.wbf.org/xml/B2MML-V0401">
<inp2:ProductionRequest>
<inp2:ID>0916A</inp2:ID>
<inp2:Description>SUBH190916A</inp2:Description>
<inp2:Location>
<inp2:EquipmentID>MYEqupiment</inp2:EquipmentID>
</inp2:Location>
<inp2:SegmentRequirement>
<inp2:ID>000</inp2:ID>
<inp2:EarliestStartTime>2015-10-17T12:00:00</inp2:EarliestStartTime>
<inp2:LatestEndTime>2015-10-19T12:00:00</inp2:LatestEndTime>
<inp2:MaterialProducedRequirement>
<inp2:MaterialDefinitionID>GEEC3MA0025EMZI</inp2:MaterialDefinitionID>
<inp2:Quantity>
<inp2:QuantityString>2</inp2:QuantityString>
</inp2:Quantity>
<inp2:MaterialProducedRequirementProperty>
<inp2:ID>ERPWOStatus</inp2:ID>
<inp2:Value>
<inp2:ValueString>Released</inp2:ValueString>
</inp2:Value>
</inp2:MaterialProducedRequirementProperty>
<inp2:MaterialProducedRequirementProperty>
<inp2:ID>ROUTING</inp2:ID>
<inp2:Value>
<inp2:ValueString>SOmeMPRVaue</inp2:ValueString>
</inp2:Value>
</inp2:MaterialProducedRequirementProperty>
<inp2:MaterialProducedRequirementProperty>
<inp2:ID>MPValue2</inp2:ID>
<inp2:Value>
<inp2:ValueString>2016-01-21T12:00:00</inp2:ValueString>
</inp2:Value>
</inp2:MaterialProducedRequirementProperty>
</inp2:MaterialProducedRequirement>
</inp2:SegmentRequirement>
</inp2:ProductionRequest>
</ProductionSchedule>
I am trying to get the value MPValue2 , from the XML.
I tried with following:
Select `#xml.value('(/ProductionSchedule/inp2:ProductionRequest/inp2:SegmentRequirement/inp2:MaterialProducedRequirement/inp2:MaterialProducedRequirementProperty)[1]','nvarchar(255)')`
Your select is OK, but you must consider/declare the namespaces:
WITH XMLNAMESPACES(DEFAULT 'http://www.wbf.org/xml/B2MML-V0401'
,'http://www.wbf.org/xml/B2MML-V0401' AS inp2)
Select #xml.value('(/ProductionSchedule/inp2:ProductionRequest/inp2:SegmentRequirement/inp2:MaterialProducedRequirement/inp2:MaterialProducedRequirementProperty)[1]','nvarchar(255)')
This works too (wildcard) but it's better to be as specific as possible:
Select #xml.value('(/*:ProductionSchedule/*:ProductionRequest/*:SegmentRequirement/*:MaterialProducedRequirement/*:MaterialProducedRequirementProperty)[1]','nvarchar(255)')
The fast and lazy would work too :-) but not fast in terms of performance...
Select #xml.value('(//*:MaterialProducedRequirementProperty)[1]','nvarchar(255)')
UPDATE
This is the query to get all your Properties:
WITH XMLNAMESPACES(DEFAULT 'http://www.wbf.org/xml/B2MML-V0401'
,'http://www.wbf.org/xml/B2MML-V0401' AS inp2)
SELECT prop.value('(inp2:ID)[1]','nvarchar(100)') AS Property
FROM #xml.nodes('/ProductionSchedule/inp2:ProductionRequest/inp2:SegmentRequirement/inp2:MaterialProducedRequirement/inp2:MaterialProducedRequirementProperty') AS A(prop)
The result
Property
--------
ERPWOStatus
ROUTING
MPValue2
UPDATE 2: Use the ID as filter in XQuery
See how I added the filter at the end of the XPath in .nodes().
Nodes will return all sub-elements row-wise. The filter will reduce the resultset to one single row (if inp2:ID is unique!) and then read the Value/ValueString.
I let the namespace declaration for DEFAULT and inp2. But, as #Serf pointed out correctly, both URLs are equal. It would be enough to declare only the DEFAULT and query without any namespace-prefixes...
DECLARE #TheID NVARCHAR(100)='MPValue2';
WITH XMLNAMESPACES(DEFAULT 'http://www.wbf.org/xml/B2MML-V0401'
,'http://www.wbf.org/xml/B2MML-V0401' AS inp2)
SELECT prop.value('(inp2:Value/inp2:ValueString)[1]','nvarchar(100)') AS Property
FROM #xml.nodes('/ProductionSchedule/inp2:ProductionRequest/inp2:SegmentRequirement/inp2:MaterialProducedRequirement/inp2:MaterialProducedRequirementProperty[inp2:ID=sql:variable("#TheID")]') AS A(prop)

SQL query to change file extension in a record containing a file-path?

Given an SQL table Table with a column path, how can I modify values like /dir/subdir/file.aaa => /dir/subdir/file.bbb e.g. modify just the file-extension without having to hard-code the specific file/path into my query?
Seems a perfect fit for regexp_replace :
with t as (select '/dir/subdir/file.aaa' as path from dual
union all select '/dir/subdir.aaa/file.aaa' from dual)
select regexp_replace(path, '[.][^.]*$', '.bbb') path
-- ^^^^ ^^^^^^^^^ ^^^^
-- replace last dot-whatever by the "right" extension
from t
where path like '%.aaa'
-- ^^^^^
-- only for path ending with the "wrong" extension
See http://sqlfiddle.com/#!4/d41d8/37017 for some tests
If the column only contains values that are structured like a file, path the following will work:
update the_table
set path = replace(path, '.aaa', '.bbb')
where path like '%.aaa';
Note that this will also update a value like /dir/subdir.aaa/file.aaa to /dir/subdir.bbb/file.bbb.
Another option is to use a regular expression:
update foo
set file_path = regexp_replace(file_path, '\.aaa$', '.bbb', 1, 0, 'i')
where lower(file_path) like '%.aaa';

Trying to get all xml nodes with SQL Server, what am I doing wrong?

I have the following xml in a table cell, in column MyColumn:
<BSDL xmlns="..." xmlns:i="...">
<dateTime>2012-12-30T00:00:00Z</dateTime>
<dateTime>2013-01-07T00:00:00Z</dateTime>
<dateTime>2013-01-14T00:00:00Z</dateTime>
<dateTime>2013-01-21T00:00:00Z</dateTime>
<dateTime>2013-01-29T00:00:00Z</dateTime>
<dateTime>2013-02-05T00:00:00Z</dateTime>
<dateTime>2013-02-12T00:00:00Z</dateTime>
<dateTime>2013-02-19T00:00:00Z</dateTime>
<dateTime>2013-03-22T00:00:00Z</dateTime>
<dateTime>2013-03-29T00:00:00Z</dateTime>
<dateTime>2013-04-19T00:00:00Z</dateTime>
</BSDL>
I'm just trying to query it (get all xml nodes) using:
SELECT BSDL.item.value('(dateTime)[1]', 'datetime')
from [MyTable]
CROSS APPLY [MyColumn].nodes ('//BSDL') BSDL(item)
it yields no result, although my MyColumn in MyTable has the same number of entries as above, for each row.
Since there's only one <BSDL> node - your call to //BSDL selects that single node and then item.value('(dateTime)[1]', 'datetime') returns the first <dateTime> child.
You need to change your XQuery to (use that xmlns=.... namespace you mention in your sample here):
;WITH XMLNAMESPACES('.....' as ns)
SELECT
item.value('.', 'datetime')
from
dbo.MyTable
CROSS APPLY
MyColumn.nodes ('/ns:BSDL/ns:dateTime') BSDL(item)
You need to get a list of all <dateTime> nodes under <BSDL> - then you'll get all of them and you can inspect them one by one.