【问题标题】:parsing xml using sql server使用 sql server 解析 xml
【发布时间】:2016-03-02 18:07:23
【问题描述】:

需要一些帮助来解析 SQL 服务器中以下 XML 中的段落元素中的文本。

<FlowDocument PagePadding="5,5,5,5" Name="RTDocument" AllowDrop="True" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">   
  <Paragraph>Licence Number: 04467</Paragraph>
  <Paragraph>Licence Number: 3535333</Paragraph>
</FlowDocument>

请分享您的任何疑问。

谢谢

【问题讨论】:

  • 下面是 XML schemas.microsoft.com/winfx/2006/xaml/presentation"> 许可证号:04467 许可证号:dddd
  • 请将 XML 放入您的问题中,而不是 cmets 中。另外:stackoverflow.com/help/how-to-ask
  • 嗨@SidharthSoneja,如果这个或任何答案已经解决了您的问题,请点击复选标记考虑accepting it。这向更广泛的社区表明您已经找到了解决方案,并为回答者和您自己提供了一些声誉。没有义务这样做。

标签: sql sql-server-2008 sqlxml


【解决方案1】:

一种方法是:(如果它们具有相同的命名空间)

;with xmlnamespaces(default 'schemas.microsoft.com/winfx/2006/xaml/presentation')
select @xml.value('(/FlowDocument/Paragraph)[1]', 'varchar(max)') + ' ' + 
    @xml.value('(/FlowDocument/Paragraph)[2]', 'varchar(max)')

另一种方式:

select data.col.value('(*:Paragraph)[1]','varchar(100)') 
    + ' ' +  data.col.value('(*:Paragraph)[2]','varchar(100)') as ParamName
FROM @xml.nodes('(*:FlowDocument)') as data(col) 

【讨论】:

    【解决方案2】:

    或者(补充罗伯托的答案)

    ;WITH XMLNAMESPACES(DEFAULT 'schemas.microsoft.com/winfx/2006/xaml/presentation')
    
    SELECT  FlowDocument.Paragraph.value('.', 'varchar(MAX)')
    FROM    @xml.nodes('//FlowDocument/Paragraph') AS FlowDocument(Paragraph)
    

    如果有很多段落标签

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多