I have the following problem, I need to remove the \"cfdi_\" for \"cfdi:\"
I used the namespaces to solve that but they are duplicated by each node and I can not elimina
It is annoying but valid output... Each sub-select withing a FOR XML
statement will add its own set of namespace declarations. There is a very old - yet ignored! - Microsoft Connect issue Go there and vote for it.
There is no real workaround, just some hacks. Most people end up with some ugly tricks on string level. In this case you create the XML without any namespace and change it with string methods when you are finished.
In your other question you had the idea to add the prefix to your aliases like cfdi_Element
and change this to cfdi:Element
later.
This is exactly what I'm talking about...
But once again: It is not wrong, just annoying and bloating your output...
Try this: declare @Mydoc xml;
WITH xmlnamespaces ('uri' as cfdi)
SELECT @Mydoc = (SELECT
'SomeValue' AS OuterElement,
(SELECT 'OtherValue' AS InnerElement
FOR XML RAW('cfdi:traslado'), TYPE)
FOR XML RAW('cfdi:gatito'), TYPE)
--The namespace declaration is replicated
SELECT @Mydoc;
--But here it "works"
SELECT @Mydoc = (SELECT
'SomeValue' AS OuterElement,
(SELECT 'OtherValue' AS InnerElement
FOR XML RAW('cfdi_traslado'), TYPE)
FOR XML RAW('cfdi_gatito'), TYPE)
--A cast to NVARCHAR(MAX)
, the ns-decl into the root node and some replaces:
SELECT CAST(REPLACE(REPLACE(CAST(@Mydoc AS NVARCHAR(MAX)),'
This is ugly, but often the only chance...