【问题标题】:SQL Server - XML parsing returning NULLSQL Server - XML 解析返回 NULL
【发布时间】:2021-04-06 03:02:01
【问题描述】:

难以解析来自第三方生成的xml 的数据。检查XML结构是否没有问题,已经解析了几十个类似的结构XMLs没有任何问题。

XML 得到了这个结构:

 <rss xmlns:media="https://...." version="2.0">
  <channel>
    <title>Title</title>
    <link>Link</link>
    <description>description</description>
    <pubDate>01.01.1900</pubDate>
    <item>
      <guid isPermaLink="false">aaaa</guid>
      <title>Title</title>
      <description>description</description>
      <pubDate>pubDate</pubDate>
      <category>Category</category>
      <enclosure url="https:..." length="72" type="video/mp4" />
      <media:content type="video/mp4" url="https://..." duration="72" lang="en">
        <media:category>AAAA</media:category>
        <media:tags>BBB</media:tags>
        <media:keywords>CCCC</media:keywords>
        <media:thumbnail url="https://...." width="1280" height="720" />
        <media:credit role="producer" scheme="urn:ebu">DDDD</media:credit>
      </media:content>
    </item>

m unable to parse anything which is under media:content. Where full path to it should be /rss/channel/item/media:content/media:thumbnail`,但显然不是。

可能遗漏了一些不经意的东西。

如果有人能就此提出建议,我将不胜感激,并祝大家新年快乐!!! :)

【问题讨论】:

  • “我无法解析“media:content”下的任何内容。”因为您没有声明命名空间。
  • 我做了:;WITH XMLNAMESPACES('search.yahoo.com/mrss' as "media") select xyvalue('media:content[1]/media:thumbnail[1]/@url', 'nvarchar (max)') as [Thubmnail] FROM @xml.nodes('/rss/channel/item') AS x(y) 抱歉忘记添加
  • FROM @xml.nodes('/media:rss/channel/item')

标签: sql-server xml xml-parsing


【解决方案1】:

正如我在评论中提到的,您需要在 SQL 中声明您的命名空间:

DECLARE @XML xml = '<rss xmlns:media="https://...." version="2.0">
  <channel>
    <title>Title</title>
    <link>Link</link>
    <description>description</description>
    <pubDate>01.01.1900</pubDate>
    <item>
      <guid isPermaLink="false">aaaa</guid>
      <title>Title</title>
      <description>description</description>
      <pubDate>pubDate</pubDate>
      <category>Category</category>
      <enclosure url="https:..." length="72" type="video/mp4" />
      <media:content type="video/mp4" url="https://..." duration="72" lang="en">
        <media:category>AAAA</media:category>
        <media:tags>BBB</media:tags>
        <media:keywords>CCCC</media:keywords>
        <media:thumbnail url="https://...." width="1280" height="720" />
        <media:credit role="producer" scheme="urn:ebu">DDDD</media:credit>
      </media:content>
    </item>
  </channel>' + --Assumed exists
'</rss>'; --Assumed exists

WITH XMLNAMESPACES ('https://....' as media) --'https://....' would be replaced with your real namespace
SELECT c.i.value('media:content[1]/media:thumbnail[1]/@url', 'nvarchar(max)') as [Thubmnail]
FROM @xml.nodes('/rss/channel/item') AS c(i);

db<>fiddle

【讨论】:

  • 谢谢拉努,谢谢你我ve found my issue was the http against https in NS. Of course you couldnt 知道,因为我没有把真正的价值观.. 但你指出我在哪里寻找它。再次感谢您,祝先生新年快乐!!! :)
猜你喜欢
  • 2013-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多