SQL Server: XPATH Query failing - sql-server

I am working in SQL Server Management Studio on a stored procedure. There is a column containing XML that I am trying to access with XPATH but I get this error:
Parsing XML with internal subset DTDs not allowed. Use CONVERT with style option 2 to enable limited internal subset DTD support`
I read the docs on convert and importing xml but no luck.
To be clear, I have XML stored in a column in a table, and now want to access it with xpath after querying it into memory in a stored procedure. Anyone know what I should do?

Your most likely getting this because somewhere inside of your XML you are using a document definition such as this: (MSDN example of DTD)
<!DOCTYPE DOC [<!ATTLIST elem1 attr1 CDATA "defVal1">]><elem1>January</elem1>
Microsoft blocks this by default as it creates a potential security hole. The convert with style option 2 message is telling you that you can enable partial support for DTD in order to process this XML through use of the CONVERT function as it relates to XML.
I don't have personal experience with doing that so I can't offer more than the link to MSDN.
The only other option is to enable full DTD support on the server which Microsoft does not recommend.

Related

MS Azure DB: cannot find data type XML & Incorrect syntax near ' Identity'

I was trying to change the data type into XML in order to split one column into multiple columns. However, when I ran the syntax below, an error: cannot find data type XML was showed. I searched some of the answers online. It seems Azure is supported for XML. Is there a way to solve it?
cast('<m>'+replace(Employee_Name,#delimiter,'</m><m>')+'</m>' as XML)
Besides, I found that IDENTITY(1,1) is not supported either.
With SQL-Server 2016 there is native support for this: STRING_SPLIT()-function. But - according to the linked doumentation - this seems not to be offered for Azure Data Warehouse...
The string splitting via XML needs the XML-DataType, since you will need .nodes() and .value() to retrieve the values. According to this documentation this is supported with Azure Database, don't know of restriction with the Data Warehouse version...
There are many examples for string splitting functions using loops or recursive CTEs. This article compares some of them...
This error occurs when you try to run the query casting to XML while logged into a Microsoft Azure SQL Data Warehouse. Azure SQL Data Warehouse does not support the XML datatype. You can confirm your version with the following sql:
select ##version
https://learn.microsoft.com/en-us/azure/sql-data-warehouse/sql-data-warehouse-tables-data-types

XML in SQL Server 2005? Better than JSON in Varchar?

What are the benefits of storing XML in SQL Server over storing JSON in a varchar field?
Any tutorial available for how to use the XML data type effectively?
Do I need to provide the dtd / xml schema somehow? I've heard it is optional, right?
Thank you.
UPDATE: here's the answer to the last part of the Q.
XML schema information is used in
storage and query optimizations. Typed
XML instances contain typed values in
the internal, binary representation as
well as in XML indexes. This provides
efficient processing of typed XML
data.
quoted from: http://msdn.microsoft.com/en-us/library/ms345117(SQL.90).aspx
XML in SQL Server 2005 and up allows you to directly manipulate the XML stored inside the database table using XQuery - you can't really do that with JSON in a VARCHAR field.
Check out XML Support in Microsoft SQL Server 2005 and Fundamentals of the XML Datatype in SQL Server 2005 for more info and more background.
Also, the XML stored in a XML column in SQL Server is "tokenized" and pre-parsed - it's not just stored as plain text. And you can even put indices on your XML - on its nodes, its values - whatever you need.
Storing it as XML allows you to leverage the SQL XML support: XPATH, XQUERY, XML Indexes and such. These allow for efficient search and manipulation of the content. I recommend you read XML Best Practices for Microsoft SQL Server 2005
JSON content in VARCHAR would be opaque to searches and manipulation.
XML can be indexed for more performant querying.
Data can be extracted from XML data usng XPath.
A schema can be provided to constrain the XML to a secifiation but this is optional.
Client libraries understan the XML data type and can send/receive it more carefully/easily.
None of the above is available for JSON stored in varchar.
you can query for individual xml property values if you use the xml data type. I don't believe the same functionality is available for json:
http://msdn.microsoft.com/en-us/library/ms191474.aspx
I don't believe that the xsd is required as I've used the query feature without having that defined before
Storing XML in SQL 2005 is great. You can put the whole XML file in a single field, then run SELECT commands to pull out certain attributes and elements. Putting a XSD is not neccesary. I don't believe it has any JSON support even in 2008 although I could be wrong. Here's a good starter article on it.

SQL Server XML (multi-namespace) Parsing question

I'm trying to use SQL Server Integration Services (SSIS) to parse an XML file and map the elements to database columns across several tables in a single database.
However, when I use the Data Flow Task->XML Source to try and parse an example XML file (that file is located here, XSD is located here), it says
"http://www.exchangenetwork.net/schema/TRI/4:TransferWasteQuantity" has multiple members named "http://www.exchangenetwork.net/schema/TRI/F:WasteQuantityCatastrophicMeasure"
Is there any way to get SSIS to parse XML data such as this? This schema changes regularly so I'd prefer to do as little parsing code outside of the data mappings as possible. Also if there's a better way to do this outside of SSIS (say, by using SQL Server Analysis Services) then that would work too.
Apparently, due to the complexity of the XML, this is not possible with the latest iterations of SQL, at least without significant modification of the incoming XML submission and XSD, which would likely lead to data corruption, therefore it is conclusive that it is not feasible to implement.

Can Reporting Services be used to format XML returned in an XML data type column?

Using SQL Server 2005 and Reporting Services, I have a stored proc that returns several columns, one of which is of the XML data type. Can I manipulate/format/style that XML column in my Reporting Services report? If so, how?
You can. Use the classes in System.Xml to parse the XML the way you want.
On the References tab of the SSRS Report Properties dialog, add a reference to System.Xml. Then add your parsing code to the Code tab.
There is an example here.
Haven't tried this but SRSS allows you to enter code in your report via scripting. You may be able to do something with the XML inside there, possibly with XSL

What are the advantages of having typed XML columns in SQL server?

From what I've read an XML column in SQL server can be typed (having a schema) or untyped. What are the advantages of using typed XML?
Is it just that the XML is validated?
First is validation.
Second you can formulate XML queries based on the XSD and know that the XML content is compatible with your queries.
It also helps with errors and debugging - see Silent XQuery Failures

Resources