从SQL解析XML文件

从SQL解析XML文件,sql,sql-server,xml,xml-parsing,Sql,Sql Server,Xml,Xml Parsing,嗨,我试图从SQL解析XML文件。但路径似乎找不到数据。我不确定这条路是否正确。下面是我拥有的示例xml文件: if OBJECT_ID('temp') is not null drop table temp CREATE TABLE temp (data xml); insert into temp values (' <Services> <Service Name="AlternativeCreditAttributes"> <C

嗨,我试图从SQL解析XML文件。但路径似乎找不到数据。我不确定这条路是否正确。下面是我拥有的示例xml文件:

   if OBJECT_ID('temp') is not null
drop table temp


CREATE TABLE temp (data xml);
insert into temp values
(' <Services>
    <Service Name="AlternativeCreditAttributes">
      <Categories>
        <Category Name="Default">
          <Attributes>
             <Attribute Name="ACA_TOF_Days_Since_LAST_PAYMENT" Value="15" />
          </Attributes>
        </Category>
      </Categories>
    </Service>
  </Services>
');
GO

正如您正确指出的,XPath完全关闭。以下是
@值
属性检索的起点

SQL

-- DDL and sample data population, start
DECLARE @tbl TABLE (ID INT IDENTITY PRIMARY KEY, xml_data XML);
INSERT INTO @tbl (xml_data)
VALUES ('<Services>
    <Service Name="AlternativeCreditAttributes">
      <Categories>
        <Category Name="Default">
          <Attributes>
             <Attribute Name="ACA_TOF_Days_Since_LAST_PAYMENT" Value="15" />
          </Attributes>
        </Category>
      </Categories>
    </Service>
  </Services>');
-- DDL and sample data population, end

SELECT col.value('(@Value)[1]', 'VARCHAR(100)') AS [Value]
FROM @tbl AS tbl
    CROSS APPLY tbl.xml_data.nodes('/Services/Service[@Name="AlternativeCreditAttributes"]/Categories/Category[@Name="Default"]/Attributes/Attribute[@Name="ACA_TOF_Days_Since_LAST_PAYMENT"]') AS tab(col);

您正在尝试选择
15
?您想要的输出是什么?
-- DDL and sample data population, start
DECLARE @tbl TABLE (ID INT IDENTITY PRIMARY KEY, xml_data XML);
INSERT INTO @tbl (xml_data)
VALUES ('<Services>
    <Service Name="AlternativeCreditAttributes">
      <Categories>
        <Category Name="Default">
          <Attributes>
             <Attribute Name="ACA_TOF_Days_Since_LAST_PAYMENT" Value="15" />
          </Attributes>
        </Category>
      </Categories>
    </Service>
  </Services>');
-- DDL and sample data population, end

SELECT col.value('(@Value)[1]', 'VARCHAR(100)') AS [Value]
FROM @tbl AS tbl
    CROSS APPLY tbl.xml_data.nodes('/Services/Service[@Name="AlternativeCreditAttributes"]/Categories/Category[@Name="Default"]/Attributes/Attribute[@Name="ACA_TOF_Days_Since_LAST_PAYMENT"]') AS tab(col);
+-------+
| Value |
+-------+
|    15 |
+-------+