5

I have to get values of company node elements.I have tried all the method to fetch data from the node but no luck. Below is my 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 Code: I have simply tried with this, but not getting any updates

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)
5
  • stackoverflow.com/questions/19165213/… Commented Sep 1, 2017 at 7:25
  • 1
    You need to introduce and use namespaces in your query. See learn.microsoft.com/en-us/sql/relational-databases/xml/… Commented Sep 1, 2017 at 7:28
  • @Serg, i introduce namespace, but no luck Commented Sep 1, 2017 at 7:46
  • 1
    But you haven't mentioned x namespace in the nodes ...nodes('CompanyInvoice/x:Customer/x:Company') ... Commented Sep 1, 2017 at 7:50
  • @Serg, Oh!!! silly mistake. Thanks to notify me Commented Sep 1, 2017 at 9:02

2 Answers 2

5

Basically you have two options. 1.Introduce and use namespaces properly. Pay attention to namespaces scope. 2.Use a wildcard namespace (not recommended in production)

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for this, But why we don't use wild card in production
Generally xml comes from another loosely coupled system and we don't want any bad stuff entering our DB. As namespaces are an essential part of the data exchange contract, we'd better strictly define them the way contract prescribes.
1

Try This this just and other example

REferThis for more info

  DECLARE @foo XML

SELECT @foo = N'
<harrys>
    <harry>
        <fish>0.015000000000</fish>
        <bicycle>2008-10-31T00:00:00+01:00</bicycle>
        <foo>ü</foo>
    </harry>
    <harry>
        <fish>0.025000000000</fish>
        <bicycle>2008-08-31T00:00:00+01:00</bicycle>
        <foo>ä</foo>
    </harry>
</harrys>
'

SELECT
    CAST(CAST(y.item.query('data(fish)') AS varchar(30)) AS float),
    CAST(LEFT(CAST(y.item.query('data(bicycle)') AS char(25)), 10) AS smalldatetime),
    CAST(y.item.query('data(foo)') AS varchar(25))
FROM
    @foo.nodes('/*') x(item)
    CROSS APPLY
    x.item.nodes('./*') AS y(item)

SELECT
    CAST(CAST(x.item.query('data(fish)') AS varchar(30)) AS float),
    CAST(LEFT(CAST(x.item.query('data(bicycle)') AS char(25)), 10) AS smalldatetime),
    CAST(x.item.query('data(foo)') AS varchar(25))
FROM
    @foo.nodes('harrys/harry') x(item)

SELECT
    CAST(CAST(y.item.query('data(fish)') AS varchar(30)) AS float),
    CAST(LEFT(CAST(y.item.query('data(bicycle)') AS char(25)), 10) AS smalldatetime),
    CAST(y.item.query('data(foo)') AS varchar(25))
FROM
    @foo.nodes('/harrys') x(item)
    CROSS APPLY
    x.item.nodes('./harry') AS y(item)

If this dosent work then Alternate Link

1 Comment

Thanks for response but this not help me to achieve my result. I have added my tsql code. Try to get values on those

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.