Question

Below is a simplified 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.

Was it helpful?

Solution 4

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')
)

OTHER TIPS

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))

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top