XML Workshop X - Working with namespaces

  • Comments posted to this topic are about the item XML Workshop X - Working with namespaces

    .

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

    */

    .

  • 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?

  • 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.

    .

  • 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.

  • You can use WITH XMLNAMESPACES() to generate namespace information along with the generated XML. You can use FOR XML EXPLICIT to generate CDATA sections.

    .

  • "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.

  • 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

    .

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

Viewing 10 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic. Login to reply