How to encode in XML on SQL Server - sql-server

I am generating XML as follows:
SET #xml = (
SELECT
PersonalMessage AS [Personal_Message]
FROM #TemporaryDB
FOR XML PATH ('Order'), ROOT ('Orders'), ELEMENTS
);
Next I add encoding info:
SELECT N'<?xml version="1.0" encoding="utf-8"?>' + Convert(nvarchar(max), #XML)
And when I save my XML with bcp I get something like this:
<?xml version="1.0" encoding="utf-8"?>
<Orders><Order>
<Personal_Message>Liebe X1 und X2,
"Wir wuenschten wir koennten heute mit euch zusammen sitzen. Das holen wir bald wieder nach."
Eure & Ina </Personal_Message></Order></Orders>
But I want something like this:
<?xml version="1.0" encoding="utf-8"?>
<Orders><Order>
<Personal_Message>Liebe X1 und X2,

 "Wir wuenschten wir koennten heute mit euch zusammen sitzen. Das holen wir bald wieder nach." 

Eure & Ina </Personal_Message></Order></Orders>
So I want to change C/R to 
 and for example " to ".
Please, advise me. How can I do this?

You might have to replace it
SET #xml = (
SELECT
Replace(Replace(Replace(PersonalMessage,char(13),'
'),char(10),''),'"','"') AS [Personal_Message]
FROM #TemporaryDB
FOR XML PATH ('Order'), ROOT ('Orders'), ELEMENTS
);

Related

How to read XML HTTP Response in SQL Server

I need help with reading some nodes in the below XML, it's an HTTP XML response which I store in a variable and need to read the values.
I want to read all the nodes that have the v31,v11,v12 namespace prefix into a table using XQuery
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:v1="http://xmlns.fl.com/InfoRequest/V1" xmlns:cor="http://soa.mic.co.af/coredata_1" xmlns:v3="http://xmlns.fl.com/RequestHeader/V3" xmlns:v2="http://xmlns.fl.com/ParameterType/V2">
<cor:SOATransactionID>96514584-be43-40f7-9335-b17f6d6669bd</cor:SOATransactionID>
</soapenv:Header>
<soapenv:Body xmlns:v1="http://xmlns.fl.com/InfoRequest/V1" xmlns:cor="http://soa.fl.co.af/coredata_1" xmlns:v3="http://xmlns.fl.com/RequestHeader/V3" xmlns:v2="http://xmlns.fl.com/ParameterType/V2">
<v11:InfoResponse xmlns:v11="http://xmlns.fl.com/InfoResponse/V1">
<v31:ResponseHeader xmlns:v31="http://xmlns.fl.com/ResponseHeader/V3">
<v31:GeneralResponse>
<v31:correlationID>AD-nXfJhT5ZMVEmKkut</v31:correlationID>
<v31:status>OK</v31:status>
<v31:code>Successfully-001</v31:code>
<v31:description>successfully.</v31:description>
</v31:GeneralResponse>
</v31:ResponseHeader>
<v11:responseBody>
<v11:msisdn>+149012098788</v11:msisdn>
<v11:customer>
<v12:name xmlns:v12="http://xmlns.fl.com/CustomerType/V1">FL Company</v12:name>
<v12:clientCode xmlns:v12="http://xmlns.fl.com/CustomerType/V1">5690001212</v12:clientCode>
<v12:firstName xmlns:v12="http://xmlns.fl.com/CustomerType/V1">FL Company</v12:firstName>
<v12:lastName xmlns:v12="http://xmlns.fl.com/CustomerType/V1">RA</v12:lastName>
<v12:birthDate xmlns:v12="http://xmlns.fl.com/CustomerType/V1">1997-08-07</v12:birthDate>
<v12:gender xmlns:v12="http://xmlns.fl.com/CustomerType/V1">Female</v12:gender>
<v12:address xmlns:v12="http://xmlns.fl.com/CustomerType/V1">example#fl.com</v12:address>
<v12:email xmlns:v12="http://xmlns.fl.com/CustomerType/V1">example#fl.com</v12:email>
<v12:nationality xmlns:v12="http://xmlns.fl.com/CustomerType/V1">233</v12:nationality>
<v12:clientGrade xmlns:v12="http://xmlns.fl.com/CustomerType/V1">CustomerLevel2</v12:clientGrade>
<v12:clientType xmlns:v12="http://xmlns.fl.com/CustomerType/V1">Company</v12:clientType>
<v12:clientState xmlns:v12="http://xmlns.fl.com/CustomerType/V1">Unspecified</v12:clientState>
<v12:admissionDate xmlns:v12="http://xmlns.fl.com/CustomerType/V1">2022-07-16T08:19:20</v12:admissionDate>
<v12:additionalProperties xmlns:v12="http://xmlns.fl.com/CustomerType/V1">
<v2:ParameterType>
<v2:parameterName>Value</v2:parameterName>
<v2:parameterValue>0.0</v2:parameterValue>
</v2:ParameterType>
</v12:additionalProperties>
</v11:customer>
<v11:subscriber>
<v12:imsi xmlns:v12="http://xmlns.fl.com/SubscriberType/V1">990423434534</v12:imsi>
<v12:clientCode xmlns:v12="http://xmlns.fl.com/SubscriberType/V1">7870559304234</v12:clientCode>
<v12:brandId xmlns:v12="http://xmlns.fl.com/SubscriberType/V1">120</v12:brandId>
<v12:language xmlns:v12="http://xmlns.fl.com/SubscriberType/V1">English</v12:language>
<v12:smsLanguage xmlns:v12="http://xmlns.fl.com/SubscriberType/V1">English</v12:smsLanguage>
<v12:ussdLanguage xmlns:v12="http://xmlns.fl.com/SubscriberType/V1">English</v12:ussdLanguage>
<v12:customerType xmlns:v12="http://xmlns.fl.com/SubscriberType/V1">Foreign</v12:customerType>
<v12:initialCredit xmlns:v12="http://xmlns.fl.com/SubscriberType/V1">30</v12:initialCredit>
<v12:mainProductID xmlns:v12="http://xmlns.fl.com/SubscriberType/V1">990877687</v12:mainProductID>
</v11:subscriber>
</v11:responseBody>
</v11:InfoResponse>
</soapenv:Body>
</soapenv:Envelope>
Please try the following.
The XML sample has lots of namespaces. You should be disciplined with them as well as with XPath expressions.
SQL
DECLARE #xml XML =
N'<?xml version="1.0"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:v1="http://xmlns.fl.com/InfoRequest/V1" xmlns:cor="http://soa.mic.co.af/coredata_1" xmlns:v3="http://xmlns.fl.com/RequestHeader/V3"
xmlns:v2="http://xmlns.fl.com/ParameterType/V2">
<cor:SOATransactionID>96514584-be43-40f7-9335-b17f6d6669bd</cor:SOATransactionID>
</soapenv:Header>
<soapenv:Body xmlns:v1="http://xmlns.fl.com/InfoRequest/V1" xmlns:cor="http://soa.fl.co.af/coredata_1" xmlns:v3="http://xmlns.fl.com/RequestHeader/V3" xmlns:v2="http://xmlns.fl.com/ParameterType/V2">
<v11:InfoResponse xmlns:v11="http://xmlns.fl.com/InfoResponse/V1">
<v31:ResponseHeader xmlns:v31="http://xmlns.fl.com/ResponseHeader/V3">
<v31:GeneralResponse>
<v31:correlationID>AD-nXfJhT5ZMVEmKkut</v31:correlationID>
<v31:status>OK</v31:status>
<v31:code>Successfully-001</v31:code>
<v31:description>successfully.</v31:description>
</v31:GeneralResponse>
</v31:ResponseHeader>
<v11:responseBody>
<v11:msisdn>+149012098788</v11:msisdn>
<v11:customer>
<v12:name xmlns:v12="http://xmlns.fl.com/CustomerType/V1">FL Company</v12:name>
<v12:clientCode xmlns:v12="http://xmlns.fl.com/CustomerType/V1">5690001212</v12:clientCode>
<v12:firstName xmlns:v12="http://xmlns.fl.com/CustomerType/V1">FL Company</v12:firstName>
<v12:lastName xmlns:v12="http://xmlns.fl.com/CustomerType/V1">RA</v12:lastName>
<v12:birthDate xmlns:v12="http://xmlns.fl.com/CustomerType/V1">1997-08-07</v12:birthDate>
<v12:gender xmlns:v12="http://xmlns.fl.com/CustomerType/V1">Female</v12:gender>
<v12:address xmlns:v12="http://xmlns.fl.com/CustomerType/V1">example#fl.com</v12:address>
<v12:email xmlns:v12="http://xmlns.fl.com/CustomerType/V1">example#fl.com</v12:email>
<v12:nationality xmlns:v12="http://xmlns.fl.com/CustomerType/V1">233</v12:nationality>
<v12:clientGrade xmlns:v12="http://xmlns.fl.com/CustomerType/V1">CustomerLevel2</v12:clientGrade>
<v12:clientType xmlns:v12="http://xmlns.fl.com/CustomerType/V1">Company</v12:clientType>
<v12:clientState xmlns:v12="http://xmlns.fl.com/CustomerType/V1">Unspecified</v12:clientState>
<v12:admissionDate xmlns:v12="http://xmlns.fl.com/CustomerType/V1">2022-07-16T08:19:20</v12:admissionDate>
<v12:additionalProperties xmlns:v12="http://xmlns.fl.com/CustomerType/V1">
<v2:ParameterType>
<v2:parameterName>Value</v2:parameterName>
<v2:parameterValue>0.0</v2:parameterValue>
</v2:ParameterType>
</v12:additionalProperties>
</v11:customer>
<v11:subscriber>
<v12:imsi xmlns:v12="http://xmlns.fl.com/SubscriberType/V1">990423434534</v12:imsi>
<v12:clientCode xmlns:v12="http://xmlns.fl.com/SubscriberType/V1">7870559304234</v12:clientCode>
<v12:brandId xmlns:v12="http://xmlns.fl.com/SubscriberType/V1">120</v12:brandId>
<v12:language xmlns:v12="http://xmlns.fl.com/SubscriberType/V1">English</v12:language>
<v12:smsLanguage xmlns:v12="http://xmlns.fl.com/SubscriberType/V1">English</v12:smsLanguage>
<v12:ussdLanguage xmlns:v12="http://xmlns.fl.com/SubscriberType/V1">English</v12:ussdLanguage>
<v12:customerType xmlns:v12="http://xmlns.fl.com/SubscriberType/V1">Foreign</v12:customerType>
<v12:initialCredit xmlns:v12="http://xmlns.fl.com/SubscriberType/V1">30</v12:initialCredit>
<v12:mainProductID xmlns:v12="http://xmlns.fl.com/SubscriberType/V1">990877687</v12:mainProductID>
</v11:subscriber>
</v11:responseBody>
</v11:InfoResponse>
</soapenv:Body>
</soapenv:Envelope>';
;WITH XMLNAMESPACES(DEFAULT 'http://schemas.xmlsoap.org/soap/envelope/'
, 'http://xmlns.fl.com/InfoResponse/V1' AS v11
, 'http://xmlns.fl.com/CustomerType/V1' AS v12
, 'http://xmlns.fl.com/SubscriberType/V1' AS v121)
SELECT r.value('(v11:msisdn/text())[1]','varchar(50)') AS msisdn
, c.value('(v12:name/text())[1]','varchar(50)') AS custname
, c.value('(v12:clientCode/text())[1]','varchar(50)') AS clientCode
, s.value('(v121:customerType/text())[1]','varchar(50)') AS customerType
, s.value('(v121:initialCredit/text())[1]','INT') AS initialCredit
FROM #XML.nodes('/Envelope/Body/v11:InfoResponse/v11:responseBody') AS t1(r)
CROSS APPLY t1.r.nodes('v11:customer') AS t2(c)
CROSS APPLY t1.r.nodes('v11:subscriber') AS t3(s);
Output
+---------------+------------+------------+--------------+---------------+
| msisdn | custname | clientCode | customerType | initialCredit |
+---------------+------------+------------+--------------+---------------+
| +149012098788 | FL Company | 5690001212 | Foreign | 30 |
+---------------+------------+------------+--------------+---------------+

Grabbing sublevel elements xml

I Have an XML conaining elemens as beeing an shipment and elements as beeein cargo-lines of that shipping.
In 1 file are multiple elements with 1 or more elements.
I need to create an XML file in another format.
Needed format:
<shipment>
--shipment lines
<cargo>
--cargo line
</cargo>
<cargo>
--cargo line
</cargo>
<cargo>
--cargo line
</cargo>
</shipment>
<shipment>
--shipment lines
<cargo>
--cargo line
</cargo>
</shipment>
Current SQL:
set #shipmentsXML = (SELECT
"customer_id" = T.c.query('./ediCustomerNumber').value('.', 'varchar(50)'),
"reference" = T.c.query('./ediReference').value('.', 'varchar(50)'),
(select
"unitamount" = T.C.query('./quantity').value('.', 'varchar(50)'),
"weight" = T.c.query('./grossWeight').value('.', 'varchar(50)'),
"loadingmeter" = T.c.query('./loadingMeters').value('.', 'varchar(50)')
FROM #Xml.nodes('./file/goodsLine') T(c)
FOR XML PATH ('cargo'), TYPE)
FROM #Xml.nodes('manifest-out/consol') T(c)
FOR XML PATH ('shipment')
)
working with the console tags, but no cargo line is showing
source XML:
<manifest-out type="tag">
<ediCustomerNumber>*******</ediCustomerNumber>
<ediCustomerDepartment>Ic</ediCustomerDepartment>
<transmitter>R</transmitter>
<receiver>*******</receiver>
<ediReference>*******</ediReference>
<referenceIndication>0</referenceIndication>
<internalShipmentNumber>*******</internalShipmentNumber>
<ediFunction1>6</ediFunction1>
<dateTimeZone>2019-07-24T13:05:55+02:00</dateTimeZone>
<fileHeader type="tag">
</fileHeader>
<consol type="tag">
<file type="tag">
<operationalPeriod>2019/07</operationalPeriod>
<loadingDate>2019-07-17</loadingDate>
<loadingTime>00:00:00</loadingTime>
<unloadingDate>2019-07-26</unloadingDate>
<unloadingTime>17:00:00</unloadingTime>
<primaryReference>8017883827</primaryReference>
<deliveryTerm>DAP</deliveryTerm>
<codeShedHandling>true</codeShedHandling>
<goodsLine type="tag">
<quantity>3.000</quantity>
<grossWeight>415.000</grossWeight>
<loadingMeters>1.633</loadingMeters>
</goodsLine>
<goodsLine type="tag">
<quantity>1.000</quantity>
<grossWeight>605.000</grossWeight>
<loadingMeters>4.633</loadingMeters>
</goodsLine>
<goodsLine type="tag">
<quantity>2.000</quantity>
<grossWeight>75.000</grossWeight>
<loadingMeters>2.633</loadingMeters>
</goodsLine>
</file>
</consol>
<consol type="tag">
<file type="tag">
<operationalPeriod>2019/07</operationalPeriod>
<loadingDate>2019-07-17</loadingDate>
<loadingTime>00:00:00</loadingTime>
<unloadingDate>2019-07-26</unloadingDate>
<unloadingTime>17:00:00</unloadingTime>
<primaryReference>8017883827</primaryReference>
<deliveryTerm>DAP</deliveryTerm>
<codeShedHandling>true</codeShedHandling>
<goodsLine type="tag">
<quantity>3.000</quantity>
<grossWeight>415.000</grossWeight>
<loadingMeters>1.633</loadingMeters>
</goodsLine>
</file>
</consol>
</manifest-out>
Could someone point me in the right direction on selecting the goodlines
Your question is missing the expected output, at least, it is not clear to me... But I hope I guessed this correctly:
SELECT #xml.value('(/manifest-out/ediCustomerNumber/text())[1]','varchar(50)') AS customer_id
,#xml.value('(/manifest-out/ediReference/text())[1]','varchar(50)') AS reference
,(
SELECT gl.value('(quantity/text())[1]','decimal(14,10)') AS unitamount
,gl.value('(grossWeight/text())[1]','decimal(14,10)') AS [weight]
,gl.value('(loadingMeters/text())[1]','decimal(14,10)') AS loadingmeter
FROM #xml.nodes('/manifest-out/consol/file/goodsLine') A(gl)
FOR XML PATH('cargo'),TYPE
)
FOR XML PATH('shipment');
The result
<shipment>
<customer_id>*******</customer_id>
<reference>*******</reference>
<cargo>
<unitamount>3.0000000000</unitamount>
<weight>415.0000000000</weight>
<loadingmeter>1.6330000000</loadingmeter>
</cargo>
<cargo>
<unitamount>1.0000000000</unitamount>
<weight>605.0000000000</weight>
<loadingmeter>4.6330000000</loadingmeter>
</cargo>
<cargo>
<unitamount>2.0000000000</unitamount>
<weight>75.0000000000</weight>
<loadingmeter>2.6330000000</loadingmeter>
</cargo>
</shipment>
The idea in short:
We can pick the 1:1 related values (your shipment data) directly from the XML and return it within <shipment>.
The nested SELECT will pick the repeating elements below <goodsLine> and return each of them within <cargo>.
Btw: Your own code was very, very close...
It would work if you either change your inner XPath to /manifest-out/consol/file/goodsLine or if you use T.c.nodes(). You are trying to use the relative path ./file/goodsLine against the source XML. You might even go the deep search route with FROM #xml.nodes('//goodsLine') T(c), but the general advise is: Be as specific as possible.
XQuery FLWOR expression is the best way to re-shape XML. There is no need to convert (1) XML data type into relational data, and after that (2) back to XML.
Check it out.
SQL
DECLARE #xml XML = '<manifest-out type="tag">
<ediCustomerNumber>*******</ediCustomerNumber>
<ediCustomerDepartment>Ic</ediCustomerDepartment>
<transmitter>R</transmitter>
<receiver>*******</receiver>
<ediReference>*******</ediReference>
<referenceIndication>0</referenceIndication>
<internalShipmentNumber>*******</internalShipmentNumber>
<ediFunction1>6</ediFunction1>
<dateTimeZone>2019-07-24T13:05:55+02:00</dateTimeZone>
<fileHeader type="tag">
</fileHeader>
<consol type="tag">
<file type="tag">
<operationalPeriod>2019/07</operationalPeriod>
<loadingDate>2019-07-17</loadingDate>
<loadingTime>00:00:00</loadingTime>
<unloadingDate>2019-07-26</unloadingDate>
<unloadingTime>17:00:00</unloadingTime>
<primaryReference>8017883827</primaryReference>
<deliveryTerm>DAP</deliveryTerm>
<codeShedHandling>true</codeShedHandling>
<goodsLine type="tag">
<quantity>3.000</quantity>
<grossWeight>415.000</grossWeight>
<loadingMeters>1.633</loadingMeters>
</goodsLine>
<goodsLine type="tag">
<quantity>1.000</quantity>
<grossWeight>605.000</grossWeight>
<loadingMeters>4.633</loadingMeters>
</goodsLine>
<goodsLine type="tag">
<quantity>2.000</quantity>
<grossWeight>75.000</grossWeight>
<loadingMeters>2.633</loadingMeters>
</goodsLine>
</file>
</consol>
</manifest-out>';
SELECT #xml.query('<shipment>
<customerID>{data(/manifest-out/ediCustomerNumber)}</customerID>
<CustomerDepartment>{data(/manifest-out/ediCustomerDepartment)}</CustomerDepartment>
{
for $x in /manifest-out/consol/file/goodsLine
return <cargo>
<unitamount>{data($x/quantity)}</unitamount>
<weight>{data($x/grossWeight)}</weight>
<loadingmeter>{data($x/loadingMeters)}</loadingmeter>
</cargo>
}
</shipment>');
Output XML:
<shipment>
<customerID>*******</customerID>
<CustomerDepartment>Ic</CustomerDepartment>
<cargo>
<unitamount>3.000</unitamount>
<weight>415.000</weight>
<loadingmeter>1.633</loadingmeter>
</cargo>
<cargo>
<unitamount>1.000</unitamount>
<weight>605.000</weight>
<loadingmeter>4.633</loadingmeter>
</cargo>
<cargo>
<unitamount>2.000</unitamount>
<weight>75.000</weight>
<loadingmeter>2.633</loadingmeter>
</cargo>
</shipment>
I found the solution:
SELECT g2.value('(/manifest-out/ediCustomerNumber/text())[1]','varchar(50)') AS customer_id
,g2.value('(/manifest-out/ediReference/text())[1]','varchar(50)') AS reference
,(
SELECT gl.value('(quantity/text())[1]','decimal(14,10)') AS unitamount
,gl.value('(grossWeight/text())[1]','decimal(14,10)') AS [weight]
,gl.value('(loadingMeters/text())[1]','decimal(14,10)') AS loadingmeter
FROM g2.nodes('./file/goodsLine') A(gl)
FOR XML PATH('cargo'),TYPE
)
FROM #xml.nodes('/manifest-out/consol') B(g2)
FOR XML PATH('shipment');
thnx for the help and suggestions, they help me finding the sollution!
As I already mentioned earlier there is no need (1) to convert XML data type into relational data, (2) CAST values into particular SQL Server data types, and after that (3) convert back to XML.
Here is an updated SQL with XQuery based on the refined requirements.
SELECT #xml.query('<root>
{
for $x in /manifest-out/consol
return <shipment>
<customerID>{data(/manifest-out/ediCustomerNumber)}</customerID>
<CustomerDepartment>{data(/manifest-out/ediCustomerDepartment)}</CustomerDepartment>
{
for $c in $x/file/goodsLine
return <cargo>
<unitamount>{data($c/quantity)}</unitamount>
<weight>{data($c/grossWeight)}</weight>
<loadingmeter>{data($c/loadingMeters)}</loadingmeter>
</cargo>
}
</shipment>
}
</root>');

read Xml into ms sql

I want to read the xml pasted below into sql. I tried several ways from the web to parse this xml without success.
I tried the following script:
CREATE TABLE [dbo].[Ejemplo2](
[RutEmisor][nvarchar](12) null,
[RutEnvia][nvarchar](12) null,
[RutReceptor][nvarchar](12) null,
[FchResol][nvarchar](12) null,
[NroResol][nvarchar](12) null
)
GO
DECLARE #messagebody XML
SELECT #messagebody = BulkColumn
FROM OPENROWSET(BULK 'C:\Ejemplo.xml', SINGLE_CLOB) AS X
INSERT INTO [dbo].[Ejemplo2]
select a.value(N'(./RutEmisor)[1]', N'nvarchar(12)') as [RutEmisor],
a.value(N'(./RutEnvia)[1]', N'nvarchar(12)') as [RutEnvia],
a.value(N'(./RutReceptor)[1]', N'nvarchar(12)') as [RutReceptor],
a.value(N'(./FchResol)[1]', N'nvarchar(12)') as [FchResol],
a.value(N'(./NroResol)[1]', N'nvarchar(12)') as [NroResol]
from #messagebody.nodes('/EnvioDTE/SetDTE/Caratula') as r(a);
Select * from dbo.Ejemplo2
But I get 0 rows
I tried with more simple examples and always return the data, but in this case I can't figure whats is wrong.
Any help on this will be appreciated.
The xml follows
<?xml version="1.0" encoding="ISO-8859-1"?>
<EnvioDTE xmlns="http://www.sii.cl/SiiDte" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sii.cl/SiiDte EnvioDTE_v10.xsd" version="1.0">
<SetDTE ID="SetDoc">
<TmstFirmaEnv>2003-10-13T09:33:22</TmstFirmaEnv>
<Caratula version="1.0">
<RutEmisor>97975000-5</RutEmisor>
<RutEnvia>7880442-4</RutEnvia>
<RutReceptor>60803000-K</RutReceptor>
<FchResol>2003-09-02</FchResol>
<NroResol>0</NroResol>
<SubTotDTE>
<TpoDTE>33</TpoDTE>
<NroDTE>1</NroDTE>
</SubTotDTE>
</Caratula>
<DTE version="1.0">
<Documento ID="F60T33">
<Encabezado>
<IdDoc>
<TipoDTE>33</TipoDTE>
<Folio>60</Folio>
<FchEmis>2003-10-13</FchEmis>
</IdDoc>
<Emisor>
<RUTEmisor>97975000-5</RUTEmisor>
<RznSoc>RUT DE PRUEBA</RznSoc>
<GiroEmis>Insumos de Computacion</GiroEmis>
<Acteco>31341</Acteco>
<CdgSIISucur>1234</CdgSIISucur>
<DirOrigen>Teatinos 120, Piso 4</DirOrigen>
<CmnaOrigen>Santiago</CmnaOrigen>
<CiudadOrigen>Santiago</CiudadOrigen>
</Emisor>
<Receptor>
<RUTRecep>77777777-7</RUTRecep>
<RznSocRecep>EMPRESA LTDA</RznSocRecep>
<GiroRecep>COMPUTACION</GiroRecep>
<DirRecep>SAN DIEGO 2222</DirRecep>
<CmnaRecep>LA FLORIDA</CmnaRecep>
<CiudadRecep>SANTIAGO</CiudadRecep>
</Receptor>
<Totales>
<MntNeto>100000</MntNeto>
<TasaIVA>19</TasaIVA>
<IVA>19000</IVA>
<MntTotal>119000</MntTotal>
</Totales>
</Encabezado>
<Detalle>
<NroLinDet>1</NroLinDet>
<CdgItem>
<TpoCodigo>INT1</TpoCodigo>
<VlrCodigo>011</VlrCodigo>
</CdgItem>
<NmbItem>Parlantes Multimedia 180W.</NmbItem>
<DscItem/>
<QtyItem>20</QtyItem>
<PrcItem>4500</PrcItem>
<MontoItem>90000</MontoItem>
</Detalle>
<Detalle>
<NroLinDet>2</NroLinDet>
<CdgItem>
<TpoCodigo>INT1</TpoCodigo>
<VlrCodigo>0231</VlrCodigo>
</CdgItem>
<NmbItem>Mouse Inalambrico PS/2</NmbItem>
<DscItem/>
<QtyItem>1</QtyItem>
<PrcItem>5000</PrcItem>
<MontoItem>5000</MontoItem>
</Detalle>
<Detalle>
<NroLinDet>3</NroLinDet>
<CdgItem>
<TpoCodigo>INT1</TpoCodigo>
<VlrCodigo>1515</VlrCodigo>
</CdgItem>
<NmbItem>Caja de Diskettes 10 Unidades</NmbItem>
<DscItem/>
<QtyItem>5</QtyItem>
<PrcItem>1000</PrcItem>
<MontoItem>5000</MontoItem>
</Detalle>
<TED version="1.0">
<DD>
<RE>97975000-5</RE>
<TD>33</TD>
<F>60</F>
<FE>2003-10-13</FE>
<RR>77777777-7</RR>
<RSR>EMPRESA LTDA</RSR>
<MNT>119000</MNT>
<IT1>Parlantes Multimedia 180W.</IT1>
<CAF version="1.0">
<DA>
<RE>97975000-5</RE>
<RS>RUT DE PRUEBA</RS>
<TD>33</TD>
<RNG>
<D>1</D>
<H>200</H>
</RNG>
<FA>2003-09-04</FA>
<RSAPK>
<M>0a4O6Kbx8Qj3K4iWSP4w7KneZYeJ+g/prihYtIEolKt3cykSxl1zO8vSXu397QhTmsX7SBEudTUx++2zDXBhZw==</M>
<E>Aw==</E>
</RSAPK>
<IDK>100</IDK>
</DA>
<FRMA algoritmo="SHA1withRSA">g1AQX0sy8NJugX52k2hTJEZAE9Cuul6pqYBdFxj1N17umW7zG/hAavCALKByHzdYAfZ3LhGTXCai5zNxOo4lDQ==</FRMA>
</CAF>
<TSTED>2003-10-13T09:33:20</TSTED>
</DD>
<FRMT algoritmo="SHA1withRSA">GbmDcS9e/jVC2LsLIe1iRV12Bf6lxsILtbQiCkh6mbjckFCJ7fj/kakFTS06Jo8i
S4HXvJj3oYZuey53Krniew==</FRMT>
</TED>
<TmstFirma>2003-10-13T09:33:20</TmstFirma>
</Documento>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
<SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
<Reference URI="#F60T33">
<Transforms>
<Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
<DigestValue>hlmQtu/AyjUjTDhM3852wvRCr8w=</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>JG1Ig0pvSIH85kIKGRZUjkyX6CNaY08Y94j4UegTgDe8+wl61GzqjdR1rfOK9BGn93AMOo6aiAgolW0k/XklNVtM/ZzpNNS3d/fYVa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FXZp78
q7mbdHijzKqvMmyvwbdP7KK8LQfwf84W4v9O8MJeUHlbJGlo5nFACrPAeTtONbHa
ReyzeMDv2EganNEDJc9c+UNfAgMBAAGjggGYMIIBlDAjBgNVHREEHDAaoBgGCCsG
AQQBwQEBoAwWCjA3ODgwNDQyLTQwCQYDVR0TBAIwADA8BgNVHR8ENTAzMDGgL6At
hitodHRwOi8vY3JsLmUtY2VydGNoaWxlLmNsL2UtY2VydGNoaWxlY2EuY3JsMCMG
A1UdEgQcMBqgGAYIKwYBBAHBAQKgDBYKOTY5MjgxODAtNTAfBgNVHSMEGDAWgBTg
KP3S4GBPs0brGsz1CJEHcjodCDCB0AYDVR0gBIHIMIHFMIHCBggrBgEEAcNSBTCB
tTAvBggrBgEFBQcCARYjaHR0cDovL3d3dy5lLWNlcnRjaGlsZS5jbC8yMDAwL0NQ
Uy8wgYEGCCsGAQUFBwICMHUac0VsIHRpdHVsYXIgaGEgc2lkbyB2YWxpZG8gZW4g
Zm9ybWEgcHJlc2VuY2lhbCwgcXVlZGFuZG8gZWwgQ2VydGlmaWNhZG8gcGFyYSB1
c28gdHJpYnV0YXJpbywgcGFnb3MsIGNvbWVyY2lvIHkgb3Ryb3MwCwYDVR0PBAQD
AgTwMA0GCSqGSIb3DQEBBQUAA4GBABMfCyJF0mNXcov8iEWvjGFyyPTsXwvsYbbk
OJ41wjaGOFMCInb4WY0ngM8BsDV22bGMs8oLyX7rVy16bGA8Z7WDUtYhoOM7mqXw
/Hrpqjh3JgAf8zqdzBdH/q6mAbdvq/yb04JHKWPC7fMFuBoeyVWAnhmuMZfReWQi
MUEHGGIW</X509Certificate>
</X509Data>
</KeyInfo>
</Signature></DTE>
</SetDTE><Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
<SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
<Reference URI="#SetDoc">
<Transforms>
<Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
<DigestValue>4OTWXyRl5fw3htjTyZXQtYEsC3E=</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>sBnr8Yq14vVAcrN/pKLD/BrqUFczKMW3y1t3JOrdsxhhq6IxvS13SgyMXbIN/T9ciRaFgNabs3pi732XhcpeiSmD1ktzbRctEbSIszYkFJY49k0eB+TVzq3eVaQr4INrymfuOnWj78BZcwKuXvDy4iAcx6/TBbAAkPFwMP9ql2s=</SignatureValue>
<KeyInfo>
<KeyValue>
<RSAKeyValue>
<Modulus>
tNEknkb1kHiD1OOAWlLKkcH/UP5UGa6V6MYso++JB+vYMg2OXFROAF7G8BNFFPQx
iuS/7y1azZljN2xq+bW3bAou1bW2ij7fxSXWTJYFZMAyndbLyGHM1e3nVmwpgEpx
BHhZzPvwLb55st1wceuKjs2Ontb13J33sUb7bbJMWh0=
</Modulus>
<Exponent>
AQAB
</Exponent>
</RSAKeyValue>
</KeyValue>
<X509Data>
<X509Certificate>MIIEgjCCA+ugAwIBAgIEAQAApzANBgkqhkiG9w0BAQUFADCBtTELMAkGA1UEBhMC
Q0wxHTAbBgNVBAgUFFJlZ2lvbiBNZXRyb3BvbGl0YW5hMREwDwYDVQQHFAhTYW50
aWFnbzEUMBIGA1UEChQLRS1DRVJUQ0hJTEUxIDAeBgNVBAsUF0F1dG9yaWRhZCBD
ZXJ0aWZpY2Fkb3JhMRcwFQYDVQQDFA5FLUNFUlRDSElMRSBDQTEjMCEGCSqGSIb3
DQEJARYUZW1haWxAZS1jZXJ0Y2hpbGUuY2wwHhcNMDMxMDAxMTg1ODE1WhcNMDQw
OTMwMDAwMDAwWjCBuDELMAkGA1UEBhMCQ0wxFjAUBgNVBAgUDU1ldHJvcG9saXRh
bmExETAPBgNVBAcUCFNhbnRpYWdvMScwJQYDVQQKFB5TZXJ2aWNp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3JgAf8zqdzBdH/q6mAbdvq/yb04JHKWPC7fMFuBoeyVWAnhmuMZfReWQi
MUEHGGIW</X509Certificate>
</X509Data>
</KeyInfo>
</Signature></EnvioDTE>
This Works. You add "*:" for the nodes.
DECLARE #messagebody XML
SELECT #messagebody = BulkColumn
FROM OPENROWSET(BULK 'C:\Ejemplo.xml', SINGLE_CLOB) AS X
PRINT CONVERT(VARCHAR(MAX),#messagebody)
INSERT INTO [dbo].[Ejemplo24]
select a.value(N'(./*:RutEmisor)[1]', N'nvarchar(12)') as [RutEmisor],
a.value(N'(./*:RutEnvia)[1]', N'nvarchar(12)') as [RutEnvia],
a.value(N'(./*:RutReceptor)[1]', N'nvarchar(12)') as [RutReceptor],
a.value(N'(./*:FchResol)[1]', N'nvarchar(12)') as [FchResol],
a.value(N'(./*:NroResol)[1]', N'nvarchar(12)') as [NroResol]
from #messagebody.nodes('/*:EnvioDTE/*:SetDTE/*:Caratula') as r(a);
Your query is great, but you must specify the default namespace xmlns
Try like this:
WITH XMLNAMESPACES(DEFAULT 'http://www.sii.cl/SiiDte')
INSERT INTO [dbo].[Ejemplo2]
select a.value(N'(./RutEmisor)[1]', N'nvarchar(12)') as [RutEmisor],
a.value(N'(./RutEnvia)[1]', N'nvarchar(12)') as [RutEnvia],
a.value(N'(./RutReceptor)[1]', N'nvarchar(12)') as [RutReceptor],
a.value(N'(./FchResol)[1]', N'nvarchar(12)') as [FchResol],
a.value(N'(./NroResol)[1]', N'nvarchar(12)') as [NroResol]
from #messagebody.nodes('/EnvioDTE/SetDTE/Caratula') as r(a);

Unable to add a row to Solr

Im trying to add a row to the Solr Index but it does not get added.
I get a response but not sure what to infer from the response.
What information the response provides here? How do I get the exception?
>>> c = SolrConnection('http://localhost:8983/solr')
>>> c
<SolrConnection (url=http://localhost:8983/solr, persistent=True, post_headers={'Content-Type': 'text/xml; charset=utf-8'}, reconnects=0)>
>>> l = [{'document_type': 'demo', 'id': 'demo11234', 'deco_name': 'test'}]
>>> c.add_many(l)
'<?xml version="1.0" encoding="UTF-8"?>\n<response>\n<lst name="responseHeader"><int name="status">0</int><int name="QTime">1</int></lst>\n</response>\n'
>>> try:
... c.add_many(l)
... except:
... print "error"
...
'<?xml version="1.0" encoding="UTF-8"?>\n<response>\n<lst name="responseHeader"><int name="status">0</int><int name="QTime">2</int></lst>\n</response>\n'

How to authenticate user in ONVIF?

we have network IP camera that supports ONVIF protocol. When I tried to get its PTZ configuration it gives Auth error. I am implementing this in C. Following are the request and response.
Request:
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\""
"xmlns:tds=\"http://www.onvif.org/ver20/ptz/wsdl\">"
"<soap:Body>"
"<tds:GetNodes/>"
"</soap:Body>"
"</soap:Envelope>"
Response:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope"
xmlns:SOAP-ENC="http://www.w3.org/2003/05/soap-encoding"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:wsa5="http://www.w3.org/2005/08/addressing"
xmlns:c14n="http://www.w3.org/2001/10/xml-exc-c14n#"
xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
xmlns:ptzimg2="http://www.onvif.org/ver10/schema"
xmlns:ptzimg3="http://www.w3.org/2005/05/xmlmime"
xmlns:ptzimg4="http://docs.oasis-open.org/wsn/b-2"
xmlns:ptzimg5="http://docs.oasis-open.org/wsrf/bf-2"
xmlns:ptzimg6="http://docs.oasis-open.org/wsn/t-1"
xmlns:ptzimg1="http://www.onvif.org/ver20/ptz/wsdl"
xmlns:ptzimg7="http://www.onvif.org/ver20/imaging/wsdl"
xmlns:ter="http://www.onvif.org/ver10/error">
<SOAP-ENV:Body>
<SOAP-ENV:Fault>
<SOAP-ENV:Code>
<SOAP-ENV:Value>
SOAP-ENV:Sender
</SOAP-ENV:Value>
<SOAP-ENV:Subcode>
<SOAP-ENV:Value>
ter:NotAuthorized
</SOAP-ENV:Value>
</SOAP-ENV:Subcode>
</SOAP-ENV:Code>
<SOAP-ENV:Reason>
<SOAP-ENV:Text xml:lang="en">
Sender Not Authorized
</SOAP-ENV:Text>
</SOAP-ENV:Reason>
<SOAP-ENV:Node>
http://www.w3.org/2003/05/soap-envelope/node/ultimateReceiver
</SOAP-ENV:Node>
<SOAP-ENV:Role>
http://www.w3.org/2003/05/soap-envelope/role/ultimateReceiver
</SOAP-ENV:Role>
<SOAP-ENV:Detail>
The action requested requires authorization and the sender is not authorized
</SOAP-ENV:Detail>
</SOAP-ENV:Fault>
</SOAP-ENV:Body>
How can I authenticate user? Thanks
those commands which required authentication. Their authentication headers can be added like this.
snprintf(postData, sizeof(postData),
"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://www.w3.org/2003/05/soap-envelope\""
"xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401"
"-wss-wssecurity-secext-1.0.xsd\""
"xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-"
"200401-wss-wssecurity-utility-1.0.xsd\""
"xmlns:tds=\"http://www.onvif.org/ver20/ptz/wsdl\">"
"<SOAP-ENV:Header><wsse:Security><wsse:UsernameToken>"
"<wsse:Username>%s</wsse:Username>"
"<wsse:Password Type=\"http://docs.oasis-open.org/wss/2004/01/oasis-"
"200401-wss-username-token-profile-1.0#PasswordDigest\">"
"%s</wsse:Password><wsse:Nonce>%s</wsse:Nonce>"
"<wsu:Created>%s</wsu:Created></wsse:UsernameToken>"
"</wsse:Security></SOAP-ENV:Header><SOAP-ENV:Body>"
"<tds:GetNodes>"
"</SOAP-ENV:Body></SOAP-ENV:Envelope>",
username, base64EncDigest, nonce_char, time_char);
You can use gSoap with WSSE plugin to generate C++ client proxy from OnVif WDSL that you require.
Do that greatly simplified my work. Here is an example of calling GetVideoEncoderConfiguration and reading response.
_media__GetVideoEncoderConfigurations query;
_media__GetVideoEncoderConfigurationsResponse response;
soap_wsse_add_Security(&mediaProxy);
soap_wsse_add_UsernameTokenDigest(&mediaProxy, NULL, m_username.c_str(), m_password.c_str());
if(mediaProxy.GetVideoEncoderConfigurations(&query, &response) == SOAP_OK)
{
LogSuccess("GetVideoEncoderConfigurations");
for(auto it = response.Configurations.begin(); it != response.Configurations.end(); ++it)
{
onvif__VideoEncoderConfiguration* videoConf = *it;
log(I3) << "Name= " << videoConf->Name << ", Encoding=" << videoConf->Encoding << ", Resolution=" << videoConf->Resolution->Width << "x" << videoConf->Resolution->Height;
}
}
else
LogError("GetVideoEncoderConfigurations", soap_faultdetail(&mediaProxy));
So no manually creating soap messages. gSOAP generated code takes care of that in lower layer. Of course, it takes mi 2 days to generate working code with wsse support, but still it was probably 10 times faster the to do it manualy. If you are interested in further information you can contact me.

Resources