XML Workshop 11 - 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 Namespacesand 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 Providersnet:Provider>

6 <net:Speed>512 KBPSnet:Speed>

7 net:Connection>

8 <db:Connection>

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

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

11 <db:Authentication>Windowsdb:Authentication>

12 db:Connection>

13 Configuration>

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

1

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 Providersnet:Provider>

10 <net:Speed>512 KBPSnet:Speed>

11 net:Connection>

12

16 <Connection>

17 <Provider>SQL Client ProviderProvider>

18 <Protocol>TCP/IPProtocol>

19 <Authentication>WindowsAuthentication>

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 XMLmakes 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

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

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

6

7 World Wide Internet Providers

8 512 KBPS

9

10

11 SQL Client Provider

12 TCP/IP

13 Windows

14

15

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

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

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

6

7 World Wide Internet Providers

8 512 KBPS

9

10

11 SQL Client Provider

12 TCP/IP

13 Windows

14

15

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.