开发者

How to eliminate superfluous namespace declarations in SQL generated XML?

开发者 https://www.devze.com 2022-12-18 06:46 出处:网络
I\'m fine tuning a web app that calls SOAP services backed by SQL stored procedure calls. Typically the stored procs generate XML that becomes part of the SOAP response, and that XML has many superflu

I'm fine tuning a web app that calls SOAP services backed by SQL stored procedure calls. Typically the stored procs generate XML that becomes part of the SOAP response, and that XML has many superfluous xmlns namespace declarations. In pathological cases this can be 30% or more of the char encoded XML measured in bytes, e.g.:

<GetFooResponse xmlns="http://service.url/">
    <GetFooResult>
        <FooItems xmlns="http://www.thisisalongishurl.com/schema12345/version12345">
            <Item xmlns="http://www.thisisalongishurl.com/schema12345/version12345" data="foo" />
            <Item xmlns="http://www.thisisalongishurl.com/schema12345/version12345" data="foo" />
            <Item xmlns="http://www.thisisalongishurl.com/schema12345/version12345" data="foo" />
            <Item xmlns="http://www.thisisalongishurl.com/schema12345/version12345" data="foo" />
            <Item xmlns="http://www.thisisalongishurl.com/schema12345/version12345" data="foo" />
        </FooItems >
    </GetFooResult>
</GetFooResponse>

The SQL I'm using to generate XML typically follows this pattern:

WITH XMLNAMESPACES(DEFAULT 'http://www.this开发者_如何转开发isalongishurl.com/schema12345/version12345')
SELECT
    [Name],
    [Value]
FROM
    [Foo]
FOR XML PATH('Item'),
TYPE,
ROOT('FooItems');

Is there any way to avoid the generation of superfluous xmlns namespace declations on each Item XML element?

thanks.


WITH    XMLNAMESPACES(
        'http://www.thisisalongishurl.com/schema12345/version12345' AS short,
        DEFAULT 'http://service.url/'
        ),
        [Foo] ([short:Name], [short:Value]) AS
        (
        SELECT  'testName', 'testValue'
        )
SELECT  *
FROM    [Foo]
FOR XML PATH('Item'),
TYPE,
ROOT('FooItems')

returns

<FooItems xmlns="http://service.url/" xmlns:short="http://www.thisisalongishurl.com/schema12345/version12345">
  <Item>
    <short:Name>testName</short:Name>
    <short:Value>testValue</short:Value>
  </Item>
</FooItems>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号