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