問題描述
我有這個查詢
WITH XMLNAMESPACES(DEFAULT 'https://tribunet.hacienda.go.cr /docs/esquemas/2017/v4.2/facturaElectronica'
,'http://www.w3.org/2001/XMLSchema' AS xsd
,'http://www.w3.org/2001/XMLSchema-instance' AS xsi)
SELECT 1 AS [id]
,0 AS [pass]
(
/*Others*/
SELECT
OT.OTH_MESSAGE as Others
FROM [crdx_COREDev1].[dbo].[OTH_OTHERS] as OT
where
OT.OTH_ID=E.OTH_ID
fOR XML PATH ('Others'), type
)
,0 AS [CONSECUTIVE]
FOR XML PATH('FE');
這會生成這個 XML
<FE xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://tribunet.hacienda.go.cr/docs/esquemas/2017/v4.2 /facturaElectronica"> <- CHANGE 2
<id>1</id>
<pass>0</pass>
<CONSECUTIVE>0</CONSECUTIVE>
<Others xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://tribunet.hacienda.go.cr/docs/esquemas/2017/v4.2 /facturaElectronica">
<MESSAGE>MESSAGE</MESSAGE>
</Others>
</FE>
現在我的問題:我只希望
顯示命名空間,但是 - 正如您在 xml 中看到的 - 聲明也出現在
?
Now my question: I would like only <FE>
to show the namespaces, but - as you see in the xml - that declarations appear also in <Others>
. How can I limit this to <FE>
?
推薦答案
這是一個令人討厭且眾所周知的問題,每當您在 FOR XML
查詢中使用與嵌套子查詢相關的命名空間時,就會發生此問題...
This is an annoying and well known issue and occurs whenever you use namespaces in connection with nested sub-queries in FOR XML
queries...
連接問題已存在 10 多年 - 直到最近消失.
There has been a connect issue for more than 10 years - until it disappaered recently.
值得一提的是,這些重復的命名空間聲明并沒有錯,只是讓您的 XML 變得臃腫.它可能會與(以)嚴格的模式驗證相沖突.
It is important to mention, that these repeated namespace declarations are not wrong, just bloating your XML. And it can collide with (to) strict schema validations.
沒有好的解決方案,只有解決方法:
No good solution, just workarounds:
- 創建沒有命名空間的內部 XML 并在字符串基礎上添加包裝節點,或
- 將命名空間創建為普通屬性(但未命名為
xmlns
)并使用REPLACE
更改名稱.
- Create the inner XML without the namespace and add the wrapping node on string base, or
- Create the namespaces as normal attributes (but not named
xmlns
) and useREPLACE
to change the names.
兩種解決方法都需要轉換為 NVARCHAR(MAX)
并返回到 XML
.
Both workarounds need a conversion to NVARCHAR(MAX)
and back to XML
.
我真的不知道,為什么要以這種方式實現...
I really have no idea, why this was implemented this way...
找一些相關的例子
- 此處
- 和此處
- 和此處
- 和此處
注意:
xmlns="https://tribunet.hacienda.go.cr/docs/esquemas/2017/v4.2 /facturaElectronica">
您正在使用帶空格的命名空間 URL.這是不允許的...
You are using namespace URLs with blanks. This is not allowed...
這篇關于將 xml-namespaces 限制為僅主根的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!