XML节点值,其中xml数据具有冒号语法

时间:2015-11-10 17:04:47

标签: sql-server xml

我试图将数据从xml列移动到规范化的数据库表,但我无法弄清楚如何读取xml节点。有人可以提供一些帮助。我还没有看到任何使用冒号:语法的例子。

    DECLARE @xmldata XML;

SET @xmldata='<storename:results xmlns:storename="http://www.storename.com/soap/rpc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.storename.com/soap/rpc results.xsd ">
  <storename:testCode>11</storename:testCode>
  <storename:drawNumber>2286</storename:drawNumber>
  <storename:resultset>
    <storename:row>
      <storename:column name="test_code" value="11" />
      <storename:column name="draw_number" value="2286" />
      <storename:column name="draw_status" value="20" />
      <storename:column name="draw_date" value="2006-03-01 00:00:00.0" />
      <storename:column name="draw_time" value="00:00:00" />
      <storename:column name="win_number">2-14-15-16-43-48</storename:column>
    </storename:row>
  </storename:resultset>
</storename:results>'

SELECT @xmldata
SELECT  x.rec.query('./test_code').value('.','varchar(10)') AS xmlresult
FROM @xmldata.nodes('/storename/results/resultset/row/column') AS x(rec)

1 个答案:

答案 0 :(得分:3)

&#34;冒号语法&#34;是一个 XML命名空间,它是XML文档的一部分,因此在查询时也需要尊重和使用它。

我并非100%确定您真正希望从该XML获得的内容 - 此处的示例代码遍历<storename:column>个节点并提取NameValue属性来自他们:

; WITH XMLNAMESPACES('http://www.storename.com/soap/rpc' AS ns)
SELECT 
    ColName = rec.value('@name','varchar(50)'),
    ColValue = rec.value('@value','varchar(50)')
FROM 
    @xmldata.nodes('/ns:results/ns:resultset/ns:row/ns:column') AS x(rec)