如何在sql server中使用命名空间获取XML节点值?

时间:2017-09-01 07:18:32

标签: sql sql-server xml

我必须得到公司节点元素的值。我已经尝试了所有的 从节点获取数据但没有运气的方法。下面是我的XML。

<?xml version="1.0"?>
    <CompanyInvoice xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <Customer xmlns="http://t.service/CompanyServices/">
        <Company>
          <CompanyId>10001</CompanyId>
          <CoastalId>454564564564564564564564565465454546565555555</CoastalId>
          <CompanyFederalId>345345</CompanyFederalId>
          <CompanyName>Anytime Home</CompanyName>
          <CompanyAddress>Address1</CompanyAddress>
          <CompanyCity>TR</CompanyCity>
          <CompanyState>UT</CompanyState>
          <CompanyPostalCode>11</CompanyPostalCode>
          <CompanyCountry>IT</CompanyCountry>
          <CompanyTelephone>(999) 999-9999</CompanyTelephone>
        </Company>
        <CustomerId>33642</CustomerId>    
      </Customer>
      </CompanyInvoice>

TSQL代码: 我只是尝试了这个,但没有得到任何更新

Declare @DATAXML xml ='<?xml version="1.0"?>
    <CompanyInvoice xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <Customer xmlns="http://t.service/CompanyServices/">
        <Company>
          <CompanyId>10001</CompanyId>
          <CoastalId>454564564564564564564564565465454546565555555</CoastalId>
          <CompanyFederalId>345345</CompanyFederalId>
          <CompanyName>Anytime Home</CompanyName>
          <CompanyAddress>Address1</CompanyAddress>
          <CompanyCity>TR</CompanyCity>
          <CompanyState>UT</CompanyState>
          <CompanyPostalCode>11</CompanyPostalCode>
          <CompanyCountry>IT</CompanyCountry>
          <CompanyTelephone>(999) 999-9999</CompanyTelephone>
        </Company>
        <CustomerId>33642</CustomerId>    
      </Customer>
      </CompanyInvoice>'


;WITH XMLNAMESPACES('http://t.service/CompanyServices/' as x)
Select
a.value('x:CompanyId[1]','nvarchar(50)') as CompanyId, 
a.value('x:CoastalId[1]','nvarchar(500)') as CoastalId, 
a.value('x:CompanyName[1]','nvarchar(500)') as CompanyName
From @DATAXML.nodes('/CompanyInvoice/Customer/Company')as a (a)

2 个答案:

答案 0 :(得分:2)

基本上你有两种选择。 1.正确引入和使用名称空间。注意命名空间范围。 2.使用通配符命名空间(不建议在生产中使用)

Declare @DATAXML xml = N'<?xml version="1.0"?>
    <CompanyInvoice xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <Customer xmlns="http://t.service/CompanyServices/">
        <Company>
          <CompanyId>10001</CompanyId>
          <CoastalId>454564564564564564564564565465454546565555555</CoastalId>
          <CompanyFederalId>345345</CompanyFederalId>
          <CompanyName>Anytime Home</CompanyName>
          <CompanyAddress>Address1</CompanyAddress>
          <CompanyCity>TR</CompanyCity>
          <CompanyState>UT</CompanyState>
          <CompanyPostalCode>11</CompanyPostalCode>
          <CompanyCountry>IT</CompanyCountry>
          <CompanyTelephone>(999) 999-9999</CompanyTelephone>
        </Company>
        <CustomerId>33642</CustomerId>    
      </Customer>
      </CompanyInvoice>';


WITH XMLNAMESPACES('http://t.service/CompanyServices/' as x)
Select
a.value('x:CompanyId[1]','nvarchar(50)') as CompanyId, 
a.value('x:CoastalId[1]','nvarchar(500)') as CoastalId, 
a.value('x:CompanyName[1]','nvarchar(500)') as CompanyName
From @DATAXML.nodes('CompanyInvoice/x:Customer/x:Company')as a (a);

-- 

select t.node.value('*:CompanyId[1]', 'int')
from @DATAXML.nodes('*:CompanyInvoice/*:Customer/*:Company') t(node);

答案 1 :(得分:1)

试试这个以及其他示例

REferThis for more info

Error {rejection: Error, promise: t, zone: r, task: t, stack: <accessor>, …}
message: "Uncaught (in promise): Error: Cannot find module '../pages/oneself/oneself.module'.
Error: Cannot find module '../pages/oneself/oneself.module'
 at file:///android_asset/www/build/main.js:65357:9
    at t.invoke

如果这个剂量起作用的话 Alternate Link