SQLServerCentral Article

XML Workshop XI - Default Namespaces

,

Introduction

In the previous workshop (XML Workshop X) we have seen a basic introduction to XML Namespaces. We have seen examples which clearly explained why we need to have namespaces in XML. In this session we will examine XML

Default Namespaces and see how to generate XML which contains default namespace definitions.

Default Namespace

Default namespace applies to all the un prefixed elements of an XML document. A default namespace declaration is usually placed at the root of an XML

document. In this case, the default namespace is said to have global scope

and all the un prefixed elements of the XML document will belong to the default namespace. It is also quite possible to have a default namespace defined for a specific XML element. In this case, the scope of the namespace declaration will last up to the closing tag of the element. Let us look at a few examples.

Here is the XML that we generated in the previous lab. [xml]

    1 <Configuration

    2   xmlns:db="urn:www.dotnetquest.com/DbConnection"

    3   xmlns:net="urn:www.dotnetquest.com/NetConnection">

    4   <net:Connection>

    5     <net:Provider>World Wide Internet Providers</net:Provider>

    6     <net:Speed>512 KBPS</net:Speed>

    7   </net:Connection>

    8   <db:Connection>

    9     <db:Provider>SQL Client Provider</db:Provider>

   10     <db:Protocol>TCP/IP</db:Protocol>

   11     <db:Authentication>Windows</db:Authentication>

   12   </db:Connection>

   13 </Configuration>

Given below is the equivalent version of the same XML which uses default a default namespace. [xml]

    1 <!--

    2 Note that the first namespace is declared as default

    3 namespace. Note that no prefix assigned to this namespace

    4 -->

    5 <Configuration

    6   xmlns="urn:www.dotnetquest.com/DbConnection"

    7   xmlns:net="urn:www.dotnetquest.com/NetConnection">

    8   <net:Connection>

    9     <net:Provider>World Wide Internet Providers</net:Provider>

   10     <net:Speed>512 KBPS</net:Speed>

   11   </net:Connection>

   12   <!--

   13   Note that we are not using any prefix here. This element

   14   and all the child elements will belong to the default

   15   namespace.-->

   16   <Connection>

   17     <Provider>SQL Client Provider</Provider>

   18     <Protocol>TCP/IP</Protocol>

   19     <Authentication>Windows</Authentication>

   20   </Connection>

   21 </Configuration>

The above XML is equivalent to the previous one that we have seen. The only difference is that the new version of the XML makes use of the default namespace declaration. Let us modify the TSQL Query that we created in the previous lab, so that it will generate the XML structure with a default namespace declaration. If you have not done the previous lab, you need to create the sample tables and populate them. You can find the script here. The following query will generate the XML structure that we just discussed. [code]

    1 WITH XMLNAMESPACES

    2 (

    3     -- This is the default namespace

    4     DEFAULT 'urn:www.dotnetquest.com/DbConnection',

    5     'urn:www.dotnetquest.com/NetConnection' AS net

    6 )

    7 SELECT

    8     net.Provider AS 'net:Connection/net:Provider',

    9     net.Speed AS 'net:Connection/net:Speed',

   10     -- we don't need the prefix any more

   11     db.Provider AS 'Connection/Provider',

   12     db.Protocol AS 'Connection/Protocol',

   13     db.[Authentication] AS 'Connection/Authentication'

   14 FROM NetConnection net

   15 CROSS JOIN DbConnection db

   16 FOR XML PATH('Configuration')

Reading values

