XML Query显示空白结果

时间:2017-11-28 14:41:15

标签: sql sql-server xml

您好我在SSMS 2012中查询XML数据需要一些帮助

我已阅读各种stackoverflow问题和答案,并遵循它们。

我的查询:

DECLARE @x xml
SELECT @x=R

FROM OPENROWSET (BULK 'C:\Users\Administrator\Desktop\results.xml',SINGLE_BLOB) AS Results(R)
DECLARE @hdoc int

EXEC sp_xml_preparedocument @hdoc OUTPUT , @x , '<root xmlns="http://schemas.serviceml.org/smlif/2007/02" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:sfc="http://schemas.microsoft.com/sqlserver/sfc/serialization/2007/08" xmlns:sml="http://schemas.serviceml.org/sml/2007/02" xmlns:DMF="http://schemas.microsoft.com/sqlserver/DMF/2007/08" />'
SELECT * FROM OPENXML (@hdoc,'PolicyEvaluationResults/model/xs:bufferSchema/difinitions/document/data/xs:schema/DMF:bufferData/instances/document/data/DMF:EvaluationHistory',1)

WITH(PolicyName varchar(100))
EXEC sp_xml_removedocument @hdoc

执行后显示空结果

我正在尝试从以下XML文档代码段中获取信息:

<PolicyEvaluationResults>
  <model xmlns="http://schemas.serviceml.org/smlif/2007/02">
    <identity>
      <name>urn:uuid:96fe1236-abf6-4a57-b54d-e9baab394fd1</name>
      <baseURI>http://documentcollection/</baseURI>
    </identity>
    <xs:bufferSchema xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <definitions xmlns:sfc="http://schemas.microsoft.com/sqlserver/sfc/serialization/2007/08">
         <document>
           <docinfo>
            <aliases>
              <alias>/system/schema/DMF</alias>
            </aliases>
            <sfc:version DomainVersion="3" />
          </docinfo>
          <data>
            <xs:schema xmlns:sfc="http://schemas.microsoft.com/sqlserver/sfc/serialization/2007/08" xmlns:sml="http://schemas.serviceml.org/sml/2007/02" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://schemas.microsoft.com/sqlserver/DMF/2007/08" elementFormDefault="qualified">
              <xs:element name="EvaluationHistory">
                <xs:complexType>
                  <xs:sequence>
                    <xs:any namespace="http://schemas.microsoft.com/sqlserver/DMF/2007/08" processContents="skip" minOccurs="0" maxOccurs="unbounded" />
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
              <xs:element name="ConnectionEvaluationHistory">
                <xs:complexType>
                  <xs:sequence>
                    <xs:any namespace="http://schemas.microsoft.com/sqlserver/DMF/2007/08" processContents="skip" minOccurs="0" maxOccurs="unbounded" />
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
               <xs:element name="EvaluationDetail">
                <xs:complexType>
              <xs:sequence>
                <xs:any namespace="http://schemas.microsoft.com/sqlserver/DMF/2007/08" processContents="skip" minOccurs="0" maxOccurs="unbounded" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
          <DMF:bufferData xmlns:DMF="http://schemas.microsoft.com/sqlserver/DMF/2007/08">
            <instances xmlns:sfc="http://schemas.microsoft.com/sqlserver/sfc/serialization/2007/08">
              <document>
                <docinfo>
                  <aliases>
                    <alias>/PolicyStore/Policy/Must Change Enabled/EvaluationHistory/1</alias>
                  </aliases>
                  <sfc:version DomainVersion="3" />
                </docinfo>
                <data>
                  <DMF:EvaluationHistory xmlns:DMF="http://schemas.microsoft.com/sqlserver/DMF/2007/08" xmlns:sfc="http://schemas.microsoft.com/sqlserver/sfc/serialization/2007/08" xmlns:sml="http://schemas.serviceml.org/sml/2007/02" xmlns:xs="http://www.w3.org/2001/XMLSchema">
                    <DMF:Parent>
                      <sfc:Reference sml:ref="true">
                        <sml:Uri>/PolicyStore/Policy/Must Change Enabled</sml:Uri>
                      </sfc:Reference>
                    </DMF:Parent>
                    <DMF:ConnectionEvaluationHistories>
                      <sfc:Collection>
                        <sfc:Reference sml:ref="true">
                          <sml:Uri>/PolicyStore/Policy/Must Change Enabled/EvaluationHistory/1/ConnectionEvaluationHistory/1</sml:Uri>
                        </sfc:Reference>
                      </sfc:Collection>
                    </DMF:ConnectionEvaluationHistories>
                    <DMF:PolicyName type="string">Must Change Enabled</DMF:PolicyName>
                    <DMF:StartDate type="dateTime">2017-11-28T14:09:50.1639672+02:00</DMF:StartDate>
                    <DMF:EndDate type="dateTime">2017-11-28T14:09:50.4139672+02:00</DMF:EndDate>
                    <DMF:Exception type="string" />
                    <DMF:ID type="long">1</DMF:ID>
                    <DMF:Result type="boolean">false</DMF:Result>
                  </DMF:EvaluationHistory>

