Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to query XML while ignoring namespaces, because the result set has multiple namespaces. I've gotten to the DataSets node, but I can't figure out how to get out the multiple DataSourceName/CommandType/CommandText. Ideally I want:

DataSetName   DataSourceName   CommandType      CommandText
SQLDS         SQLDS            StoredProcedure  ReportProc_aaaaa
SQLDS         SQLDS            StoredProcedure  ReportProc_lalala

Help greatly appreciated.

DECLARE @xmltable TABLE (myxml XML)
INSERT INTO @xmltable 
SELECT   
'<Report xmlns="http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition" xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner">
  <DataSources>
    <DataSource Name="SQLDS">
      <rd:DataSourceID>32e83b35-434d-4808-b685-ada14accd0e7</rd:DataSourceID>
      <DataSourceReference>SQLDS</DataSourceReference>
    </DataSource>
  </DataSources>
  <DataSets>
    <DataSet Name="SQLDS">
      <Query>
        <DataSourceName>SQLDS</DataSourceName>
        <CommandType>StoredProcedure</CommandType>
        <CommandText>ReportProc_ServerPerformanceGroup</CommandText>
      </Query>
    </DataSet>
    <DataSet Name="GroupDetails">
      <Query>
        <DataSourceName>SQLDS</DataSourceName>
        <CommandType>StoredProcedure</CommandType>
        <CommandText>ReportProc_lalala</CommandText>
      </Query>
    </DataSet>
  </DataSets>
</Report>'

SELECT myxml.value('(/*:Report/*:DataSets)[1]','varchar(100)') FROM @xmltable
share|improve this question
1  
Is your DBMS Oracle? –  Sean B. Durkin Jun 26 '13 at 23:48
    
I have edited your title. Please see, "Should questions include “tags” in their titles?", where the consensus is "no, they should not". –  John Saunders Jun 27 '13 at 0:00
    
@JohnSaunders okay. Thanks. –  mbourgon Jun 27 '13 at 4:01
    
@SeanB.Durkin Unfortunately, SQL Server. Let me (re?)add the tag for that. –  mbourgon Jun 27 '13 at 4:01
    
Your xml document does not match your expected output. For example ReportProc_aaaaa is not be be found anywhere in your xml. –  Sean B. Durkin Jun 27 '13 at 4:27

2 Answers 2

up vote 1 down vote accepted

Use nodes() Method (xml Data Type) to shred yoru XML to rows and use value() Method (xml Data Type) to get specific values from the XML.

select T1.N.value('@Name', 'nvarchar(128)') as DataSetName,
       T2.N.value('(*:DataSourceName/text())[1]', 'nvarchar(128)') as DataSourceName,
       T2.N.value('(*:CommandType/text())[1]', 'nvarchar(128)') as CommandType,
       T2.N.value('(*:CommandText/text())[1]', 'nvarchar(max)') as CommandText
from @xmltable as T
  cross apply T.myxml.nodes('/*:Report/*:DataSets/*:DataSet') as T1(N)
  cross apply T1.N.nodes('*:Query') as T2(N)

SQL Fiddle

share|improve this answer
    
works like a charm! I figured I would need the cross apply, but couldn't figure out the colon syntax stuff. Much appreciated! –  mbourgon Jun 27 '13 at 13:07

How about (untested) ....

select
    T.c.value(N'DataSourceName', N'nvarchar(100)') as DataSourceName,
    T.c.value(N'CommandType',N'nvarchar(100)') as CommandType,
    T.c.value(N'CommandText', N'nvarchar(100)') as CommandText
  from
    @myxml.nodes(N'/Report/DataSets/DataSet/Query') T(c)
share|improve this answer
    
FWIW, that didn't work - needed singletons. But IIRC I tried that way, and it didn't work because of the namespaces. –  mbourgon Jun 27 '13 at 14:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.