在SQL Server 2008中使用命名空间解析XML

时间:2018-06-08 21:39:58

标签: sql-server-2008 xml-parsing xml-namespaces

我在表格中跟随xml。我想获得ID节点的fv: 是<fv ID="Description">还是不

<DataFormItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="bb_appfx_dataforms">
  <Values>
    <fv ID="JobInterval">
      <Value xsi:type="xsd:int">10</Value>
    </fv>
    <fv ID="JobEnabled">
      <Value xsi:type="xsd:boolean">false</Value>
    </fv>
    <fv ID="JobName">
      <Value xsi:type="xsd:string">Default transactional email process</Value>
    </fv>
    <fv ID="ScheduleFrequencySubDayType">
      <Value xsi:type="xsd:int">2</Value>
    </fv>
  </Values>
</DataFormItem>

1 个答案:

答案 0 :(得分:0)

假设您的表名为YourTable,并且XML列名为XmlData(根据您的实际情况进行调整和更改!),您可以尝试使用此XQuery代码:

-- declare the XML namespace as the DEFAULT
;WITH XMLNAMESPACES(DEFAULT 'bb_appfx_dataforms')
    SELECT
        -- extract the value from the <Value> subnode of the <fv> node, if found
        FvValue = XC.value('(Value)[1]', 'varchar(200)')
    FROM
        dbo.YourTable
    CROSS APPLY
        -- get a list of XML fragments - one for each <fv> node
        XmlData.nodes('/DataFormItem/Values/fv') AS XT(XC)
    WHERE
        -- find the XML fragment with the ID="Description"
        XC.value('@ID', 'varchar(50)') = 'Description'