Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

XML Workshop X - Working with namespaces Expand / Collapse
Author
Message
Posted Thursday, October 18, 2007 3:07 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Tuesday, December 24, 2013 4:42 AM
Points: 460, Visits: 2,523
Comments posted to this topic are about the item XML Workshop X - Working with namespaces

.
Post #412439
Posted Wednesday, November 7, 2007 10:07 AM


SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, March 17, 2014 8:51 AM
Points: 42, 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!
Post #419692
Posted Wednesday, November 7, 2007 11:37 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Tuesday, December 24, 2013 4:42 AM
Points: 460, Visits: 2,523
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)
*/


.
Post #419927
Posted Thursday, January 28, 2010 12:05 PM


Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, July 9, 2012 2:36 PM
Points: 12, 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?
Post #855549
Posted Thursday, January 28, 2010 7:27 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Tuesday, December 24, 2013 4:42 AM
Points: 460, Visits: 2,523
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.


.
Post #855718
Posted Thursday, January 28, 2010 8:10 PM


Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, July 9, 2012 2:36 PM
Points: 12, 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.
Post #855728
Posted Thursday, January 28, 2010 10:10 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Tuesday, December 24, 2013 4:42 AM
Points: 460, Visits: 2,523
You can use WITH XMLNAMESPACES() to generate namespace information along with the generated XML. You can use FOR XML EXPLICIT to generate CDATA sections.

.
Post #855761
Posted Friday, January 29, 2010 7:13 AM


Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, July 9, 2012 2:36 PM
Points: 12, 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.
Post #856051
Posted Saturday, February 6, 2010 9:11 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Tuesday, December 24, 2013 4:42 AM
Points: 460, Visits: 2,523
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



.
Post #861111
Posted Friday, December 23, 2011 8:13 AM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Today @ 12:32 AM
Points: 142, Visits: 461
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/net:Provider',
net.Speed AS 'net:Connection/net:Speed',
db.Provider AS 'db:Connection/db:Provider',
db.Protocol AS 'db:Connection/db:Protocol',
db.[Authentication] AS 'db:Connection/db:Authentication'
FROM NetConnection net
CROSS JOIN DbConnection db
FOR XML PATH('Configuration')

Post #1226268
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse