【问题标题】:Print XML attribute in SQL Server在 SQL Server 中打印 XML 属性
【发布时间】:2017-01-23 21:21:19
【问题描述】:

我正在尝试从 SQL 表中的 XML 获取属性的值

<?xml version="1.0" encoding="utf-8"?>
<container>
  <Property Name="paramA" Vocabulary="someVocu">
  <Property Name="paramB" Value="valueA" />
  <Property Name="paramC" Value="valueB" />
  </Property>
  <Property Name="paramA" Vocabulary="anotherVocu">
  <Property Name="paramB" Value="valueY" />
  <Property Name="paramC" Value="valueZ" />
  </Property>
</container>


select x.XmlCol.value('(Property[@Name="paramB"]/@Value)[1]', 'varchar(50)')    from tempTbl CROSS APPLY rawxml.nodes('/container') AS x(XmlCol)

我正在尝试打印“valueA”和“valueY”我得到一个 NULL。

我该怎么做?

谢谢

【问题讨论】:

    标签: sql-server-2008 xml-parsing


    【解决方案1】:

    我不能告诉你你的陈述有什么特别的问题,因为我还在学习如何查询 XML。通过引用这个问题,我能够想出一些我认为应该适用的 SQL...

    How to query for Xml values and attributes from table in SQL Server?

    在这里。

    CREATE TABLE tempTbl
    (
        id INT,
        data XML
    )
    
    INSERT INTO dbo.tempTbl
            (id, data)
    SELECT 1, '<?xml version="1.0" encoding="utf-8"?>
    <container>
      <Property Name="paramA" Vocabulary="someVocu">
      <Property Name="paramB" Value="valueA" />
      <Property Name="paramC" Value="valueB" />
      </Property>
      <Property Name="paramA" Vocabulary="anotherVocu">
      <Property Name="paramB" Value="valueY" />
      <Property Name="paramC" Value="valueZ" />
      </Property>
    </container>'
    
    SELECT 
    x.XmlCol.value('@Value', 'varchar(25)') AS Value
    FROM tempTbl AS t
    CROSS APPLY t.data.nodes('/container/Property/Property') AS x(XmlCol)
    WHERE x.XmlCol.value('@Name', 'varchar(25)') = 'paramB'
    

    要了解有关查询 XML 的更多信息,我正在通过 Stairway to XML 工作。

    诺埃尔

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-23
      • 1970-01-01
      • 1970-01-01
      • 2021-08-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多