开发者

XML declaration with "FOR XML PATH" in SQL Server 2005

开发者 https://www.devze.com 2022-12-17 08:14 出处:网络
Below is a si开发者_StackOverflowmplified version of a query that I have already created.The query works fine, but I cannot figure out how to get the XML declaration at the top of the generated XML.I\

Below is a si开发者_StackOverflowmplified version of a query that I have already created. The query works fine, but I cannot figure out how to get the XML declaration at the top of the generated XML. I've tried multiple things and searched far and wide on the Google, but alas I cannot seem to find out how to do this ... or even if it is possible.

select 
    'Dimension' "@type",
    (
        select
            (
                select
                    'X102' "TransactionType",
                    convert(varchar, getdate(), 104) "Transfer/TransferDate",
                    convert(varchar, getdate(), 108) "Transfer/TransferTime"
                for xml path (''), type
            ) "TransactionInformation"
        for xml path (''), type
    )
for xml path ('DimensionImport'), type

Gives me...

<DimensionImport type="Dimension">
    <TransactionInformation>
        <TransactionType>X102</TransactionType>
        <Transfer>
            <TransferDate>21.01.2010</TransferDate>
            <TransferTime>15:46:36</TransferTime>
        </Transfer>
    </TransactionInformation>
</DimensionImport>

I'm wanting...

<?xml version="1.0" encoding="ISO-8859-1" ?>
<DimensionImport type="Dimension">
    <TransactionInformation>
        <TransactionType>X102</TransactionType>
        <Transfer>
            <TransferDate>21.01.2010</TransferDate>
            <TransferTime>15:46:36</TransferTime>
        </Transfer>
    </TransactionInformation>
</DimensionImport>

Thank you in advance for any help you might be able to lend.


It's messy, but you could just concatenate it on the front...

SELECT '<? xml...>' + 
(select 
    'Dimension' "@type",
    (
        select
            (
                select
                    'X102' "TransactionType",
                    convert(varchar, getdate(), 104) "Transfer/TransferDate",
                    convert(varchar, getdate(), 108) "Transfer/TransferTime"
                for xml path (''), type
            ) "TransactionInformation"
        for xml path (''), type
    )
for xml path ('DimensionImport'), type)


try this:

select '<?xml version="1.0" encoding="ISO-8859-1" ?>' + 
       (your whole upper select here)


Unfortunately this is what I found in SQL Server Books Online:

The XML declaration PI in an instance is not preserved when the instance is stored in the database. For example:

Copy Code CREATE TABLE T1 (Col1 int primary key, Col2 xml)
GO
INSERT INTO T1 values (1, '<?xml version="1.0" encoding="windows-1252" ?><doc></doc>')
GO
SELECT Col2
FROM T1

The result is <doc/>.

The only workaround is to return the XML as a varchar(max) type:

select '<?xml version="1.0" encoding="ISO-8859-1" ?>'
+
cast( (
select 
    'Dimension' "@type",
    (
        select
            (
                select
                    'X102' "TransactionType",
                    convert(varchar, getdate(), 104) "Transfer/TransferDate",
                    convert(varchar, getdate(), 108) "Transfer/TransferTime"
                for xml path (''), type
            ) "TransactionInformation"
        for xml path (''), type
    )
for xml path ('DimensionImport'), type) as varchar(max))


Here is what I ended up doing. The data type is not returned as an XML datatype, but I can deal with that I guess.

select '<?xml version="1.0" encoding="ISO-8859-1" ?>' +
(
    select 
        'Dimension' "@type",
        (
            select
                (
                    select
                        'X102' "TransactionType",
                        convert(varchar, getdate(), 104) "Transfer/TransferDate",
                        convert(varchar, getdate(), 108) "Transfer/TransferTime"
                    for xml path (''), type
                )
            for xml path ('TransactionInformation'), type
        ),
        (
            ... queried up data here ...
        )
    for xml path ('DimensionImport')
)


In 2008 R2 from EM, if you open "Save as" and expand "Save" button, then you can save witch encoding, after that, the xml declaration is added automatically.

Hope it helps. Regards.

0

精彩评论

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