0

I have a XML column in a SQL Server database. I want to get results from that column. Here is what the XML column looks like:

<ArrayOfTarget xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/TriTech.InformRMS.Domain.Core.ComplexTypes">
  <Target>
    <AgencyId i:nil="true" />
    <AgencyName i:nil="true" />
    <Id i:nil="true" />
    <Name>Quick Search</Name>
    <Type>Search</Type>
  </Target>
  <Target>
    <AgencyId i:nil="true" />
    <AgencyName i:nil="true" />
    <Id i:nil="true" />
    <Name>Quick Search = wbinc2000125</Name>
    <Type>Quick Search</Type>
  </Target>
  <Target>
    <AgencyId i:nil="true" />
    <AgencyName i:nil="true" />
    <Id i:nil="true" />
    <Name>Results (0)</Name>
    <Type>Result</Type>
  </Target>
</ArrayOfTarget>

Here are some things I have tried but no results:

select 
    [XML].value ('(ArrayofTarget/Target/Name/node())[1]', 'nvarchar(max)') as Results
from 
    [DB].[dbo].[Table]

Another example:

;WITH XMLNAMESPACES (N'http://www.w3.org/2001/XMLSchema-instance' as X)
SELECT
    [XML].value('(/X:ArrayOfTarget/X:Target/X:Name[1]', 'nvarchar(max)') as Name
FROM [DB].[dbo].[Table]
1
  • 1
    You're on the right track with WITH XMLNAMESPACES, but the relevant namespace is the default namespace mentioned in xmlns (http://schemas.datacontract.org/2004/07/TriTech.InformRMS.Domain.Core.ComplexTypes). You can use DEFAULT for that. Commented Apr 20, 2021 at 15:00

1 Answer 1

1

Here is a working example.

It shows how to handle properly a default namespace as well as how to use two XQuery methods: .nodes() and .value()

SQL

-- DDL and sample data population, start
DECLARE @tbl TABLE (ID INT IDENTITY PRIMARY KEY, xmldata XML);
INSERT INTO @tbl (xmldata) VALUES
(N'<ArrayOfTarget xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
               xmlns="http://schemas.datacontract.org/2004/07/TriTech.InformRMS.Domain.Core.ComplexTypes">
    <Target>
        <AgencyId i:nil="true"/>
        <AgencyName i:nil="true"/>
        <Id i:nil="true"/>
        <Name>Quick Search</Name>
        <Type>Search</Type>
    </Target>
    <Target>
        <AgencyId i:nil="true"/>
        <AgencyName i:nil="true"/>
        <Id i:nil="true"/>
        <Name>Quick Search = wbinc2000125</Name>
        <Type>Quick Search</Type>
    </Target>
    <Target>
        <AgencyId i:nil="true"/>
        <AgencyName i:nil="true"/>
        <Id i:nil="true"/>
        <Name>Results (0)</Name>
        <Type>Result</Type>
    </Target>
</ArrayOfTarget>');
-- DDL and sample data population, end

;WITH XMLNAMESPACES (DEFAULT 'http://schemas.datacontract.org/2004/07/TriTech.InformRMS.Domain.Core.ComplexTypes')
SELECT c.value('(Name/text())[1]', 'VARCHAR(30)') AS [Name]
    , c.value('(Type/text())[1]', 'VARCHAR(30)') AS [Type]
FROM @tbl CROSS APPLY xmldata.nodes('/ArrayOfTarget/Target') AS t(c);

Output

+-----------------------------+--------------+
|            Name             |     Type     |
+-----------------------------+--------------+
| Quick Search                | Search       |
| Quick Search = wbinc2000125 | Quick Search |
| Results (0)                 | Result       |
+-----------------------------+--------------+
Sign up to request clarification or add additional context in comments.

3 Comments

I have many different XML results to pull from that one column. Would this example work with if the XML structure is the same in each cell but has different information?
@ITNoob, it should work. The dependency is on the XPath expressions only. As long as they are the same it will work.
@ITNoob, if the proposed solution is working for you, please don't forget to mark it as Answer.

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.