SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


XML Workshop X - Working with namespaces


XML Workshop X - Working with namespaces

Author
Message
jacob sebastian
jacob sebastian
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1906 Visits: 2523
Comments posted to this topic are about the item XML Workshop X - Working with namespaces

.
panesofglass
panesofglass
SSC Journeyman
SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)

Group: General Forum Members
Points: 86 Visits: 104
Great article, Jacob. I think you might have mentioned this previously, but is there a limit to the number of characters an XML query will return? Also, is there a way to get around this limitation? My queries keep coming back as only the first several thousand characters.

Thanks!
jacob sebastian
jacob sebastian
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1906 Visits: 2523
Ryan Riley (11/7/2007)
Great article, Jacob. I think you might have mentioned this previously, but is there a limit to the number of characters an XML query will return? Also, is there a way to get around this limitation? My queries keep coming back as only the first several thousand characters.

Thanks!


Ryan,
I do not think there is a limitation. The following query returns an XML stream that is half a million characters long.

DECLARE @x XML
SELECT @x = (
select * from sys.columns for xml auto
)

SELECT LEN(CAST (@x as VARCHAR(MAX)))

/*
OUTPUT:


--------------------
417597

(1 row(s) affected)
*/

.
JWIDM76
JWIDM76
SSC Rookie
SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)

Group: General Forum Members
Points: 48 Visits: 176
I believe I need to use FOR XML EXPLICIT because of my need for the CDATA directive, and I also want to be able to have a collection based on a related table (the SKU's in the example below) within my XML results. Is there a way to specify a namespace when using FOR XML EXPLICIT or on the flip side is there a way for me to have CDATA and collections if I don't use EXPLICIT mode.

I need my results to look like this:

<product:product xmlns:product="http://www.myco.com/product" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.myco.com/product rl-product+CustomProductCatalog+product.xsd " xmlns:enc="http://schemas.xmlsoap.org/soap/encoding/" ID="productprod000191" repositoryId="prod000191">
<product:product.heightMax xsi:nil="true"/>
<product:product.gender collectionType="List" componentType="string">
<product:string>Womens</product:string>
</product:product.gender>
<product:product.displayName xsi:type="string"><![CDATA[a shirt, logo]]></product:product.displayName>
<product:product.id xsi:type="string"><![CDATA[prod000191]]></product:product.id>
<product:product.color collectionType="List" componentType="string">
<product:string>Red</product:string>
<product:string> White</product:string>
</product:product.color>
<product:product.childSKUs collectionType="List" componentType="itemRef">
<product:childSKUssku ID="sku84016" repositoryId="84016">
<product:sku.displayName xsi:type="string"><![CDATA[small, green]]></product:sku.displayName>
<product:sku.newSku xsi:type="boolean">true</product:sku.newSku>
<product:sku.listPrice xsi:type="double">16.95</product:sku.listPrice>
<product:sku.id xsi:type="string"><![CDATA[84016]]></product:sku.id>
</product:childSKUssku>
<product:childSKUssku ID="sku84020" repositoryId="84020">
<product:sku.displayName xsi:type="string"><![CDATA[large, green]]></product:sku.displayName>
<product:sku.newSku xsi:type="boolean">true</product:sku.newSku>
<product:sku.listPrice xsi:type="double">16.95</product:sku.listPrice>
<product:sku.id xsi:type="string"><![CDATA[84020]]></product:sku.id>
</product:childSKUssku>
</product:product.childSKUs>
<product:product.form xsi:nil="true"/>
</product:product>



What mode do you recommend?
jacob sebastian
jacob sebastian
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1906 Visits: 2523
Which version of SQL Server are you using? If you are on SQL Server 2005 or above, you can use WITH XMLNAMESPACES() to generate an XML document with namespace information.

if you need CDATA sections, you need to use EXPLICIT.

.
JWIDM76
JWIDM76
SSC Rookie
SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)

Group: General Forum Members
Points: 48 Visits: 176
I'm using 2005. I need to specify namespace AND use CDATA. I was wondering if there was a method for achieving both.

Thank you.
jacob sebastian
jacob sebastian
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1906 Visits: 2523
You can use WITH XMLNAMESPACES() to generate namespace information along with the generated XML. You can use FOR XML EXPLICIT to generate CDATA sections.

.
JWIDM76
JWIDM76
SSC Rookie
SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)

