将 xml-namespaces 限制为仅主根

Limit xml-namespaces to only the main root(将 xml-namespaces 限制为仅主根)
本文介绍了将 xml-namespaces 限制为仅主根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个查询

 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 use REPLACE 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 限制为仅主根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
SSIS: Model design issue causing duplications - can two fact tables be connected?(SSIS:模型设计问题导致重复-两个事实表可以连接吗?)
SQL Server Graph Database - shortest path using multiple edge types(SQL Server图形数据库-使用多种边类型的最短路径)
Invalid column name when using EF Core filtered includes(使用EF核心过滤包括时无效的列名)
How should make faster SQL Server filtering procedure with many parameters(如何让多参数的SQL Server过滤程序更快)
How can I generate an entity–relationship (ER) diagram of a database using Microsoft SQL Server Management Studio?(如何使用Microsoft SQL Server Management Studio生成数据库的实体关系(ER)图?)