简体   繁体   English

为sp_xml_preparedocument配置名称空间

[英]Configuring namespace for sp_xml_preparedocument

I have an RSS xml with this format: 我有一个这种格式的RSS xml:

<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
  <channel>
    <title></title>
    <link></link>
    <description></description>
    <language></language>
    <lastBuildDate></lastBuildDate>
    <generator></generator>
    <docs></docs>
    <managingEditor></managingEditor>
    <webMaster></webMaster>
    <ttl></ttl>
    <item>
      <title></title>
      <link></link>
      <description></description>
      <guid isPermaLink="false"></guid>
      <pubDate></pubDate>
      <author></author>

      <dc:date></dc:date>
      <dc:publisher></dc:publisher>
      <dc:language></dc:language>

    </item>
    <item>
      <title></title>
      <link></link>
      <description></description>
      <guid isPermaLink="false"></guid>
      <pubDate></pubDate>
      <author></author>

      <dc:date></dc:date>
      <dc:publisher></dc:publisher>
      <dc:language></dc:language>

    </item>
   </channel>
</rss>

And I want to parse it with sp_xml_preparedocument in SQLServer. 我想在SQLServer中使用sp_xml_preparedocument解析它。 My problem is the "namespce" field. 我的问题是“namespce”字段。 There are three tags in each item which has namespace, and I don't know how to specify them. 每个项目中有三个标签,它们都有命名空间,我不知道如何指定它们。 I have tried this: 我试过这个:

EXEC sp_xml_preparedocument @hDoc OUTPUT, @xmlContent,'<item xmlns:dc="http://purl.org/dc/elements/1.1/"/>'

but it just parse the first item and forget the rest! 但它只是解析第一项并忘记其余的! Any idea? 任何的想法?

The namespace needs to be defined as a character type: 命名空间需要定义为字符类型:

EXEC sp_xml_preparedocument @hDoc OUTPUT, @xmlContent,'<item xmlns:dc="http://purl.org/dc/elements/1.1/"/>'

[ xpath_namespaces ] Specifies the namespace declarations that are used in row and column XPath expressions in OPENXML. [xpath_namespaces]指定在OPENXML中的行和列XPath表达式中使用的名称空间声明。 xpath_namespaces is a text parameter: char, nchar, varchar, nvarchar, text, ntext or xml. xpath_namespaces是一个文本参数:char,nchar,varchar,nvarchar,text,ntext或xml。

The default value is . 默认值为。 xpath_namespaces provides the namespace URIs for the prefixes used in the XPath expressions in OPENXML by means of a well-formed XML document. xpath_namespaces通过格式良好的XML文档为OPENXML中的XPath表达式中使用的前缀提供名称空间URI。 xpath_namespaces declares the prefix that must be used to refer to the namespace urn:schemas-microsoft-com:xml-metaprop; xpath_namespaces声明必须用于引用命名空间urn的前缀:schemas-microsoft-com:xml-metaprop; this provides metadata about the parsed XML elements. 这提供了有关已解析的XML元素的元数据。 Although you can redefine the namespace prefix for the metaproperty namespace by using this technique, this namespace is not lost. 虽然您可以使用此技术重新定义元属性命名空间的命名空间前缀,但此命名空间不会丢失。 The prefix mp is still valid for urn:schemas-microsoft-com:xml-metaprop even if xpath_namespaces contains no such declaration. 前缀mp对于urn仍然有效:schemas-microsoft-com:xml-metaprop,即使xpath_namespaces不包含此类声明也是如此。

http://msdn.microsoft.com/en-us/library/ms187367.aspx http://msdn.microsoft.com/en-us/library/ms187367.aspx

The fact that you are only getting one row has nothing to do with the namespace. 您只获得一行这一事实与命名空间无关。 You have some error in your openxml query against @hDoc . 您在针对@hDoc的openxml查询中有一些错误。

There might be reasons for you to still use openxml but until you show the query that is not working for you I will suggest you use the XML data type instead. 可能有理由仍然使用openxml,但在您显示不适合您的查询之前,我建议您使用XML数据类型。

with xmlnamespaces('http://purl.org/dc/elements/1.1/' as dc)
select C.N.value('(title/text())[1]', 'nvarchar(100)') as channel_title,
       I.N.value('(title/text())[1]', 'nvarchar(100)') as item_title,
       I.N.value('(dc:publisher/text())[1]', 'nvarchar(100)') as publisher
from @XML.nodes('/rss/channel') as C(N)
  cross apply C.N.nodes('item') as I(N);

SQL Fiddle SQL小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM