I want to be able to produce the following namesspaces and types for an XML root element
我希望能够为XML根元素生成以下名称空间和类型
<BaseTransactionRequest xmlns="http://schemas.datacontract.org/2004/07/SomeCompany" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" i:type="AType">
Typically the first 2 (that is, not including i:type="AType") can be produced without issue (with some tradeoffs, when using custom namespaces we cant represent nulls using the xmlns:ni namespace etc) So, the latter type is problematic. For a referesher, the WITH XMLNAMESPACES fearure is used like below (FOR XML part omitted):
通常,前两个(即不包括i:type =“AType”)可以毫无问题地生成(有一些权衡,当使用自定义命名空间时,我们不能使用xmlns:ni命名空间等来表示空值)所以,后一种类型是有问题的。对于参考者,WITH XMLNAMESPACES恐惧使用如下(FOR XML部分省略):
;WITH XMLNAMESPACES ('http://www.w3.org/2001/XMLSchema-instance' as i, DEFAULT 'http://schemas.datacontract.org/2004/07/SomeCompany',
;使用XMLNAMESPACES('http://www.w3.org/2001/XMLSchema-instance'作为我,DEFAULT'http://schemas.datacontract.org/2004/07/SomeCompany',
A solution to overcome was to write XML "literally" using string concatenation. But I believe and hope FOR XML and this can be used together.
要克服的解决方案是使用字符串连接“逐字地”编写XML。但我相信并希望FOR XML和它可以一起使用。
EDIT: First cut was added in a real rush. Apologies. EDIT2: Dyslexic fix
编辑:第一次切割是真正的加入。道歉。 EDIT2:阅读障碍修复
1 个解决方案
#1
1
Your question is not very clear... You might have a misconception about your i:type="AType"
. This is not a namespace (whatever a custom namespace is), but a normal attribute, named type
living in your namespace i
, which is declared at xmlns:i="blah"
.
你的问题不是很清楚......你可能对你的i:type =“AType”有误解。这不是命名空间(无论自定义命名空间是什么),而是一个普通属性,名为type in living in your namespace i,在xmlns中声明:i =“blah”。
Try this
WITH XMLNAMESPACES ('http://www.w3.org/2001/XMLSchema-instance' as i
,DEFAULT 'http://schemas.datacontract.org/2004/07/SomeCompany')
SELECT 'AType' AS [@i:type]
FOR XML PATH('BaseTransactionRequest');
The result is a self closing tag, declaring two namespaces and containing your attribute:
结果是一个自闭标签,声明两个名称空间并包含您的属性:
<BaseTransactionRequest xmlns="http://schemas.datacontract.org/2004/07/SomeCompany"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
i:type="AType" />
#1
1
Your question is not very clear... You might have a misconception about your i:type="AType"
. This is not a namespace (whatever a custom namespace is), but a normal attribute, named type
living in your namespace i
, which is declared at xmlns:i="blah"
.
你的问题不是很清楚......你可能对你的i:type =“AType”有误解。这不是命名空间(无论自定义命名空间是什么),而是一个普通属性,名为type in living in your namespace i,在xmlns中声明:i =“blah”。
Try this
WITH XMLNAMESPACES ('http://www.w3.org/2001/XMLSchema-instance' as i
,DEFAULT 'http://schemas.datacontract.org/2004/07/SomeCompany')
SELECT 'AType' AS [@i:type]
FOR XML PATH('BaseTransactionRequest');
The result is a self closing tag, declaring two namespaces and containing your attribute:
结果是一个自闭标签,声明两个名称空间并包含您的属性:
<BaseTransactionRequest xmlns="http://schemas.datacontract.org/2004/07/SomeCompany"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
i:type="AType" />