读取XML文件时,T-SQL返回null

时间:2015-07-24 15:58:18

标签: xml tsql xsd

我很擅长使用XML。我需要导入以下XML,我尝试过的所有内容都返回了空值。

XML:

<instance xmlns="http://www.devicemagic.com/xforms/e3a08280-797d-0132-1d7a-22000b3609ea" xmlns:dm="http://mobileforms.devicemagic.com/xforms" xmlns:dm_device="http://mobileforms.devicemagic.com/xforms/custom_device_attributes" submittingDevice="Android_355794055830490" submissionIdentifier="528f6bf6-1f5e-42aa-bef0-b4c339df755f" writeTime="2015-07-21T08:56:53-0600" formVersion="1.05" dm:submitting_user="Jeremy" dm:submitting_device="Android_355794055830490" dm:submit_time="2015-07-21T14:56:54Z" dm:form="Daily Timesheet &amp; Job Details" dm:form_id="4506512" dm:form_version="1.05" dm:submission_id="7517417">
  <inputs>
    <Work_Date>2015-07-21</Work_Date>
    <Job_Details>
      <Stratty_Tech>John Doe</Stratty_Tech>
      <Job_Number>2222</Job_Number>
      <Type_Of_Work>Course</Type_Of_Work>
      <Directions_To_Location>go here and there</Directions_To_Location>
      <Stand_By_Hours>0</Stand_By_Hours>
      <Set_up_Hours>8</Set_up_Hours>
      <Travel_Hours>0</Travel_Hours>
      <Kilometers>0</Kilometers>
      <Unit_No>122</Unit_No>
      <Include_Personal_Truck>false</Include_Personal_Truck>
      <Jobs_Performed>Labour</Jobs_Performed>
      <Garbage_Removal>false</Garbage_Removal>
    </Job_Details>
    <Include_Shop_TIme_>false</Include_Shop_TIme_>
    <Total_Stand_By_Hours>0</Total_Stand_By_Hours>
    <Total_Set_up_Hours>8</Total_Set_up_Hours>
    <Total_Travel_Hours>0</Total_Travel_Hours>
    <Total_Personal_Truck>0</Total_Personal_Truck>
    <Total_Field_Hours>8</Total_Field_Hours>
    <Total_Hours>8</Total_Hours>
  </inputs>
</instance>

我也尝试过使用SSMS来创建xsd文件,它给我带来了关于不声明匹配命名空间的错误。 理想情况下,我想使用T-SQL自动执行此导入。任何帮助将不胜感激。

编辑。
我能够将它导入XML_Import表之后尝试过这个。

DECLARE @MyXML XML

SET @MyXML = (SELECT CAST(XMLCOL As XML)  FROM XML_Import)


SELECT xmlData.Col.value('Work_Date[1]', 'varchar(max)') As Work_Date
FROM @MyXML.nodes('//instance/inputs[1]') xmlData(Col);

1 个答案:

答案 0 :(得分:1)

问题是由命名空间引起的。您可以使用以下方法之一指定默认命名空间:

SELECT xmlData.Col.value('declare default element namespace "http://www.devicemagic.com/xforms/e3a08280-797d-0132-1d7a-22000b3609ea";Work_Date[1]', 'varchar(max)') As Work_Date
FROM @MyXML.nodes('declare default element namespace "http://www.devicemagic.com/xforms/e3a08280-797d-0132-1d7a-22000b3609ea";//instance/inputs[1]') xmlData(Col);

;WITH XMLNAMESPACES (DEFAULT 'http://www.devicemagic.com/xforms/e3a08280-797d-0132-1d7a-22000b3609ea')
SELECT xmlData.Col.value('Work_Date[1]', 'varchar(max)') As Work_Date
FROM @MyXML.nodes('//instance/inputs[1]') xmlData(Col);

请参阅https://msdn.microsoft.com/en-us/library/ms187013.aspxhttps://msdn.microsoft.com/en-us/library/ms177400.aspx