我尝试过不同的方法从XML文档中获取PolicyName,StartDate,ID和Results的结果

2 个答案:

答案 0 :(得分:0)

我相信您正在寻找的XPath是这样的:

EXEC sp_xml_preparedocument @hdoc OUTPUT , @xml , 
'<x:model xmlns:x="http://schemas.serviceml.org/smlif/2007/02"
          xmlns:xs="http://www.w3.org/2001/XMLSchema"
          xmlns:sfc="http://schemas.microsoft.com/sqlserver/sfc/serialization/2007/08" 
          xmlns:sml="http://schemas.serviceml.org/sml/2007/02" 
          xmlns:DMF="http://schemas.microsoft.com/sqlserver/DMF/2007/08" />'

SELECT * 
FROM OPENXML (@hdoc,'/PolicyEvaluationResults/x:model/xs:bufferSchema/x:definitions/
                     x:document/x:data/xs:schema/DMF:bufferData/x:instances/
                     x:document/x:data/DMF:EvaluationHistory',1)
WITH
(
  PolicyName varchar(100) 'DMF:PolicyName'
)

With a SqlFiddle here

和笔记:

  • 我建议你也使用默认的xmlns别名(我已使用x),以便于追溯。
  • 我切换到OpenXml以使用SqlFiddle。
  • 包含元素definitions
  • 的错字
  • model本身位于名称空间xmlns="http://schemas.serviceml.org/smlif/2007/02"
  • Everthing else与'model'在同一名称空间中(我将命名空间别名为'x')
  • 最终元素数据也在命名空间中。您可以在类型定义后指定此项,例如DMF:PolicyName

但是,那就是说,你应该转而使用XQuery来查询Sql Server中的Xml数据

答案 1 :(得分:0)

首先:function connexionOK() { $cookieName = "CookieCo"; $cookieValue = "true"; $isSent = setcookie($cookieName, $cookieValue, time() + 3600, "/"); if($isSent) { echo '<script type="text/javascript">'; echo "alert('Cookie envoyé');"; echo "</script>"; } else { echo '<script type="text/javascript">'; echo "alert('Cookie failed');"; echo "</script>"; }} 与相应的SP准备和删除文档已经过时,不应再使用了。而是使用适当的methods the XML data type provides

试试这样:

SELECT
    *
    ,(SELECT COALESCE(SUM(streamers),0) FROM Table t2 WHERE t2.end_time > t1.start_time AND t2.start_time <= t1.start_time) as total_streamers
FROM
    Table t1

一些解释:首先我声明所需的唯一命名空间:&#34; DMF&#34;。在FROM OPENXML方法WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/DMF/2007/08' AS DMF) SELECT @x.value(N'(//DMF:PolicyName/text())[1]',N'nvarchar(max)') AS PolicyName ,@x.value(N'(//DMF:StartDate/text())[1]',N'datetime') AS StartDate ,@x.value(N'(//DMF:ID/text())[1]',N'int') AS ID ,@x.value(N'(//DMF:Result/text())[1]',N'bit') AS Result 中,您说明了XQuery.value()。双斜杠XPath触发深度搜索,意思是:找到名为&#34; DMF:PolicyName&#34; 的第一个元素,而不用担心所有混乱。

我必须承认,你的XML非常复杂......在我理解结构之前,我必须通过在不同的地方添加结束标记来修复它。

一般规则是:尽可能具体。在你的情况下,深度嵌套和各种命名空间使得很难干净,只是为了得到一些简单的值......