Modify xml element name in SQL Server - sql

How to change element name from Cust to Customer?
<Cust id="1">
<Name>aaaaaaaaaa</Name>
<Desc>bbbbbbbbbb</Desc>
</Cust>
When I'm using following statement
select #myXml.query('/node()[1]/node()') for xml raw('Customer')
sql removes attributes
<Customer>
<Name>aaaaaaaaaa</Name>
<Desc>bbbbbbbbbb</Desc>
</Customer>

Try this:
SELECT
#myXml.value('(/Cust/#id)[1]', 'int') AS '#id',
#myXml.query('/node()[1]/node()')
FOR XML PATH('Customer')
Gives me an output of:
<Customer id="1">
<Name>aaaaaaaaaa</Name>
<Desc>bbbbbbbbbb</Desc>
</Customer>
With the FOR XML PATH, you can fairly easily "restore" that attribute that gets lost in the conversion.

You could use replace:
replace(replace(#YourXml, '<Cust id', '<Customer id)', '</Cust>', '</Customer>')
This is fairly safe, as < is not valid as data in XML, it would appear as < or an ASCII or UNICODE sequence.

Related

Add data to result of FOR XML PATH SQL SERVER

I have a query that produces XML, exactly as I want it:
SELECT *
FROM [staging].[vw_mydata]
FOR XML PATH('DOCUMENT')
This code produces this output:
<DOCUMENT>
<BRON>Bulk Import</BRON>
<INDEXEERDATUM>2015-07-30</INDEXEERDATUM>
<ITEMVALUE1>ITEMVALUE1</ITEMVALUE1>
<ITEMVALUE2>ITEMVALUE2</ITEMVALUE2>
<ITEMVALUE3>ITEMVALUE3</ITEMVALUE3>
<ITEMVALUE4>ITEMVALUE4</ITEMVALUE4>
<ITEMVALUE5>ITEMVALUE5</ITEMVALUE5>
<ITEMVALUE6>2015-07-30</ITEMVALUE6>
<ITEMVALUE7>ITEMVALUE7</ITEMVALUE7>
<ITEMVALUE8></ITEMVALUE8>
<ITEMVALUE9></ITEMVALUE9>
<ITEMVALUE10></ITEMVALUE10>
<ITEMVALUE11>ITEMVALUE11</ITEMVALUE11>
</DOCUMENT>
<BRON>Bulk Import</BRON>
<INDEXEERDATUM>2015-07-30</INDEXEERDATUM>
<ITEMVALUE1>ITEMVALUE1</ITEMVALUE1>
<ITEMVALUE2>ITEMVALUE2</ITEMVALUE2>
<ITEMVALUE3>ITEMVALUE3</ITEMVALUE3>
<ITEMVALUE4>ITEMVALUE4</ITEMVALUE4>
<ITEMVALUE5>ITEMVALUE5</ITEMVALUE5>
<ITEMVALUE6>2015-07-30</ITEMVALUE6>
<ITEMVALUE7>ITEMVALUE7</ITEMVALUE7>
<ITEMVALUE8></ITEMVALUE8>
<ITEMVALUE9></ITEMVALUE9>
<ITEMVALUE10></ITEMVALUE10>
<ITEMVALUE11>ITEMVALUE11</ITEMVALUE11>
</DOCUMENT>
However, I need to add information before and after this result.
The following has to preceed the results from the query. This part is fixed and does not change.
<?xml version="1.0"?>
<DOCUMENTS>
<VERSION>2.0</VERSION>
<LICENTIEHOUDER>Henrov</LICENTIEHOUDER>
<XTN>BulkImport</XTN>
<ARCHIEFNAAM>Name</ARCHIEFNAAM>
<ITEMLABEL1>Label1</ITEMLABEL1>
<ITEMLABEL2>Label2</ITEMLABEL2>
<ITEMLABEL3>Label3</ITEMLABEL3>
<ITEMLABEL4>Label4</ITEMLABEL4>
<ITEMLABEL5>Label5</ITEMLABEL5>
<ITEMLABEL6>Label6</ITEMLABEL6>
<ITEMLABEL7>Label7</ITEMLABEL7>
<ITEMLABEL8>Label8</ITEMLABEL8>
<ITEMLABEL9>Label9</ITEMLABEL9>
<ITEMLABEL10>Label0</ITEMLABEL10>
<ITEMLABEL11>Label1</ITEMLABEL11>
<ITEMLABEL12>Label2</ITEMLABEL12>
<ITEMLABEL13>Label3</ITEMLABEL13>
<ITEMLABEL14>Label4</ITEMLABEL14>
<ITEMLABEL15>Label5</ITEMLABEL15>
<ITEMLABEL16>Label6</ITEMLABEL16>
<ITEMLABEL17>Label7</ITEMLABEL17>`
</DOCUMENTS> needs to be added to the end so that the whole result looks like this:
<?xml version="1.0"?>
<DOCUMENTS>
<VERSION>2.0</VERSION>
<LICENTIEHOUDER>Henrov</LICENTIEHOUDER>
<XTN>BulkImport</XTN>
<ARCHIEFNAAM>Name</ARCHIEFNAAM>
<ITEMLABEL1>Label1</ITEMLABEL1>
<ITEMLABEL2>Label2</ITEMLABEL2>
<ITEMLABEL3>Label3</ITEMLABEL3>
<ITEMLABEL4>Label4</ITEMLABEL4>
<ITEMLABEL5>Label5</ITEMLABEL5>
<ITEMLABEL6>Label6</ITEMLABEL6>
<ITEMLABEL7>Label7</ITEMLABEL7>
<ITEMLABEL8>Label8</ITEMLABEL8>
<ITEMLABEL9>Label9</ITEMLABEL9>
<ITEMLABEL10>Label0</ITEMLABEL10>
<ITEMLABEL11>Label1</ITEMLABEL11>
<ITEMLABEL12>Label2</ITEMLABEL12>
<ITEMLABEL13>Label3</ITEMLABEL13>
<ITEMLABEL14>Label4</ITEMLABEL14>
<ITEMLABEL15>Label5</ITEMLABEL15>
<ITEMLABEL16>Label6</ITEMLABEL16>
<ITEMLABEL17>Label7</ITEMLABEL17>
<DOCUMENT>
<BRON>Bulk Import</BRON>
<INDEXEERDATUM>2015-07-30</INDEXEERDATUM>
<ITEMVALUE1>ITEMVALUE1</ITEMVALUE1>
<ITEMVALUE2>ITEMVALUE2</ITEMVALUE2>
<ITEMVALUE3>ITEMVALUE3</ITEMVALUE3>
<ITEMVALUE4>ITEMVALUE4</ITEMVALUE4>
<ITEMVALUE5>ITEMVALUE5</ITEMVALUE5>
<ITEMVALUE6>2015-07-30</ITEMVALUE6>
<ITEMVALUE7>ITEMVALUE7</ITEMVALUE7>
<ITEMVALUE8></ITEMVALUE8>
<ITEMVALUE9></ITEMVALUE9>
<ITEMVALUE10></ITEMVALUE10>
<ITEMVALUE11>ITEMVALUE11</ITEMVALUE11>
</DOCUMENT>
<BRON>Bulk Import</BRON>
<INDEXEERDATUM>2015-07-30</INDEXEERDATUM>
<ITEMVALUE1>ITEMVALUE1</ITEMVALUE1>
<ITEMVALUE2>ITEMVALUE2</ITEMVALUE2>
<ITEMVALUE3>ITEMVALUE3</ITEMVALUE3>
<ITEMVALUE4>ITEMVALUE4</ITEMVALUE4>
<ITEMVALUE5>ITEMVALUE5</ITEMVALUE5>
<ITEMVALUE6>2015-07-30</ITEMVALUE6>
<ITEMVALUE7>ITEMVALUE7</ITEMVALUE7>
<ITEMVALUE8></ITEMVALUE8>
<ITEMVALUE9></ITEMVALUE9>
<ITEMVALUE10></ITEMVALUE10>
<ITEMVALUE11>ITEMVALUE11</ITEMVALUE11>
</DOCUMENT>
</DOCUMENTS>
This needs to be exported as a XML file.
I tried to do SELECT 'text in front' + (select for xml) + 'text after' but that has an unwanted result: all < and > are replaced with &lt en &gt effectively destroying my xml.
So basically: how do I add information to the beginning and the end of the result of a FOR XML query?
Simply create a sub query for documents. You'll need to use type to prevent XML tags escaping.
SELECT
'2.0' AS [VERSION],
'Henrov' AS [LICENTIEHOUDER],
-- Add all other fields.
(
SELECT *
FROM [staging].[vw_mydata]
FOR XML PATH('DOCUMENT'), type
)
FOR XML PATH('DOCUMENTS')

SQL query for XML data

I have a SQL Server database table with a column called XML that contains XML data which is structured like this:
<Item xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://test/data">
<Roots>
<Root>
<Name>Field Name</Name>
<Value>Field Value</Value>
</Root>
<Root>
<Name>Field Name</Name>
<Value>Field Value</Value>
</Root>
</Roots>
I want to use T-SQL to get the Value where Name = Total. I have tried the following but it isn't returning any data:
SELECT [XML]
FROM [BusinessAccount]
WHERE [XML].value('(/Root/Name)[13]', 'VARCHAR(MAX)') LIKE '%Total%'
If anyone could tell me where I've gone wrong?
You are missing the required WITH XMLNAMESPACES for your XML and the path is incorrect.
If you want to bring back rows where the 13th element consists of the text Total you can use the below.
WITH XMLNAMESPACES (DEFAULT 'http://test/data')
SELECT [XML]
FROM [BusinessAccount]
WHERE 1 = [XML].exist('(/Item/Roots/Root/Name)[13][text() = "Total"]')
Otherwise you can add the WITH XMLNAMESPACES to your original query and fix the path there too.
You need to specify namespaces. You can then match <Name> and <Value> pairs and extract the contents of <Value> like so:
SELECT NameNode.value('declare namespace x="http://test/data"; (../x:Value)[1]', 'varchar(100)')
FROM [BusinessAccount]
CROSS APPLY [XML].nodes('declare namespace x="http://test/data"; //x:Root/x:Name') AS n(NameNode)
WHERE NameNode.value('.', 'varchar(100)') = 'Total'
Demo on db<>fiddle

SQL Query to Pars Out a Value from NTEXT (XML format) field

I have XML data in an NTEXT field (ou.ORDMODE), that I need to parse out a value (description) from. Column may contain null values. The data looks like this:
<?xml version="1.0" encoding="utf-16"?>
<UDFValidatedValue xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Accellos.Platform.UDF">
<Description>Export</Description>
<Value>EXP</Value>
<ValueType>String</ValueType>
</UDFValidatedValue>
The line I have in my query is this:
CAST(REPLACE(CAST(ou.ORDMODE as NVARCHAR(MAX)),' xmlns="http://schemas.datacontract.org/2004/07/Accellos.Platform.UDF"','') as XML).value ('(/UDFValidatedValue/Description/text())[0]', 'nvarchar(100)') as Mode3,
but Mode3 column is returned as blank.
What am I doing wrong?
You could use this:
DEclare #xml xml = N'<?xml version="1.0" encoding="utf-16"?>
<UDFValidatedValue xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Accellos.Platform.UDF">
<Description>Export</Description>
<Value>EXP</Value>
<ValueType>String</ValueType>
</UDFValidatedValue>';
;WITH XMLNAMESPACES(DEFAULT 'http://schemas.datacontract.org/2004/07/Accellos.Platform.UDF')
select #xml.value ('(/UDFValidatedValue/Description)[1]', 'nvarchar(100)') as Mode3
or
select #xml.value ('declare namespace
udf="http://schemas.datacontract.org/2004/07/Accellos.Platform.UDF";
(/udf:UDFValidatedValue/udf:Description)[1]', 'nvarchar(100)') as Mode3
Demo link: Rextester
As ZLK pointed out in his comment, your own approach seems to be quite okay, just the index must be 1 instead of 0. Try it here:
ad "quite okay": Actually this is absolutely far away from "quite okay", read my hint below...
DECLARE #tbl TABLE(SomeXmlInText TEXT);
INSERT INTO #tbl VALUES
('<?xml version="1.0" encoding="utf-16"?>
<UDFValidatedValue xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Accellos.Platform.UDF">
<Description>Export</Description>
<Value>EXP</Value>
<ValueType>String</ValueType>
</UDFValidatedValue>');
SELECT SomeXmlInText
,CAST(REPLACE(CAST(SomeXmlInText as NVARCHAR(MAX)),' xmlns="http://schemas.datacontract.org/2004/07/Accellos.Platform.UDF"','') as XML).value ('(/UDFValidatedValue/Description/text())[1]', 'nvarchar(100)') as Mode3
FROM #tbl
From your answer I take, that there is still no result...
Some possible issues:
XML is not really as you expect it
more / other namespaces
some weird no, not for you issue :-D
Hint
As you probably know TEXT, NTEXT and IMAGE are deprecated for centuries and should really not be used any more... If you have the slightest chance to change this, then you should store your XML in the appropritate XML-type...
You should not need any casts or manipulations on string level. This can be working, but is really out-dated...

Getting node value in xml column

Please let me know why the following XML query is not fetching any result.
I am trying to get value EffectiveUserName tag.
DECLARE #MyXML XML
SET #MyXML = '<PropertyList xmlns="urn:schemas-microsoft-com:xml-analysis" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<Catalog>name</Catalog>
<Timeout>600</Timeout>
<Format>Native</Format>
<DbpropMsmdFlattened2>false</DbpropMsmdFlattened2>
<Cube>Model</Cube>
<DbpropMsmdOptimizeResponse>1</DbpropMsmdOptimizeResponse>
<DbpropMsmdActivityID>68A6900B-20F8-4A02-AEC3-7C56B2D3C5D5</DbpropMsmdActivityID>
<DbpropMsmdRequestID>A0D1E07F-AE29-4CCA-AEE4-3B79D97CA426</DbpropMsmdRequestID>
<DbpropMsmdCurrentActivityID>68A6900B-20F8-4A02-AEC3-7C56B2D3C5D5</DbpropMsmdCurrentActivityID>
<LocaleIdentifier>1033</LocaleIdentifier>
<EffectiveUserName>userid#domainname.com</EffectiveUserName>
<sspropinitappname>PowerBI</sspropinitappname>
</PropertyList>'
select #MyXML.value('(/PropertyList/EffectiveUserName)[1]','varchar(max)')
Your XML has a default namespace that you must respect and include in your query!
<PropertyList xmlns="urn:schemas-microsoft-com:xml-analysis"
***********************************************
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
Use this code to grab the value you're looking for by defining the default XML namespace for your query:
;WITH XMLNAMESPACES(DEFAULT 'urn:schemas-microsoft-com:xml-analysis')
SELECT
#MyXML.value('(/PropertyList/EffectiveUserName)[1]', 'varchar(50)')
You can ignore the namespace by using *: before the tag names:
select #MyXML.value('(/*:PropertyList/*:EffectiveUserName)[1]','varchar(max)')

Select values from XML in SQL

I need to select values from an XML document. These values are stored as childnodes as follows:
<customers>
<customer>
<kunnr>1</kunnr>
<kdgrp>2</kdgrp>
</customer>
<customer>
<kunnr>2</kunnr>
<kdgrp>2</kdgrp>
</customer>
</customers>
I need to select the values of kunnr and kdgrp for every customer node. I expect a result like this:
kunnr kdgrp
1 2
2 2
What I tried so far:
SELECT #xml.query('/customers/customer/kunnr') AS KUNNR,
#xml.query('/customers/customer/kdgrp') AS KDGRP
This results in one row with two colums containing XML:
KUNNR KDGRP
<kunnr>1</kunnr><kunnr>2</kunnr> <kdgrp>2</kdgrp><kdgrp>2</kdgrp>
Another try:
SELECT C.value('/kunnr/text()','nvarchar(10)') as KUNNR,
C.value('/kdgrp/text()','nvarchar(10)') as KDGRP
from #xml.nodes('/customers/customer') AS T(C);
This resulted in the following error message:
XQuery [value()]: 'value()' requires a singleton (or empty sequence), found operand of type 'xdt:untypedAtomic *'
Maybe something like this:
DECLARE #xml XML
SET #xml='<customers>
<customer>
<kunnr>1</kunnr>
<kdgrp>2</kdgrp>
</customer>
<customer>
<kunnr>2</kunnr>
<kdgrp>2</kdgrp>
</customer>
</customers>'
And then a query like this:
SELECT
c.value('kunnr[1]', 'nvarchar(10)') AS kunnr,
c.value('kdgrp[1]', 'nvarchar(10)') AS kdgrp
FROM
#xml.nodes('//customers/customer') as t(c)
This will give you this result:
kunnr kdgrp
1 2
2 2
I had a problem related to extracting values from T-SQL XML and found an issue that may help others. When retrieving data with: .value('(/root/subnode)[1]', 'varchar(max)') that call would not retrieve data but the following call did: .value('(//subnode)[1]', 'varchar(max)'). Note that the working version replaced the root node with a /. The problem with the first call seemed to be that the root node came with a specification of an xml namespace like &< root xmlns="http://www..." &> and to get the .value call to return data I needed to get past the specification of the namespace which was causing things to fail for some reason.