Group: General Forum Members
Points: 48 Visits: 176
"The following FOR XML features are not supported with WITH XMLNAMESPACES list: EXPLICIT mode, XMLSCHEMA and XMLDATA directives."

Okay, this is the message I was getting in the first place, which prompted me to ask my original question. I can only guess what you're trying to tell me is not to use XMLNAMESPACES and in the same statement where I'm using EXPLICIT mode to generate the body of the results.
jacob sebastian
jacob sebastian
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1906 Visits: 2523
you are right.
Here is an example that generates CDATA and namespace declarations.

SELECT
1 AS Tag,
NULL AS Parent,
'Jacob' AS 'Person!1!Name!element',
'Hellow world' AS 'Person!1!greeting!CDATA',
'http://beyondrelational.com/xml.aspx'
AS 'Person!1!xmlns:x'
FOR XML EXPLICIT



.
yazalpizar_
yazalpizar_
SSC Veteran
SSC Veteran (220 reputation)SSC Veteran (220 reputation)SSC Veteran (220 reputation)SSC Veteran (220 reputation)SSC Veteran (220 reputation)SSC Veteran (220 reputation)SSC Veteran (220 reputation)SSC Veteran (220 reputation)

Group: General Forum Members
Points: 220 Visits: 626
Need to do some "homework" with the namespaces feature.

Here is the code with comments in spanish:

--=================================================================================================================================
-- XML Workshop Part V Namespaces: el uso de NAMESPACES permite desambiguar el nombre de los elementos
/*
normalmente en SQL usamos alias para desambiguar, en programación usamos clase.procedimiento para diferenciar 2 llamadas
iguales en diferentes clases. En XML usamos Namespaces
*/
--=================================================================================================================================
--vamos a crear un XML de ejemplo
/*
<configuration>
<connection>
<provider>World Wide Internet Providers</provider>
<speed>512 KBPS</speed>
</connection>
<connection>
<provider>SQL Client Provider</provider>
<protocol>TCP/IP</protocol>
<Authentication>Windows</Authentication>
</connection>
</configuration>

una aplicación que intente leer el XML anterior fallará pues tenemos el nodo connection repetido, aunque uno se refiere a
Internet y el otro a SQL

modifiquemos el XML para tener en cuenta los namespaces

<configuration
xmlns:net="urn:www.dotnetquest.com/internetconnection"
xmlns:db="urn:www.dotnetquest.com/databaseconnection">
<net:connection>
<net:provider>World Wide Internet Providers</net:provider>
<net:speed>512 KBPS</net:speed>
</net:connection>
<db:connection>
<db:provider>SQL Client Provider</db:provider>
<db:protocol>TCP/IP</db:protocol>
<db:Authentication>Windows</db:Authentication>
</db:connection>
</configuration>
*/

--Veamos como generar un XML con esta estructura, primero crearemos las tablas necesarias e introduzcamos datos
-- Create the table for Net Connection
-- be aware that if using AdventureWorks database it has already a NetConnection table, you can temporary rename it or change test table name
CREATE TABLE NetConnection (Provider VARCHAR(50),Speed VARCHAR(20))

-- Populate the Table
INSERT INTO NetConnection(Provider, Speed)
SELECT 'World Wide Internet Providers','512 KBPS'

-- Create table for DB Connection
CREATE TABLE DbConnection (Provider VARCHAR(50),Protocol VARCHAR(20),[Authentication] VARCHAR(20))

-- Populate the Table
INSERT INTO DbConnection (Provider,Protocol,[Authentication] )
SELECT 'SQL Client Provider','TCP/IP','Windows'

--ahora crearemos el XML, usando la sentencia TSQL: WITH XMLNAMESPACES
WITH XMLNAMESPACES
(
'urn:www.dotnetquest.com/internetconnection' AS net,
'urn:www.dotnetquest.com/databaseconnection' AS db
)
SELECT net.Provider AS 'net:Connection/netTonguerovider',
net.Speed AS 'net:Connection/net:Speed',
db.Provider AS 'db:Connection/dbTonguerovider',
db.Protocol AS 'db:Connection/dbTonguerotocol',
db.[Authentication] AS 'db:Connection/db:Authentication'
FROM NetConnection net
CROSS JOIN DbConnection db
FOR XML PATH('Configuration')


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search