简体   繁体   English

SQL Server:XQUERY名称空间解析

[英]SQL Server : XQUERY namespace parsing

I have the following XML: 我有以下XML:

    declare @xml    xml = '
    <DiscoverResponse xmlns="urn:schemas-microsoft-com:xml-analysis">
      <return>
        <root xmlns="urn:schemas-microsoft-com:xml-analysis:rowset">
          <row>
            <xars:METADATA xmlns="http://schemas.microsoft.com/analysisservices/2003/engine">
              <Server>
                <Name>myName</Name>
              </Server>
            </xars:METADATA>
          </row>
        </root>
       </return>
    </DiscoverResponse>'

And I have been able to parse up to the <row> tag with the following query 而且我已经能够使用以下查询解析到<row>标记

    ; WITH XMLNAMESPACES(
        'urn:schemas-microsoft-com:xml-analysis'                        AS ns1,
        'urn:schemas-microsoft-com:xml-analysis:rowset'                 AS ns2
    )
    select 
        t.x.value('ns2:Name[1]','varchar(100)') AS Name
    from @xml.nodes('//ns1:DiscoverResponse/ns1:return/ns2:root/ns2:row') t(x)

I am stuck trying to figure out how to query past the <xars:METADATA> tag. 我一直试图找出如何通过<xars:METADATA>标记进行查询。 That is, I have not been able successfully construct a namespace declaration and path specification to get to the <Server> node (I am trying to extract the Name of the Server). 也就是说,我无法成功构造名称空间声明和路径规范以获取<Server>节点(我正在尝试提取服务器的名称)。 I am running afoul of the extra colon it seems. 我似乎在冒多余的冒号。

Thanks in advance. 提前致谢。

1) That XML isn't valid. 1)XML无效。

2) If you want to avoid XML NAMESPACES then you could use following syntax for every XML element: *:XmlElement . 2)如果要避免使用 XML命名空间,则可以对每个XML元素使用以下语法: *:XmlElement

Example: 例:

declare @xml    xml = N'<DiscoverResponse xmlns="urn:schemas-microsoft-com:xml-analysis">
      <return>
        <root xmlns="urn:schemas-microsoft-com:xml-analysis:rowset">
          <row>
            <xars:METADATA xmlns="http://schemas.microsoft.com/analysisservices/2003/engine" xmlns:xars="aaa">
              <Server>
                <Name>myName</Name>
              </Server>
            </xars:METADATA>
          </row>
        </root>
       </return>
    </DiscoverResponse>'

SELECT  x.y.value('(*:Server/*:Name/text())[1]', 'NVARCHAR(260)') AS Server_Name
FROM    @xml.nodes('*:DiscoverResponse/*:return/*:root/*:row/*:METADATA') x(y)

Results: 结果:

Server_Name
-----------
myName

You have a couple of things going wrong: 您有几处出问题的地方:

  1. The xars namespace is not declared in the xml. xars名称空间未在xml中声明。 I believe the namespace for xars is "urn:schemas-microsoft-com:xml-analysis:rowset" which you actually have as a default namespace (form the root) but not explicitly mapped to the xars prefix. 我相信xars的名称空间是“ urn:schemas-microsoft-com:xml-analysis:rowset”,您实际上将其作为默认名称空间(形成根),但未明确映射到xars前缀。

  2. The xquery is missing the reference to namespace " http://schemas.microsoft.com/analysisservices/2003/engine " which in the xml is set as the default namespace from and including METADATA. xquery缺少对名称空间“ http://schemas.microsoft.com/analysisservices/2003/engine ”的引用,该名称空间在xml中被设置为METADATA(包括METADATA)中的默认名称空间。

So first, I've altered your sample xml to include a namespace for xars: I expect your actual xml has this declared elsewhere, so you problably dont need to make this change. 因此,首先,我更改了示例xml,以包含xars的命名空间:我希望您的实际xml在其他地方声明了此名称,因此您可能不需要进行此更改。

declare @xml    xml = N'<DiscoverResponse xmlns="urn:schemas-microsoft-com:xml-analysis">
  <return>
    <root xmlns="urn:schemas-microsoft-com:xml-analysis:rowset">
      <row>
        <xars:METADATA xmlns="http://schemas.microsoft.com/analysisservices/2003/engine" xmlns:xars="urn:schemas-microsoft-com:xml-analysis:rowset">
          <Server>
            <Name>myName</Name>
          </Server>
        </xars:METADATA>
      </row>
    </root>
   </return>
</DiscoverResponse>'

Second, I've added the ' http://schemas.microsoft.com/analysisservices/2003/engine ' (as ns3) to your query and change the query to reference it. 其次,我在您的查询中添加了“ http://schemas.microsoft.com/analysisservices/2003/engine ”(作为ns3),并更改了查询以引用它。

; WITH XMLNAMESPACES(
    'urn:schemas-microsoft-com:xml-analysis'                        AS ns1,
    'urn:schemas-microsoft-com:xml-analysis:rowset'                 AS ns2,
    'http://schemas.microsoft.com/analysisservices/2003/engine'     AS ns3
)
select 
    t.x.value('ns3:Name[1]','varchar(100)') AS Name
from @xml.nodes('//ns1:DiscoverResponse/ns1:return/ns2:root/ns2:row/ns2:METADATA/ns3:Server') t(x)

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

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