Now let us see how to read values from an XML variable which contains namespace information. In the previous labs we have seen several examples of reading values from XML variables and columns. We have not seen any example with namespace information so far. Here is the query that reads values from an XML variable which contains namespace information. [code]

    1 declare @x xml

    2 set @x = '

    3 <Configuration

    4   xmlns:db="urn:www.dotnetquest.com/DbConnection"

    5   xmlns:net="urn:www.dotnetquest.com/NetConnection">

    6   <net:Connection>

    7     <net:Provider>World Wide Internet Providers</net:Provider>

    8     <net:Speed>512 KBPS</net:Speed>

    9   </net:Connection>

   10   <db:Connection>

   11     <db:Provider>SQL Client Provider</db:Provider>

   12     <db:Protocol>TCP/IP</db:Protocol>

   13     <db:Authentication>Windows</db:Authentication>

   14   </db:Connection>

   15 </Configuration>

   16 '

   17 -- read values from the XML variable

   18 SELECT

   19     x.c.value(

   20         'declare namespace net="urn:www.dotnetquest.com/NetConnection";

   21         (net:Connection/net:Provider)[1]', 'varchar(max)')

   22         AS NetProvider,

   23     x.c.value(

   24         'declare namespace net="urn:www.dotnetquest.com/NetConnection";

   25         (net:Connection/net:Speed)[1]', 'varchar(max)')

   26         AS Speed,

   27     x.c.value(

   28         'declare namespace db="urn:www.dotnetquest.com/DbConnection";

   29         (db:Connection/db:Provider)[1]', 'varchar(max)')

   30         AS DbProvider,

   31     x.c.value(

   32         'declare namespace db="urn:www.dotnetquest.com/DbConnection";

   33         (db:Connection/db:Protocol)[1]', 'varchar(max)')

   34         AS Protocol,

   35     x.c.value(

   36         'declare namespace db="urn:www.dotnetquest.com/DbConnection";

   37         (db:Connection/db:Authentication)[1]', 'varchar(max)')

   38         AS [Authentication]

   39     FROM @x.nodes('/Configuration') x(c)

Using WITH XMLNAMESPACES you can make this query simpler. Here is a different syntax which produces the same results, but using WITH XMLNAMESPACES. [code]

    1 declare @x xml

    2 set @x = '

    3 <Configuration

    4   xmlns:db="urn:www.dotnetquest.com/DbConnection"

    5   xmlns:net="urn:www.dotnetquest.com/NetConnection">

    6   <net:Connection>

    7     <net:Provider>World Wide Internet Providers</net:Provider>

    8     <net:Speed>512 KBPS</net:Speed>

    9   </net:Connection>

   10   <db:Connection>

   11     <db:Provider>SQL Client Provider</db:Provider>

   12     <db:Protocol>TCP/IP</db:Protocol>

   13     <db:Authentication>Windows</db:Authentication>

   14   </db:Connection>

   15 </Configuration>

   16 '

   17 -- read values from the XML variable

   18 ;WITH XMLNAMESPACES

   19 (

   20     'urn:www.dotnetquest.com/NetConnection' AS net,

   21     'urn:www.dotnetquest.com/DbConnection' AS db

   22 )

   23 SELECT

   24     x.c.value(

   25         '(net:Connection/net:Provider)[1]', 'varchar(20)')

   26         AS NetProvider,

   27     x.c.value(

   28         '(net:Connection/net:Speed)[1]', 'varchar(10)')

   29         AS Speed,

   30     x.c.value(

   31         '(db:Connection/db:Provider)[1]', 'varchar(20)')

   32         AS DbProvider,

   33     x.c.value(

   34         '(db:Connection/db:Protocol)[1]', 'varchar(10)')

   35         AS Protocol,

   36     x.c.value(

   37         '(db:Connection/db:Authentication)[1]', 'varchar(10)')

   38         AS [Authentication]

   39     FROM @x.nodes('/Configuration') x(c)

   40 

   41 /*

   42 OUTPUT:

   43 

   44 NetProvider         Speed     DbProvider           Protocol   Authentication

   45 -------------------- ---------- -------------------- ---------- --------------

   46 World Wide Internet 512 KBPS   SQL Client Provider TCP/IP     Windows

   47 

   48 (1 row(s) affected)

   49 */

Conclusions

This workshop focussed on explaining XML NAMESPACES. We have seen how go generate XML which contains namespace information. We then saw how to read values from an XML variable which contains namespace information.

Resources

Rate

4.67 (6)

You rated this post out of 5. Change rating

Share

Share

Rate

4.67 (6)

You rated this post out of 5. Change rating