Importer XML dans le server SQL en utilisant la command OPENXML avec XMLNS

J'ai le code suivant pour importer un XML dans SQL

DECLARE @XML AS XML, @hDoc AS INT, @SQL NVARCHAR (MAX) SELECT @XML = XMLData FROM XMLwithOpenXML EXEC sp_xml_preparedocument @hDoc OUTPUT, @XML SELECT rid, uid FROM OPENXML(@hDoc, '/PportTimetable/Journey') WITH ( rid [varchar](50) '@rid', uid [varchar](100) '@uid' ) EXEC sp_xml_removedocument @hDoc GO 

Je peux get le code pour fonctionner mais seulement quand il ne contient pas l'information de xmlns comme vu ci-dessous pourquoi est ceci?

xmlns: xsd = "http://www.w3.org/2001/XMLSchema"

xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"

xmlns = "http://www.thalesgroup.com/rtti/XmlTimetable/v8"

En-tête XML

 <PportTimetable xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" timetableID="20161018020822" xmlns="http://www.thalesgroup.com/rtti/XmlTimetable/v8"> <Journey rid="201610188012733" uid="P12733" trainId="2J27" ssd="2016-10-18" toc="AW"> </Journey> </PportTimetable> 

Je recommand d'ignorer OPENXML éléments OPENXML et d'utiliser le support XQuery natif embedded dans SQL Server:

 declare @input XML = '<PportTimetable xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" timetableID="20161018020822" xmlns="http://www.thalesgroup.com/rtti/XmlTimetable/v8"> <Journey rid="201610188012733" uid="P12733" trainId="2J27" ssd="2016-10-18" toc="AW"> </Journey> </PportTimetable>' -- define your XML namespaces - here, there's only a single "default" namespace ;WITH XMLNAMESPACES(DEFAULT 'http://www.thalesgroup.com/rtti/XmlTimetable/v8') SELECT RID = XC.value('@rid', 'varchar(50)'), UID = XC.value('@uid', 'varchar(20)'), TrainId = XC.value('@trainId', 'varchar(25)'), SSD = XC.value('@ssd', 'varchar(25)'), TOC = XC.value('@toc', 'varchar(20)') FROM @input.nodes('/PportTimetable/Journey') AS XT(XC) 

Utilisez la fonction XQuery .nodes() pour "détruire" votre .nodes() XML en un tableau "en ligne" de fragments XML (un pour chaque nœud <Journey> , dans cet exemple), puis utilisez la fonction .value() pour récupérer l'individu éléments et attributes de ces fragments XML un par un.

Vous devez spécifier l'espace de noms en tant que troisième argument de sp_xml_preparedocument

 DECLARE @XML AS XML='<PportTimetable xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" timetableID="20161018020822" xmlns="http://www.thalesgroup.com/rtti/XmlTimetable/v8"> <Journey rid="201610188012733" uid="P12733" trainId="2J27" ssd="2016-10-18" toc="AW"> </Journey> </PportTimetable>'; DECLARE @hDoc AS INT, @SQL NVARCHAR (MAX); EXEC sp_xml_preparedocument @hDoc OUTPUT, @XML,'<PportTimetable xmlns:b="http://www.thalesgroup.com/rtti/XmlTimetable/v8"/>'; SELECT rid, uid FROM OPENXML(@hDoc, '/b:PportTimetable/b:Journey') WITH ( rid [varchar](50) '@rid', uid [varchar](100) '@uid' ) EXEC sp_xml_removedocument @hDoc GO