DECLARE @.batchXML XML
DECLARE @.docHandle INT
-- <Response xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/windows/test/XMLSchema.xsd">
SET @.batchXML ='<?xml version="1.0" encoding="utf-8"?>
<Response>
<Student>
<Name>Tony</Name><Status>Success</Status>
</Student>
<Student>
<Name>Pam</Name><Status>Fail</Status>
</Student>
<Student>
<Name>Sam</Name><Status>Success</Status>
</Student>
</Response>'
EXECsp_xml_preparedocument @.docHandle OUTPUT, @.batchXML
SELECT NAME, Status FROM OPENXML(@.docHandle, N'/Response/Student')
WITH(NameNVARCHAR(25)'./Name', Status NVARCHAR(25)'./Status')
EXECsp_xml_removedocument @.docHandle
The above query returns the values correctly.
Name Status
Tony Success
Pam Fail
Sam Success
But when I use the root element along namespaces it isn't retuning any values. (commented line above)
Your help is much appreciated.
Thanks,
Loonysan
For SQL Server 2000
EXEC sp_xml_preparedocument @.docHandle OUTPUT, @.batchXML,'<Response xmlns:x="'" href="http://schemas.microsoft.com/windows/test/XMLSchema.xsd"/>'">http://schemas.microsoft.com/windows/test/XMLSchema.xsd"/>'
SELECT NAME, Status FROM OPENXML(@.docHandle, N'/x:Response/x:Student')
WITH (Name NVARCHAR(25) './x:Name', Status NVARCHAR(25) './x:Status')
EXEC sp_xml_removedocument @.docHandle
For SQL Server 2005
;
WITH XMLNAMESPACES(default 'http://schemas.microsoft.com/windows/test/XMLSchema.xsd' )
SELECT x.value('./Name[1]','NVARCHAR(25)') AS Name,
x.value('./Status[1]','NVARCHAR(25)') AS Status
FROM @.batchXML.nodes('/Response/Student') as R(x)
No comments:
Post a Comment