NameSpaces in xml sql

Question

How can I declare the namespaces in a query?

I get an error when declaring the namespaces

Declare @Mydoc xml
set @Mydoc = (
         select importe, algo,XD,
               (select impuesto,tasa,transferencia
                from CfdiDet
                for xml raw('cfdi_detalle'),type)
         from cfdienc
         for xml raw('cfdi_encabezado'),type)

This is the part where I have a problem

set @Mydoc.modify('declare xmlnamespaces ('uri',cfdi')

I want to replace the "_" with ":"


Show source
| xml   | sql-server-2008   | sql   | xml-namespaces   2017-11-29 18:11 1 Answers

Answers to NameSpaces in xml sql ( 1 )

  1. 2017-11-29 22:11

    The namespace prefixes are not just a part of string you could manipulate with a .modify()... Well, you could convert the XML to string, do a REPLACE and convert it back to XML. But the proper way is to declare the namespace.

    To declare a namespace you can either use WITH XMLNAMESPACES or use an implicit namespace declaration (not possible in your case).

    Try this

    DECLARE @Mydoc XML;
    
    WITH XMLNAMESPACES('uri' AS cfdi)
    SELECT @Mydoc=
    (
    SELECT 'SomeDummy' AS importe
    FOR XML RAW('cfdi:encabezado'),TYPE
    );
    
    SELECT @Mydoc 
    

    The result

    <cfdi:encabezado xmlns:cfdi="uri" importe="SomeDummy" />
    

Leave a reply to - NameSpaces in xml sql

◀ Go back