Illegal XML Characters When Declaring XML Variable In SQL - sql

The Problem
I am attempting to declare an XML variable and and set it equal to this XML file within my SQL query like this: DECLARE #x XML = 'xml content here';.
However, when I try to do this with my current XML file, I get this error message printed to my screen:
Msg 9420, Level 16, State 1, Line 1
XML parsing: line 1132, character 265, illegal xml character
Ways I've Already Tried To Solve It
I've tried to escape the single quotes by replacing all the single quotes in the XML file with two single quotes and then copying into the query. No luck.
I've tried taking out the ampersands, but that doesn't seem to work. Near where the error is detected by SQL, there don't seem to be any special characters. No luck again.
The Question
It seems to me that single quotes aren't the problem. I am having trouble finding exactly what the illegal character is, how I filter these characters out. Maybe importing the XML file from an external file and setting the contents equal to the variable instead of heard coding it will solve the issue. How would this work? I am also aware that single quotes have to be replaced by two single quotes. I did that, but I still have the exact same problem. Any help understanding the issue is appreciated.
System Parameters
MS SQL Server Management Studio
Windows Server 2012 R2 Standard
The Full Code
Here's the full code that I'm executing that's giving me the error:
DECLARE #x XML = ' copy xml file here... ';
With MyPersonCTE AS
(
SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS PersonID
,p.value('FirstName[1]','varchar(max)') AS FirstName
,p.value('LastName[1]','varchar(max)') AS LastName
,p.value('Biography[1]','varchar(max)') AS Biography
,p.value('Expertise[1]','varchar(max)') AS Expertise
,p.value('Image[1]','varchar(max)') AS Image
,p.value('Link[1]','varchar(max)') AS Link
,p.query('Books') AS BookNode
,p.query('Articles') AS ArticleNode
--same for Papers, Artwork...
FROM #x.nodes('/People/Person') AS A(p)
)
,MyBooksCTE AS
(
SELECT MyPersonCTE.*
,ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS BookID
,x.value('Year[1]','int') AS BookYear
,x.value('Details[1]','varchar(max)') AS BookDetails
FROM MyPersonCTE
CROSS APPLY MyPersonCTE.BookNode.nodes('/Books/Book') A(x)
)
,MyArticlesCTE AS
(
SELECT MyPersonCTE.*
,ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS ArticleID
,x.value('Year[1]','int') AS ArticleYear
,x.value('Details[1]','varchar(max)') AS ArticleDetails
FROM MyPersonCTE
CROSS APPLY MyPersonCTE.ArticleNode.nodes('/Articles/Article') A(x)
)
--same for Papers, Artwork...
SELECT p.*
,b.BookID
,b.BookYear
,b.BookDetails
,a.ArticleID
,a.ArticleYear
,a.ArticleDetails
INTO #tempAllData
FROM MyPersonCTE AS p
LEFT JOIN MyBooksCTE AS b ON p.PersonID=b.PersonID
LEFT JOIN MyArticlesCTE AS a ON p.PersonID=a.PersonID ;
--#tempAllData is now filled with all data, copied in all combination: much to much
--but DISTINCT is your friend
--in this case you'd use the PersonID as FK in all related tables
SELECT DISTINCT PersonID,FirstName,LastName,Biography,Expertise --other fields
FROM #tempAllData;
SELECT DISTINCT PersonID,BookID,BookYear,BookDetails
FROM #tempAllData;
SELECT DISTINCT PersonID,ArticleID,ArticleYear,ArticleDetails
FROM #tempAllData;
DROP TABLE #tempAllData;

