How to add attributes to xml nodes in sql server 2005 - sql

If i wanted to add an attribute to the root element record, can i do this from the sql side?
SELECT top 1 'text' as nodeA
from test as z
FOR XML AUTO, ELEMENTS, root('record')
i would like to produce the xml like this:
<Root attribute="value">
<z>
<NodeA>text</NodeA>
</z>
</Root>

Use the new FOR XML PATH syntax:
SELECT TOP 1
'someValue' AS '#Attribute',
'text' as 'z/NodeA'
FROM dbo.Test
WHERE....
FOR XML PATH('YourElement'), ROOT('Root')
This would give something like
<Root>
<YourElement Attribute="someValue">
<z>
<NodeA>text</NodeA>
</z>
</YourElement>
</Root>
Read more about it here:
Simple Example of Creating XML File Using T-SQL
Using XML Serialization with SQL's FOR XML PATH

Your example is not doing what is requested.
request:
<Root attribute="someValue">
<YourElement>
<z>
<NodeA>text</NodeA>
</z>
</YourElement>
</Root>
your answer:
<Root>
<YourElement Attribute="someValue">
<z>
<NodeA>text</NodeA>
</z>
</YourElement>
</Root>
I'm doing something similar and using PowerShell to scrub the file before saving it:
scrub reason 1: https://connect.microsoft.com/SQLServer/feedback/details/265956/suppress-namespace-attributes-in-nested-select-for-xml-statements
scrub reason 2: THIS

SELECT
'someValue' AS '#Attribute',
(SELECT TOP 1
'text' as 'z/NodeA'
FROM dbo.Test
WHERE....
FOR XML PATH('YourElement')
)
FOR XML PATH('ROOT');
It should create a xml with ROOT containg attribute and list of ... inside.

Related

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

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)')

Selecting columns as XML with namespace

I need to select some columns from a table as XML with namespaces included in them along with other columns as is. For example, I have a following table layout:
ID C1 X1C1 X1C2 X2C3
1 A 1 2 3
What the query should return is:
ID C1 XmlData
1 A <xmldata1>
2 A <xmldata2>
Where <xmldata1> would be:
<Root xmlns:xsd="w3.org/2001/XMLSchema" xmlns:xsi="w3.org/2001/XMLSchema-instance" xmlns:mst="microsoft.com/wsdl/types/">
<Child attrib="C1">
<ChildValue xsi:type="xsd:integer">1</ChildNode>
</Child>
<Child attrib="C2">
<ChildNode xsi:type="xsd:integer">2</ChildNode>
</Child>
</Root>
and <xmldata2> would be:
<Rootxmlns:xsd="w3.org/2001/XMLSchema" xmlns:xsi="w3.org/2001/XMLSchema-instance" xmlns:mst="microsoft.com/wsdl/types/">
<Child attrib="C3">
<ChildNode xsi:type="xsd:integer">3</ChildNode>
</Child>
</Root>
I have a good reference how to build the xml from this SO question but I'm not able to put in the namespaces. If this is possible how to do it?
Edit:
I've used following query attempting to get the required result:
select 1 ID, 'A' C1, 1 X1C1, 2 X1C2, 3 X2C3
into #t
;with xmlnamespaces('w3.org/2001/XMLSchema' as xsd, 'w3.org/2001/XMLSchema-instance' as xsi, 'microsoft.com/wsdl/types/' as mst)
select ID, C1, (select (SELECT 'C1' "#attrib", 'xsd:integer' "ChildValue/#xsi:type",t.X1C1 as 'ChildValue' FOR XML PATH('Child'), type),(SELECT 'C2' "#name", 'xsd:integer' "ChildValue/#xsi:type", t.X1C2 as 'ChildValue' FOR XML PATH('Child'), type) FOR XML PATH('Root'), type) as property_data
FROM #t t
drop table #t
Here is the output of its xml part:
<Root xmlns:mst="microsoft.com/wsdl/types/" xmlns:xsi="w3.org/2001/XMLSchema-instance" xmlns:xsd="w3.org/2001/XMLSchema">
<Child xmlns:mst="microsoft.com/wsdl/types/" xmlns:xsi="w3.org/2001/XMLSchema-instance" xmlns:xsd="w3.org/2001/XMLSchema" attrib="C1">
<ChildValue xsi:type="xsd:integer">1</ChildValue>
</Child>
<Child xmlns:mst="microsoft.com/wsdl/types/" xmlns:xsi="w3.org/2001/XMLSchema-instance" xmlns:xsd="w3.org/2001/XMLSchema" name="C2">
<ChildValue xsi:type="xsd:integer">2</ChildValue>
</Child>
</Root>
I can't get rid of the namespaces in the Child node.
I used this solution: TSQL for xml add schema attribute to root node
Basically, I did not put the namespace in the beginning but after generating the required xml structure I casted the xml to nvarchar(max) and replaced the root node with the desired namespace.
I also needed to use namespace prefix in the attribute. For that I used a pseudo attribute name which I replaced with a proper xml namespace prefix.
Both operations were done using tsql REPLACE function. Hacky but couldn't find other proper ways to do it.
you need to include WITH xmlnamespaces , example:
;with xmlnamespaces('w3.org/2001/XMLSchema' as xsd, 'w3.org/2001/XMLSchema-instance' as xsi, 'microsoft.com/wsdl/types/' as mst)
select ID, C1,
(select
(SELECT 'C1' "#name",t.C1 as 'value'FOR XML PATH('Property'), type),
(SELECT 'C2' "#name",t.C2 as 'value'FOR XML PATH('property'), type)
FOR XML PATH('data'), type) as property_data
FROM TableName t
Have you tried like?
select XML_COL_NAME.value('(/rootNode//childNode/node())[1]', 'nvarchar(64)') from tableName

XML sql query , attribute as column

I need to query xml data available in server
<root>
<Parameter>
<Param>APP_REG_NUMBER</Param>
<Value>AL/T/2010/86</Value>
</Parameter>
<Parameter>
<Param>SUBLINEID</Param>
<Value>235931</Value>
</Parameter>
</root>
This is the structure I am saving data into SQL Server, I need output as follows:
Filed1 , Filed2 , APP_REG_NUMBER, SUBLINEID
something something , AL/T/2010/86, 235931
please do the needful
You can use XQuery for this, but realize you are recreating key-value-pairs in XML, which negates the ability to use schemas or XQuery/XPATH in any reasonable manner. Consider changing the format to:
<root>
<APP_REG_NUMBER>AL/T/2010/86</APP_REG_NUMBER>
<SUBLINEID>235931</SUBLINEID>
</root>
I digress... the query you want is:
DECLARE #testXml xml = N'<root>
<Parameter><Param>APP_REG_NUMBER</Param><Value>AL/T/2010/86</Value></Parameter>
<Parameter><Param>SUBLINEID</Param><Value>235931</Value></Parameter>
</root>'
SELECT
#testXml.value('(//Parameter[Param/text()="APP_REG_NUMBER"]/Value)[1]', 'nvarchar(50)') as APP_REG_NUMBER,
#testXml.value('(//Parameter[Param/text()="SUBLINEID"]/Value)[1]', 'nvarchar(50)') as SUBLINEID
You use the //Parameter syntax to find all Parameter elements and then filter them ([Param/text()="foobar"]) to only those which have a child named Value that have the inner text of SUBLINEID. From there, you navigate to the /Value child element and return the first result ((query)[1]).

Modify xml element name in SQL Server

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.