I am trying to get envelope_id as 80621b17-97a8-926d945b602a from the below XML through this script, but nothing comes out. Can any one have any idea ?
SELECT b.EnvelopeID
FROM sample_xml a,
XMLTABLE(xmlnamespaces('http://www.w3.org/2001/XMLSchema' as "xsd_k",
'http://www.w3.org/2001/XMLSchema-instance' AS "xsi",
'http://www.docusign.net/API/3.0' AS "k"),
'/xsd_k:DocuSignEnvelopeInformation/xsd_k:EnvelopeStatus' PASSING a.xml
COLUMNS EnvelopeID VARCHAR2(200) PATH 'EnvelopeID')b;
My sample xml is :
<DocuSignEnvelopeInformation xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.docgign.net/API/3.0">
<EnvelopeStatus>
<EnvelopeID>80621b17-97a8-926d945b602a</EnvelopeID>
</EnvelopeStatus>
</DocuSignEnvelopeInformation>
You can use the default namespace. Just say 'default' :). And look to be the same as in your XML document:
SELECT b.EnvelopeID
FROM (SELECT xmltype ('
<DocuSignEnvelopeInformation xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.docgign.net/API/3.0">
<EnvelopeStatus>
<EnvelopeID>80621b17-97a8-926d945b602a</EnvelopeID>
</EnvelopeStatus>
</DocuSignEnvelopeInformation>') AS xml FROM DUAL) a,
XMLTABLE(xmlnamespaces(default 'http://www.docgign.net/API/3.0'),
'/DocuSignEnvelopeInformation/EnvelopeStatus' PASSING a.xml
COLUMNS EnvelopeID VARCHAR2(200) PATH 'EnvelopeID')b;
We can also repair your variant. First you have 'docgign' instead of 'docusign' in the xml document. Repair one of the both. Than change '/xsd_k:DocuSignEnvelopeInformation/xsd_k:EnvelopeStatus' with '/k:DocuSignEnvelopeInformation/k:EnvelopeStatus', because this is the default namespace and at last change 'EnvelopeID' with 'k:EnvelopeID' :
SELECT EnvelopeID
FROM (SELECT xmltype ('
<DocuSignEnvelopeInformation xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.docusign.net/API/3.0">
<EnvelopeStatus>
<EnvelopeID>80621b17-97a8-926d945b602a</EnvelopeID>
</EnvelopeStatus>
</DocuSignEnvelopeInformation>') AS xml FROM DUAL) a,
XMLTABLE(xmlnamespaces('http://www.w3.org/2001/XMLSchema' as "xsd_k",
'http://www.w3.org/2001/XMLSchema-instance' AS "xsi",
'http://www.docusign.net/API/3.0' AS "k"),
'/k:DocuSignEnvelopeInformation/k:EnvelopeStatus' PASSING a.xml
COLUMNS EnvelopeID VARCHAR2(200) PATH 'k:EnvelopeID')b;
See HERE(search for Default Namespace)
When a default namespace declaration is used on an element, all
unqualified element names within its scope are automatically
associated with the specified namespace identifier.
That is the reason because DocuSignEnvelopeInformation and all other nodes under this have the namespace xmlns="http://www.docusign.net/API/3.0". If this namespace was not defined, the nodes would be without namespace and you wouldn't need to use namespace in xmltable.
Related
I am trying to update an element which is inside a clob column in oracle DB.
First challenge I am facing is that my clob xml has 2 namespaces and I am not able to get that working .
<?xml version="1.0"?>
<esbmsg:EsbMessage xmlns:esbmsg="http://www.test.com/esb/message/1.0">
<esbmsg:Body>
<Transaction xmlns="http://test.com">
<test-element>
<finalElement>false</finalElement>
</test-elemen>
</Transaction>
</esbmsg:Body>
</esbmsg:EsbMessage>
select x.* from cc_messagehistory y
cross join xmltable(
xmlnamespaces('http://www.test.com/esb/message/1.0' as "esbmsg",
'http://test.com ' ),
'/esbmsg:EsbMessage'
passing xmltype.createxml(y.payload)
factext varchar2(10) path '/esbmsg:EsbMessage/esbmsg:Body/Transaction/test-element/finalElement'
) x;
ORA-19102: XQuery string literal expected
19102. 00000 - "XQuery string literal expected"
*Cause: The string literal containing the XQuery expression was missing.
*Action: Specify the XQuery expression as a string literal. Error at Line: 64 Column: 99
The immediate cause of the ORA-01902 is that you missed the default keyword:
xmlnamespaces('http://www.test.com/esb/message/1.0' as "esbmsg",
default 'http://test.com'),
I've removed the extra space at the end of the URI, which would cause problems later. But you are also missing the columns keyword, and you can simplify the conversion of the CLOB value to an XMLType.
Putting that together, and with a CTE to supply your (corrected) sample XML:
-- CTE for sample data
with cc_messagehistory(payload) as (
select to_clob('<?xml version="1.0"?>
<esbmsg:EsbMessage xmlns:esbmsg="http://www.test.com/esb/message/1.0">
<esbmsg:Body>
<Transaction xmlns="http://test.com">
<test-element>
<finalElement>false</finalElement>
</test-element>
</Transaction>
</esbmsg:Body>
</esbmsg:EsbMessage>') from dual
)
-- actual query
select x.*
from cc_messagehistory y
cross join xmltable (
xmlnamespaces (
'http://www.test.com/esb/message/1.0' as "esbmsg",
default 'http://test.com'
),
'/esbmsg:EsbMessage'
passing xmltype(y.payload)
columns factext varchar2(10)
path '/esbmsg:EsbMessage/esbmsg:Body/Transaction/test-element/finalElement'
) x;
FACTEXT
----------
false
For the update you could do something like:
update cc_messagehistory y
set payload = XMLSerialize(document
XMLQuery('declare default element namespace "http://test.com"; (: :)
declare namespace esbmsg="http://www.test.com/esb/message/1.0"; (: :)
copy $i := $xml modify (
for $j in $i//esbmsg:EsbMessage/esbmsg:Body/Transaction/test-element/finalElement
return replace value of node $j with $new
)
return $i'
passing xmltype(y.payload) as "xml",
'true' AS "new"
returning content
)
indent size=2
)
where xmlexists('declare default element namespace "http://test.com"; (: :)
declare namespace esbmsg="http://www.test.com/esb/message/1.0"; (: :)
$xml//esbmsg:EsbMessage/esbmsg:Body/Transaction/test-element/finalElement[text()="false"]'
passing xmltype(y.payload) as "xml");
which transforms that source CLOB into:
<?xml version="1.0"?>
<esbmsg:EsbMessage xmlns:esbmsg="http://www.test.com/esb/message/1.0">
<esbmsg:Body>
<Transaction xmlns="http://test.com">
<test-element>
<finalElement>true</finalElement>
</test-element>
</Transaction>
</esbmsg:Body>
</esbmsg:EsbMessage>
db<>fiddle (works on 18c; errors on 11gR2, but patch levels may make a difference; also tested successfully elsewhere on 12cR1)
query from xml not return rows.
I running this query but not return rows.
The my xml is :
<?xml version="1.0" encoding="UTF-8"?>
<ns0:testata xmlns:ns0="http://siete">
<ns0:product>
<ns0:YEAR>2019</ns0:YEAR>
<ns0:PERIOD>1</ns0:PERIOD>
</ns0:product>
<ns0:product>
<ns0:YEAR>2019</ns0:YEAR>
<ns0:PERIOD>2</ns0:PERIOD>
</ns0:product>
</ns0:testata>
My query is
FROM XMLTABLE('/testata/product'
PASSING
(select xmltype(t.XML1) doc
from tb_test t)
COLUMNS
name varchar2(4) PATH './YEAR'
) xmlt
0 rows
please help me
Your XML document has a namespace, so you either need to wildcard the nodes in your XMLTable call, or - preferably - supply the same namespace information and prefixes:
-- CTE for sample data
with tb_test (xml1) as (select '<?xml version="1.0" encoding="UTF-8"?>
<ns0:testata xmlns:ns0="http://siete">
<ns0:product>
<ns0:YEAR>2019</ns0:YEAR>
<ns0:PERIOD>1</ns0:PERIOD>
</ns0:product>
<ns0:product>
<ns0:YEAR>2019</ns0:YEAR>
<ns0:PERIOD>2</ns0:PERIOD>
</ns0:product>
</ns0:testata>' from dual
)
-- actual query
select x.year
from tb_test t
cross join xmltable(
xmlnamespaces('http://siete' as "ns0"),
'/ns0:testata/ns0:product'
passing xmltype(t.xml1)
columns year number path 'ns0:YEAR'
) x;
YEAR
----------
2019
2019
I am trying to parse webservice SOAP response returned by a JAVA webservice in plsql. I am facing no error but i am getting nothing. Below is the code
WITH t as (select XMLTYPE('<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:helloResponse xmlns:ns2="http://example.com/">
<ns2:return>Hello World</ns2:return>
</ns2:helloResponse>
</S:Body>
</S:Envelope>') as xml from dual)
select *
from t,
xmltable(XMLNAMESPACES('http://example.com/' as "ns2"),
'/Envelope/Body'
passing t.xml
columns myret varchar2(50) path '/ns2:helloResponse/ns2:return'
) x
What am i doing wrong here ?
I can see a couple of issues here....
1) You have two namespaces (one for the envelope and body, and the other for the nodes) you need to account for both of these in your select
2) in your path for MyRet the context is from the envelope so you either need to specify the full path or use // at the beginning.
I am making some assumptions as to the output that you actually want but I think the below should work for you.
WITH t as (select XMLTYPE('<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:helloResponse xmlns:ns2="http://example.com/">
<ns2:return>Hello World</ns2:return>
</ns2:helloResponse>
</S:Body>
</S:Envelope>') as xml from dual)
select *
from t,
xmltable(XMLNAMESPACES('http://example.com/' as "ns2", 'http://schemas.xmlsoap.org/soap/envelope/' as "S"),
'/S:Envelope/S:Body'
passing t.xml
columns myret varchar2(50) path '//ns2:helloResponse/ns2:return'
) x
How to display parent tags and attributes from XML TYPE column using stored procedure?
Example: I have below data in XMLTYPE Column in one table:
<Employee>
<Employee_information >
<Employee_content>
<task>
<Employee_stem>
<Employee_stem_paragraph>fsdbnfjksdflsdj.</Employee_stem_paragraph>
<Employee_stem_paragraph>dsfsdfsdfsdf</Employee_stem_paragraph>
</Employee_stem>
<Employee_response>
<Employee_response_choices>
<Employee_choice_list>
<Employee_block_choice numeric_identifier="1">
<Employee_choice_paragraph>sdfsdfsdfsdf</Employee_choice_paragraph>
</Employee_block_choice>
</Employee_choice_list>
</Employee_response_choices>
</Employee>
Output: display all the parent tags and attributes in it.
Output example:
Employee,Employee_information,Employee_content,Employee_stem,Employee_stem_paragraph,Employee_response,Employee_response_choices,Employee_choice_list,Employee_block_choice,
numeric_identifier,Employee_choice_paragraph.
In general you have to fix you xml. All tag have to be closed.
1-st select //*/name(.) - extract only element's name.
2-en select '//*/name(#*) -extract only attributes' name.
select *
from xmltable('//*/name(.)'
passing xmltype('<Employee>
<Employee_information>
<Employee_content>
<task>
<Employee_stem>
<Employee_stem_paragraph>fsdbnfjksdflsdj.</Employee_stem_paragraph>
<Employee_stem_paragraph>dsfsdfsdfsdf</Employee_stem_paragraph>
</Employee_stem>
<Employee_response>
<Employee_response_choices>
<Employee_choice_list>
<Employee_block_choice numeric_identifier="1">
<Employee_choice_paragraph>sdfsdfsdfsdf</Employee_choice_paragraph>
</Employee_block_choice>
</Employee_choice_list>
</Employee_response_choices>
</Employee_response>
</task>
</Employee_content>
</Employee_information>
</Employee>') columns name varchar2(200) path '.' )
union all
select *
from xmltable('//*/name(#*)'
passing xmltype('<Employee>
<Employee_information>
<Employee_content>
<task>
<Employee_stem>
<Employee_stem_paragraph>fsdbnfjksdflsdj.</Employee_stem_paragraph>
<Employee_stem_paragraph>dsfsdfsdfsdf</Employee_stem_paragraph>
</Employee_stem>
<Employee_response>
<Employee_response_choices>
<Employee_choice_list>
<Employee_block_choice numeric_identifier="1">
<Employee_choice_paragraph>sdfsdfsdfsdf</Employee_choice_paragraph>
</Employee_block_choice>
</Employee_choice_list>
</Employee_response_choices>
</Employee_response>
</task>
</Employee_content>
</Employee_information>
</Employee>') columns name varchar2(200) path '.' )
I have a following xml document stored in XML type table
<?xml version="1.0" encoding="utf-8"?>
<Document xmlns="urn:iso:std:iso:200:tech:xsd:101" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<FIToFICstmrCdtTrf xmlns="urn:iso:std:iso:200:tech:xsd:101">
<CreditTx>
<PaymentID>
<InstrumentId>AAB000001</InstrumentId>
<Id>4730 2013-10-23 AAB000001</Id>
<TranxId>BULKTTXTDAAB000001</TranxId>
</PaymentID>
</CreditTx>
</FIToFICstmrCdtTrf>
</Document>
When i execute the below SQL
SELECT payments.instrumentid,
payments.id,
payments.tranxid
FROM paymentxml,
XMLTable('for $i in /Document/FIToFICstmrCdtTrf/CreditTx/PaymentID
return $i'
PASSING OBJECT_VALUE
COLUMNS
instrumentid VARCHAR2(20) PATH 'InstrumentId' ,
id VARCHAR2(20) PATH 'Id' ,
tranxid VARCHAR2(20) PATH 'TranxId'
)payments
I get no results even though the xpath is correct any ideas what am i missing
For the
for $i in /Document/FIToFICstmrCdtTrf/CreditTx/PaymentID return $i
part you must give the doc as
for $i in
doc("/your/path/your.xml")/Document/FIToFICstmrCdtTrf/CreditTx/PaymentID
return $i
it is important to know resources' location which is /your/path/your.xml
i hope this helps you.