Look, this is a reduced example to reproduce the error
In this snippet you've got twice the name "O’Brien" where the ’ makes troubles
And here “Experiencing physical warmth promotes interpersonal warmth” the opening and closing qoutes are problematic
DECLARE #x XML=
'<?xml version="1.0" encoding="UTF-8"?>
<People>
<Person>
<FirstName>Katherine</FirstName>
<LastName>Corker</LastName>
<Articles>
<Article>
<Year></Year>
<Details><![CDATA[<p>Corker, K. S., Lynott, D., Wortman, J., Connell, L., Donnellan, M. B., Lucas, R. E., & O’Brien, K. (2014). High quality direct replications matter: Response to Williams (2014). Social Psychology, 45, 324-326. Available here.</p>
<p>Lynott, D., Corker, K. S., Wortman, J., Connell, L., Donnellan, M. B., Lucas, R. E., & O’Brien, K. (2014). Replication of “Experiencing physical warmth promotes interpersonal warmth” by Williams & Bargh (2008, Science). Social Psychology, 45, 216-222. Available here.</p>]]></Details>
</Article>
</Articles>
</Person>
</People>';
SELECT #x;
And now just change this
'<?xml version="1.0" encoding="UTF-8"?>
to this (don't forget the "N" in the beginning to force this to Unicode)
N'<?xml version="1.0" encoding="UTF-16"?>
But just try this:
DECLARE #yourXML AS XML=
(
SELECT CONVERT(XML, BulkColumn,2) AS BulkColumn
FROM OPENROWSET(BULK 'X:\Path2file\faculty-xml.xml', SINGLE_BLOB) AS x
);
SELECT #yourXML;
With this you do not have to bother about encondings, you just read the file from a location on the disk...

Related

Can't parse XML with outer apply

I have an XML column in a table which i am trying to parse out values from to flat table structure.
I am trying to input the XML here but stackoverflow ses it as code and when i try and format as code it still won't accept it.
I can't even get data from "Header" level.
<RequestMessage xmlns="http://iec.ch/TC57/2011/schema/message" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Message.xsd">
<Header>
<Verb>created</Verb>
<Noun>MeterReadings</Noun>
<Timestamp>2021-03-08T00:57:18+01:00</Timestamp>
<Source>Ipsum Lorum</Source>
<AsyncReplyFlag>true</AsyncReplyFlag>
<AckRequired>true</AckRequired>
<MessageID>Ipsum Lorum</MessageID>
<CorrelationID />
</Header>
<Payload>
<MeterReadings xmlns:MeterReadings="http://iec.ch/TC57/2011/MeterReadings#" xmlns="http://iec.ch/TC57/2011/MeterReadings#">
<MeterReading>
<IntervalBlocks>
<IntervalReadings>
<timeStamp>2021-03-07T01:00:00+01:00</timeStamp>
<value>480.196</value>
<ReadingQualities>
<ReadingQualityType ref="3.0.0" />
</ReadingQualities>
</IntervalReadings>
<IntervalReadings>
<ReadingType ref="11.0.7.3.1.2.12.1.1.0.0.0.0.101.0.3.72.0" />
</IntervalReadings>
</IntervalBlocks>
<Meter>
<mRID>0000000000000</mRID>
<status>
<remark>Ipsum Lorum</remark>
<value>ESP</value>
</status>
</Meter>
<UsagePoint>
<mRID>73599900000000</mRID>
</UsagePoint>
</MeterReading>
</MeterReadings>
</Payload>
</RequestMessage>
I am not able to parse it and i have tried using examples from other threads. I am trying to not use OPENXML solution because requires DECLARE and executing the built in procedure for clearing the XML from memmory periodically. I am trying to use the OUTER APPLY solution.
Like Shugos solution in How to parse XML data in SQL server table or Query XML with nested nodes on Cross Apply.
It doesn't work.
It returns null for the timestamp column.
select
t.file_created_time
,c.value('(Timestamp)[1]','varchar(max)') as timestamp
from load.t t
OUTER APPLY t.xml_data.nodes('RequestMessage/Header') as m(c)
Please try the following solution.
Starting from SQL Server 2005 onwards, it is better to use XQuery language, based on the w3c standards, while dealing with the XML data type.
Microsoft proprietary OPENXML and its companions sp_xml_preparedocument and sp_xml_removedocument are kept just for backward compatibility with the obsolete SQL Server 2000. Their use is diminished just to very few fringe cases.
I had to comment out the following tag <!--<IntervalReadings>--> to make your XML well-formed.
XML Header fragment has a default namespace:
xmlns="http://iec.ch/TC57/2011/schema/message"
XML Payload fragment has its own two additional namespaces:
xmlns:MeterReadings="http://iec.ch/TC57/2011/MeterReadings#"
xmlns="http://iec.ch/TC57/2011/MeterReadings#"
Namespaces should be taken into account.
Check it out below.
SQL
DECLARE #tbl TABLE (ID INT IDENTITY PRIMARY KEY, xml_data XML);
INSERT INTO #tbl (xml_data) VALUES
(N'<RequestMessage xmlns="http://iec.ch/TC57/2011/schema/message"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="Message.xsd">
<Header>
<Verb>created</Verb>
<Noun>MeterReadings</Noun>
<Timestamp>2021-03-08T00:57:18+01:00</Timestamp>
<Source>Ipsum Lorum</Source>
<AsyncReplyFlag>true</AsyncReplyFlag>
<AckRequired>true</AckRequired>
<MessageID>Ipsum Lorum</MessageID>
<CorrelationID/>
</Header>
<Payload>
<MeterReadings xmlns:MeterReadings="http://iec.ch/TC57/2011/MeterReadings#"
xmlns="http://iec.ch/TC57/2011/MeterReadings#">
<MeterReading>
<IntervalBlocks>
<IntervalReadings>
<timeStamp>2021-03-07T01:00:00+01:00</timeStamp>
<value>480.196</value>
<ReadingQualities>
<ReadingQualityType ref="3.0.0"/>
</ReadingQualities>
</IntervalReadings>
<!--<IntervalReadings>-->
<ReadingType ref="11.0.7.3.1.2.12.1.1.0.0.0.0.101.0.3.72.0"/>
</IntervalBlocks>
<Meter>
<mRID>0000000000000</mRID>
<status>
<remark>Ipsum Lorum</remark>
<value>ESP</value>
</status>
</Meter>
<UsagePoint>
<mRID>73599900000000</mRID>
</UsagePoint>
</MeterReading>
</MeterReadings>
</Payload>
</RequestMessage>');
-- DDL and sample data population, end
WITH XMLNAMESPACES(DEFAULT 'http://iec.ch/TC57/2011/schema/message')
SELECT id
, c.value('(Noun/text())[1]','VARCHAR(30)') AS Noun
, c.value('(Timestamp/text())[1]','DATETIMEOFFSET(0)') AS [timestamp]
FROM #tbl
CROSS APPLY xml_data.nodes('/RequestMessage/Header') AS t(c);
Output
+----+---------------+----------------------------+
| id | Noun | timestamp |
+----+---------------+----------------------------+
| 1 | MeterReadings | 2021-03-08 00:57:18 +01:00 |
+----+---------------+----------------------------+
You need to respect and include the XML namespace in your XML document in your XQuery!
<RequestMessage xmlns="http://iec.ch/TC57/2011/schema/message"
**********************************************
Try something like this:
WITH XMLNAMESPACES(DEFAULT N'http://iec.ch/TC57/2011/schema/message')
SELECT
t.id,
c.value('(Timestamp)[1]','varchar(max)') as timestamp
FROM
load.t t
CROSS APPLY
t.xml_data.nodes('RequestMessage/Header') AS m(c)
Also when trying to run this on my SQL Server, I get an error that the XML as shown is malformed.....
UPDATE:
If you need to also access bits in the Payload section - you need to also respect that XML namespace there:
<MeterReadings xmlns:MeterReadings="http://iec.ch/TC57/2011/MeterReadings#"
xmlns="http://iec.ch/TC57/2011/MeterReadings#">
***********************************************
Try this:
WITH XMLNAMESPACES(N'http://iec.ch/TC57/2011/schema/message' as hdr,
N'http://iec.ch/TC57/2011/MeterReadings#' as mr)
SELECT
t.id,
c.value('(hdr:Timestamp)[1]', 'varchar(50)') AS timestamp,
col.value('(mr:MeterReading/mr:IntervalBlocks/mr:IntervalReadings/mr:timeStamp)[1]', 'varchar(50)') AS MeterReadingsTimestamp
FROM
load.t t
CROSS APPLY
t.xml_data.nodes('/hdr:RequestMessage/hdr:Header') AS m(c)
CROSS APPLY
t.xml_data.nodes('/hdr:RequestMessage/hdr:Payload/mr:MeterReadings') AS mr(col)

SSIS Extraction/ DTSX

I got this amazing code here on Stack but it was 8 years ago but works perfectly but, when run the t-script the column ObjectName always is NULL, someone can help me to fix it if possible?
The SQL Script
SELECT Pkg.props.value('../../DTS:Property[#DTS:Name="ObjectName"]
[1]','varchar(MAX)') ObjectName,
Pkg.props.value('(#SQLTask:SqlStatementSource)[1]', 'NVARCHAR(MAX)') AS
SqlStatement FROM (select cast(pkgblob.BulkColumn as XML) pkgXML from
openrowset(bulk '\\MYDTS.dtsx',single_blob)
as pkgblob) t CROSS APPLY pkgXML.nodes('//DTS:ObjectData//SQLTask:SqlTaskData') Pkg(props)
UNION
SELECT Pkg.props.value('../../../../DTS:Property[#DTS:Name="ObjectName"]
[1]','varchar(MAX)') ObjectName,
Pkg.props.value('data(./properties/property[#name=''SqlCommand''])[1]',
'varchar(max)') SqlStatement FROM(select cast(pkgblob.BulkColumn as XML)
pkgXML from openrowset(bulk '\\MYDTS.dtsx',single_blob) as
pkgblob) t CROSS APPLY
pkgXML.nodes('//DTS:Executable//pipeline//components//component') Pkg(props)
WHERE Pkg.props.value('data(./properties/property[#name=''SqlCommand''])
[1]', 'varchar(max)') <>''
And the result has been attached below. I would like to show the ObjectName instead NULL and I don't know how I can fix it.
ObjectName is no longer what/where it use to be from those years ago.
You'll need to browse the current dtsx in XML form and find your equivalent ObjectName.
SELECT CONVERT(XML, BulkColumn) AS BulkColumn FROM OPENROWSET(BULK 'C:\PathToPackage\MYDTS.dtsx', SINGLE_BLOB) AS x;
My sample dtsx looks like this in XML form:
<DTS:Executable xmlns:DTS="www.microsoft.com/SqlServer/Dts" DTS:refId="Package" DTS:CreationDate="7/23/2019 7:23:29 AM" DTS:CreationName="Microsoft.Package" DTS:CreatorComputerName="Overflow" DTS:CreatorName="Overflow\Stack" DTS:DTSID="{DEED1FFC-FCE3-4C63-A90C-3382EDA388A1}" DTS:ExecutableType="Microsoft.Package" DTS:LastModifiedProductVersion="14.0.3002.113" DTS:LocaleID="1033" DTS:ObjectName="deduplicate" DTS:PackageType="5" DTS:VersionBuild="20" DTS:VersionGUID="{27B38889-4D80-45F6-9CB0-F6B430B1DBBE}">
<DTS:Property DTS:Name="PackageFormatVersion">8</DTS:Property>
<DTS:Variables />
<DTS:Executables>
<DTS:Executable DTS:refId="Package\Execute SQL Task" DTS:CreationName="Microsoft.ExecuteSQLTask" DTS:Description="Execute SQL Task" DTS:DTSID="{A6053AE9-7445-4973-9AFD-E3AFFE97B68F}" DTS:ExecutableType="Microsoft.ExecuteSQLTask" DTS:LocaleID="-1" DTS:ObjectName="Execute SQL Task" DTS:TaskContact="Execute SQL Task; Microsoft Corporation; SQL Server 2017 RC1; © 2017 Microsoft Corporation; All Rights Reserved;http://www.microsoft.com/sql/support/default.asp;1" DTS:ThreadHint="1">
<DTS:Variables />
<DTS:ObjectData>
<SQLTask:SqlTaskData xmlns:SQLTask="www.microsoft.com/sqlserver/dts/tasks/sqltask" SQLTask:SqlStatementSource="Select GetDate()" />
</DTS:ObjectData>
</DTS:Executable>
...
In this specific XML, the next todos:
Pull out everything that is a SqlTaskData
Retrieve the SqlStatementSource value
Walk up two levels "../../" to DTS:Executable and Retrieve the ObjectName Attribute
WITH XMLNAMESPACES(
'www.microsoft.com/SqlServer/Dts' AS DTS,
'www.microsoft.com/sqlserver/dts/tasks/sqltask' AS SQLTask
),
dtsxAsXML AS (
SELECT CONVERT(XML, BulkColumn) AS BulkColumn
FROM OPENROWSET(BULK 'C:\PathToPackage\MYDTS.dtsx', SINGLE_BLOB) AS x
)
SELECT
X.Y.value('(../../#DTS:ObjectName)[1]', 'NVARCHAR(MAX)') AS ObjectName,
X.Y.value('(#SQLTask:SqlStatementSource)[1]', 'NVARCHAR(MAX)') AS SqlStatementSource
FROM dtsxAsXML d
CROSS APPLY d.BulkColumn.nodes('//DTS:ObjectData//SQLTask:SqlTaskData') X(Y)
ObjectName
SqlStatementSource
Execute SQL Task
Select GetDate()
Repeat this process for the dataflow component, the bottom union in your example.
Since you're already getting your SQL Statements, you don't need to use these example queries, you just need to review the xml from your dtsx, then find and change the relative paths in your existing query to the value you want.

Parsing XML Data Into SQL Server

I am struggling with importing XML Data into SQL Server 2016. I have tried a few things, but keep either getting errors or just no data is returned.
I have this XML Data stored in an XML file (limited the data because it is pretty sensitive:
<?xml version='1.0' encoding='UTF-8'?>
<wd:Report_Data xmlns:wd="urn:com.workday.report/Worker_Details_-_EXPORT_-_Workplace">
<wd:Report_Entry>
<wd:Active_Status>0</wd:Active_Status>
<wd:Legal_Name_-_First_Name>Charlotte</wd:Legal_Name_-_First_Name>
<wd:Position>Executive Housekeeper I</wd:Position>
<wd:Worker_Management_Level>Supervisor</wd:Worker_Management_Level>
<wd:continuous_service_date>1979-04-29-08:00</wd:continuous_service_date>
<wd:Hire_Date>1979-04-29-08:00</wd:Hire_Date>
<wd:termination_date>2019-12-22-08:00</wd:termination_date>
<wd:Anniversary_Month>04</wd:Anniversary_Month>
<wd:Years_of_Service>40</wd:Years_of_Service>
<wd:Employee_Type>Hotel</wd:Employee_Type>
<wd:Time_Type>Full Time</wd:Time_Type>
<wd:Pay_Rate_Type>Salary</wd:Pay_Rate_Type>
<wd:Marital_Status>Single</wd:Marital_Status>
</wd:Report_Entry>
<wd:Report_Entry>
<wd:Active_Status>0</wd:Active_Status>
<wd:Legal_Name_-_First_Name>Robert</wd:Legal_Name_-_First_Name>
<wd:Cost_Center_-_Name>Electronics</wd:Cost_Center_-_Name>
<wd:Work_Address_-_State_Province>Missouri</wd:Work_Address_-_State_Province>
<wd:Position>Manager Of Voice Networks</wd:Position>
<wd:Worker_Management_Level>Manager</wd:Worker_Management_Level>
<wd:continuous_service_date>1980-02-25-08:00</wd:continuous_service_date>
<wd:Hire_Date>1980-02-25-08:00</wd:Hire_Date>
<wd:termination_date>2020-03-22-07:00</wd:termination_date>
<wd:Anniversary_Month>02</wd:Anniversary_Month>
<wd:Years_of_Service>40</wd:Years_of_Service>
<wd:Employee_Type>Corporate</wd:Employee_Type>
<wd:Time_Type>Full Time</wd:Time_Type>
<wd:Pay_Rate_Type>Salary</wd:Pay_Rate_Type>
<wd:Marital_Status>Married</wd:Marital_Status>
</wd:Report_Entry>
</wd:Report_Data>
I have this code that I am trying to use, but keep getting just an empty result:
SELECT
XMLCol.ReportEntry.query('Active_Status').value('.', 'VARCHAR(20)') AS ActiveStatus
FROM
(SELECT
CAST(XMLCol AS XML)
FROM
OPENROWSET(BULK '\\afcn2011\root\DATA\VisualCron\Employee Export\EmployeeExport.xml', SINGLE_BLOB) AS T(XMLCol)
) AS T(XMLCol)
CROSS APPLY
XMLCol.nodes('Report_Data/Report_Entry') AS XMLCol(ReportEntry);
You need to respect and include the XML namespace defined in your document.
Try something like this:
-- define the namespace and give it a prefix - here "wd"
;WITH XMLNAMESPACES ('urn:com.workday.report/Worker_Details_-_EXPORT_-_Workplace' as wd)
SELECT
-- you need to include namespace prefix when referring to the XML element
-- also: is "VARCHAR(20)" really the best datatype?? Looks more like "INT" to me ...
XMLCol.ReportEntry.value('(wd:Active_Status/text())[1]', 'VARCHAR(20)') AS ActiveStatus
FROM
(SELECT
CAST(XMLCol AS XML)
FROM
OPENROWSET(BULK '\\afcn2011\root\DATA\VisualCron\Employee Export\EmployeeExport.xml', SINGLE_BLOB) AS T(XMLCol)
) AS T(XMLCol)
CROSS APPLY
-- you need to include namespace prefix in your XPath expression
XMLCol.nodes('/wd:Report_Data/wd:Report_Entry') AS XMLCol(ReportEntry);

SQL Server -XML nodes not returning multiple records

I'm trying to get my head round SQL, XML nodes and Cross apply, i would have thought that the following code would return 3 rows of data but instead i only get one. i had thought that using Cross apply to join the query back onto itself would solve this for me.
Can someone tell me what I'm doing wrong?
Thanks in advance
Dan
DECLARE #XML AS XML;
SELECT #XML = ('<Deal deal_id="821">
<Application_owner>me</Application_owner>
<Deal_files>
<File>file1.doc</File>
<File>file2.pdf</File>
<File>file3.xcl</File>
</Deal_files>
</Deal>
')
select
t.n.value('#deal_id', 'int') as [ID],
f.n.value('(File)[1]','varchar(50)') as [FILENAME]
from
#XML.nodes('Deal') t(n)
cross apply
t.n.nodes('Deal_files') f(n)
You were close
DECLARE #XML AS XML;
SELECT #XML = ('<Deal deal_id="821">
<Application_owner>me</Application_owner>
<Deal_files>
<File>file1.doc</File>
<File>file2.pdf</File>
<File>file3.xcl</File>
</Deal_files>
</Deal>
'
)
Select
ID = t.n.value('#deal_id', 'int'),
FileName = f.n.value('(.)[1]','varchar(50)')
From
#XML.nodes('Deal') t(n)
Cross Apply
t.n.nodes('Deal_files/*') f(n)
Returns
ID FileName
--------------
821 file1.doc
821 file2.pdf
821 file3.xcl

Does nodes() or openxml returns rows in same order as it finds in xml?

I have an xml which i need to parse using openxml or nodes(). The xml contains few child tags that repeat with different values, as below.
<root>
<value>10</value>
<value>12</value>
<value>11</value>
<value>1</value>
<value>15</value>
<root>
For my code it is very important that i get all these rows returned in same order as in xml. I googled and gogled but nothing tells me if the #mp:id is always returned in same order as in xml. Or if nodes() return values in same order as it encounters them.
All I want to know if I can trust any of those two methods and be happy with proper order of rows.
P.S. excuse any errors or mistakes in above text, I dont enjoy typing codes in an android window either.
You can use row_number on the shredded XML like this.
declare #XML xml=
'<root>
<value>10</value>
<value>12</value>
<value>11</value>
<value>1</value>
<value>15</value>
</root>'
select value
from
(
select T.N.value('.', 'int') as value,
row_number() over(order by T.N) as rn
from #xml.nodes('/root/value') as T(N)
) as T
order by T.rn
Uniquely Identifying XML Nodes with DENSE_RANK
Update:
You can also use a numbers table like this;
declare #XML xml=
'<root>
<value>10</value>
<value>12</value>
<value>11</value>
<value>1</value>
<value>15</value>
</root>';
with N(Number) as
(
select Number
from master..spt_values
where type = 'P'
)
select #XML.value('(/root/value[sql:column("N.Number")])[1]', 'int')
from N
where N.Number between 1 and #XML.value('count(/root/value)', 'int')
order by N.Number
XPath allows you to select nodes explicitly by ordinal: '/root[1]/value[1]' is the first element, '/root[1]/value[2]' is the second etc. Also could use '(/root/value)[1]' and '(/root/value[2])'. This way you can select exactly the element you want, and selecting element 1 then element 2 then element 3 etc will give you controlled order. Slow, but controlled.
Updated P.S. Wouldn't this be nice to be true?
declare #x xml = '<root>
<value>10</value>
<value>12</value>
<value>11</value>
<value>1</value>
<value>15</value>
<root>';
select x.value(N'position()', N'int') as position,
x.value(N'.', 'int') as value
from #x.nodes(N'//root/value') t(x)
Unfortunately, is not...
Msg 2371, Level 16, State 1, Line 9
XQuery [value()]: 'position()' can only be used within a predicate or XPath selector
And the existence of this error makes me worry that order may be broken sometimes...