How to get child of xmlelement in sql query without its name? - xpath

In sql server xml column I have xml like this:
<Test>
<Operations>
<Operations type="OperationSend">
<OperationSend>
<ToCompanyId>1</ToCompanyId>
<Date>2011-05-01T00:00:00</Date>
</OperationSend>
</Operations>
<Operations type="OperationSell">
<OperationSell>
<ToCompanyId>33</ToCompanyId>
<Amount>12</Amount>
</OperationSell>
</Operations>
<Operations type="OperationEdit">
<OperationEdit>
<ToCompanyId>12</ToCompanyId>
<Date>2011-11-01T00:00:00</Date>
</OperationEdit>
</Operations>
</Operations>
</Test>
I need to take ToCompanyId from last operation (12). I came to something like this. What should be in ??? when there can be any operation type with ToCompanyId.
select testxml.query('(/Test/Operations/Operations)[last()]/???/ToCompanyId') from dbo.MyXmlTable

You can use *
select testxml.query('(/Test/Operations/Operations)[last()]/*/ToCompanyId').value('.', 'int')
from MyXmlTable

Put node() instead of ???
node() matches all nodes of any kind

Assuming that you have set your xml to be a variable named #x then this is how to get your 12.
select x.header.value('.', 'int')
from #x.nodes('//Test/Operations/Operations[last()]/OperationSend/ToCompanyId')
as x(header)
the query would be slightly different to get from a column of a table but the XPATH would be the same.
select testxml.query('//Test/Operations/Operations[last()]/OperationSend/ToCompanyId') from dbo.MyXmlTable

Related

SAP Business Objects Query Builder Query - SI_SESSION_USER

I am trying to get a list of all connections where the SI_SESSION_USER='xyz'.
When I do a query like
select * from si_infoobjects where si_id='00000', I can see this field in the results with that value (xyz).
When I modify the query to look for that specific field and value, it returns zero rows.
I am using:
select * from si_infoobjects where SI_SESSION_USER='xyz'
What query will return the correct results?
Just a guess here, but si_session_user is probably in the Processing Info bag. So use this:
select *
from ci_infoobjects
where si_processinfo.SI_SESSION_USER='xyz'
Note that that's ci_infoobjects not si_infoobjects, but I assume that's just a typo in your question.

How to execute a function that returns an Oracle UDT (User data type) in WSO2 DSS?

I've tried with:
select package.faunction(param1,param2...paramX) as "data" from dual.
Works like a charm. Then, I´ve got the output result with:
element column="data[0]" name="name1" xsdType="string"
element column="data[1]" name="name2" xsdType="string"
And that works too. But when the output has more than 10 columns I've got an exception
Cannot find parameter with type:column name:data[10] DS Code: INCOMPATIBLE_PARAMETERS_ERROR
Any idea what I can do to make this work with more than 10 parameters?
PS: If I use the same query with the "User column numbers" option checked and <element column="1" name="result" xsdType="string"/> as output mapping WORKS, but all the UDT came in just one column.

how to remove namespace from an existing xml subtree in oracle

I'm facing an issue with removing namespace from existing xml subtree.
For Example: I have a table with 1 field - data type xmltype. One entry is holding this xml:
<Item xmlns="http://www.w3.org/2001/XMLSchema-instance">
<Box>Blaff</Box>
<Door>Steal</Door>
<Chair>Wood</Chair>
</Item>
I would like to remove the namespace - i.e.:
<Item>
<Box>Black-box</Box>
<Door>Steal</Door>
<Chair>Wood</Chair>
</Item>
Do you have any suggestions? - I tried using the UPDATE() function with no luck.
Thank you very much for the help.
One idea is to transform it to a clob, then replace the namespace string and transform it back to xml:
with xtab as (
select xmltype('<Item xmlns="http://www.w3.org/2001/XMLSchema-instance">
<Box>Blaff</Box>
<Door>Steal</Door>
<Chair>Wood</Chair>
</Item>') my_xml from dual)
select xmltype(replace(xmltype.getclobval(my_xml),
' xmlns="http://www.w3.org/2001/XMLSchema-instance"', '')) clo
from xtab;

Multiple conditions in xpath

I am new to xquery and want to add the conditions on activityStatus as 'A' and maritalStatus as "Married" to get gender of person. I have already created query to add conditions individually but am kind of stuck in case to adding multiple conditions. Any pointers will be helpful.
Thanks in advance.
Query to get gender on the basis of Marital Status:
SELECT PersonInfo.gender_1
from personData personData,
xmltable('$personData/Person/personDataCollection/
personData[(maritalStatus=("Married"))]'
passing personData."XMLDATA" as "personData"
columns gender_1 varchar(12) path 'gender'
) PersonInfo
...and the document...
<?xml version="1.0" encoding="UTF-8"?>
<Person>
<activityStatus>A</activityStatus>
<personDataCollection>
<personData>
<personName>
<lastName>ABC</lastName>
<firstName>XYZ</firstName>
</personName>
<addressCollection>
<address>
<line1>123</line1>
<city>QWERTY</city>
<state>ASDF</state>
<postalCode>147852</postalCode>
</address>
</addressCollection>
<maritalStatus>Married</maritalStatus>
<gender>M</gender>
</personData>
</personDataCollection>
</Person>
It sounds like you just want to add a second predicate to your XPath. If that's the case then you were almost done:
$personData/Person[activityStatus="A"]/personDataCollection/personData[maritalStatus="Married"]

Can't get data with a xpath query

how could i get only the rows where the ProcedureID = 6104 in my xml database field?
<CDirData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://Fnet.ESB.Schemas.CentroDirectivo.CDirData">
<ProcedureData xmlns="">
<ProcedureId>6104</ProcedureId>
<CentroDirectivo>SGRP</CentroDirectivo>
</ProcedureData>
<SolicitudData xmlns="">
<SolicitudId>MFom635230432391710791</SolicitudId>
<Status>Iniciado</Status>
I've been trying something like
WITH XMLNAMESPACES (
'http://www.w3.org/2001/XMLSchema-instance' AS "xsi",
'http://www.w3.org/2001/XMLSchema' AS "xsd",
'http://Fnet.ESB.Schemas.CentroDirectivo.CDirData' AS "de")
SELECT [Message].value(
'(/de:CDirData/de:ProcedureData/de:ProcedureId)[1]', 'nvarch
but always returns null rows ...
Thanks in advance
The complication here is the default namespace defined at the root.
One workaround is to define your query in terms of local-name
//*[local-name()='ProcedureId' and text()='6104']

Resources