简体   繁体   English

如何在SQL Server的XQuery中获取特定的XML名称空间

[英]How to get specific XML namespace in XQuery in SQL Server

I have a XML that I need one specific namespace according to node like temprature with hls i need namespace of that " http://www.schema.hls.com/extension " I have tried with these 我有一个XML,我根据就像HLS我需要“的命名空间温度图节点需要一个特定的命名空间http://www.schema.hls.com/extension ”我曾尝试与这些

DECLARE @EventXML AS XML

SET @EventXML='<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns:test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ns="urn:global:test:xsd:1" 
xmlns:hls="http://schema.hls.com/extension" creationDate="2007-01-25T00:00:00Z"
schemaVersion="1.0">
<TestBody>
<TestList>
<TestEvent>
    <hls:temperature>20</hls:temperature>
  </TestEvent>
</TestList>
</TestBody>
</ns:test>'

SELECT 
OE.value('@ns','varchar(50)') + '#' + OE.value('fn:local-name(.)[1]','varchar(50)'),
OE.value('@id','varchar(50)'),
CONVERT(VARCHAR(4000),CASE WHEN OE.exist('./*') =1 THEN OE.query('./*')  ELSE    
OE.value('./text()[1]','varchar(100)') END)
FROM @EventXML.nodes('//TestEvent/*') TestEvent(OE)
WHERE OE.value('fn:local-name(.)[1]','varchar(50)') IN --(@tag) 
(SELECT  Split.a.value('.', 'VARCHAR(100)') AS extag 
FROM  (SELECT   CONVERT(XML,'<M>' + REPLACE(ISNULL('temperature','0'), ',', '</M><M>') + '</M>') AS String 
 ) AS A CROSS APPLY String.nodes ('/M') AS Split(a))  

I am using these in SQL query window but getting only third column value 20 not get namespace by @ns 我在SQL查询窗口中使用这些,但仅获取第三列值20而不通过@ns获取名称空间

Please suggest how to get the namespace 请建议如何获取名称空间

OE.value('@ns','varchar(50)') 

by these. 通过这些。

thanks in advanced. 提前致谢。

Your code and XML somehow just don't quite match up - and the query is really quite confusing.... 您的代码和XML某种程度上根本不匹配-而且查询确实很混乱。

If you want to fetch the data, you must respect the XML namespaces in play. 如果要获取数据,则必须遵守使用中的XML名称空间。 You need to declare them with a WITH XMLNAMESPACES() construct, and you need to use them in your XPath. 您需要WITH XMLNAMESPACES()构造声明它们,并且需要在XPath中使用它们。

But also: the node you're selecting ( <hls:temperature> ) doesn't really have any id and ns attributes..... so of course you're not getting any values! 而且还:您选择的节点( <hls:temperature> )实际上没有任何idns属性.....因此,您当然没有任何值!

I tried to use a trimmed down version and I added the two attributes - just to show how to use the XML namespaces stuff in your code. 我尝试使用精简版本,并添加了两个属性-只是为了展示如何在代码中使用XML名称空间。

Here it comes: 它来了:

DECLARE @EventXML AS XML

SET @EventXML = 
   '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <ns:test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:ns="urn:global:test:xsd:1" 
             xmlns:hls="http://schema.hls.com/extension" 
             creationDate="2007-01-25T00:00:00Z" schemaVersion="1.0">
       <TestBody>
          <TestList>
             <TestEvent>
                <hls:temperature ns="test" id="42">20</hls:temperature>
             </TestEvent>
         </TestList>
      </TestBody>
   </ns:test>'

-- define your XML namespaces that are in play. 
-- You *MUST* match the namespace definition, but the *prefixes* that you define
-- can be something else entirely than in the XML document!
-- Of course, inside your XPath, you *MUST* use the defined prefixes!
;WITH XMLNAMESPACES('urn:global:test:xsd:1' AS x1, 
                    'http://schema.hls.com/extension' AS x2)
SELECT 
    OE.value('@ns', 'varchar(50)'),
    OE.value('@id', 'varchar(50)')
FROM 
    @EventXML.nodes('/x1:test/TestBody/TestList/TestEvent/x2:*') TestEvent(OE)

This code - using the XML namespaces defined and used in your XML - produces this output: 这段代码-使用XML中定义和使用的XML名称空间-产生以下输出:

(No column name)  (No column name)
test                   42

So this shows how you can access the attributes - if they are present! 因此,这说明了如何访问属性- 如果存在的话! - on your XML nodes, even with the presence of XML namespaces. -在XML节点上,即使存在XML名称空间也是如此。

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

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