Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TSQL FOR XML PATH Attribute On , Type

Tags:

tsql

xml

Basically I need to output both single value and muti value fields from a database. Most users of the data are not technical and it would be nice if they could read the XML and make sense of it. With help from another post I got almost all the way there.

The problem I have is that the element name cannot have any spaces. Need to include a display name that may have spaces. For example MVtext displayName="Multi Value Text". If the right answer is an inline schema or another approach please let me know. It is not like I can a publish static schema as the application can dynamically add fields. And the fields can change with the type of report. The fields are in a table and I read that table to build the select.

How can I communicate a display name that may have spaces for the elements? SQL 2008 R2.

   SELECT top 4
     [sv].[sID] AS '@sID'
    ,[sv].[sParID] AS '@sParID' 
    ,[sv].[docID] AS 'docID'
    ,[sv].addDate as 'addDate'
    ,(SELECT [value] AS 'value'
       FROM  [docMVtext] as [mv]
       WHERE [mv].[sID] = [sv].[sID]
         AND [mv].[fieldID] = '113'
       ORDER BY [mv].[value]
       FOR XML PATH (''), type
     ) AS "To"  
    ,(SELECT [value] AS 'value'
       FROM  [docMVtext] as [mv]
       WHERE [mv].[sID] = [sv].[sID]
         AND [mv].[fieldID] = '130'
       ORDER BY [mv].[value]
       FOR XML PATH (''), type
     ) AS "MVtest" 
  FROM  [docSVsys] as [sv]
  WHERE [sv].[sID] >= '57' 
  ORDER BY 
      [sv].[sParID], [sv].[sID]
  FOR XML PATH('Document'), root('Documents')

Produces:

<Documents>
  <Document sID="57" sParID="57">
    <docID>3.818919.C41P3UKK00BRICLAY0AR1ET2EBPYSU4SA</docID>
    <addDate>2011-10-28T12:26:00</addDate>
    <To>
      <value>Frank Ermis</value>
      <value>Keith Holst</value>
      <value>Mike Grigsby</value>
    </To>
    <MVtest>
      <value>MV test 01</value>
      <value>MV test 02</value>
      <value>MV test 03</value>
      <value>MV test 04</value>
    </MVtest>
  </Document>
  <Document sID="58" sParID="57">
    <docID>3.818919.C41P3UKK00BRICLAY0AR1ET2EBPYSU4SA.1</docID>
    <addDate>2011-10-28T12:26:00</addDate>
  </Document>
  <Document sID="59" sParID="59">
    <docID>3.818920.KJKP5LYKTNIODOEI4JDOKJ2BXJI5P0BIA</docID>
    <addDate>2011-10-28T12:26:00</addDate>
    <To>
      <value>Vladimir Gorny</value>
    </To>
  </Document>
  <Document sID="60" sParID="59">
    <docID>3.818920.KJKP5LYKTNIODOEI4JDOKJ2BXJI5P0BIA.1</docID>
    <addDate>2011-10-28T12:26:00</addDate>
  </Document>
</Documents>

I tired

  SELECT  [value] AS 'value', 'MV test' as 'dispName'

But I get

 <MVtest>
  <value>MV test 01</value>
  <dispName>MV test</dispName>
  <value>MV test 02</value>
  <dispName>MV test</dispName>
  <value>MV test 03</value>
  <dispName>MV test</dispName>
  <value>MV test 04</value>
  <dispName>MV test</dispName>
</MVtest>

SELECT [value] AS 'value', 'MV test' as '@dispName' Throws a execution error Row tag omission (empty row tag name) cannot be used with attribute-centric FOR XML serialization

Desired output:

  <Documents>
  <Document sID="57" sParID="57">
    <docID>3.818919.C41P3UKK00BRICLAY0AR1ET2EBPYSU4SA</docID>
    <addDate>2011-10-28T12:26:00</addDate>
    <To>
      <value>Frank Ermis</value>
      <value>Keith Holst</value>
      <value>Mike Grigsby</value>
    </To>
    <MVtest dispName="Multi Value Text">
      <value>MV test 01</value>
      <value>MV test 02</value>
      <value>MV test 03</value>
      <value>MV test 04</value>
    </MVtest>
  </Document>
  </Documents>

Solution:

SELECT top 4
     [sv].[sID] AS '@sID'
    ,[sv].[sParID] AS '@sParID'
    ,'SV' as 'docID/@SVMV' 
    ,[sv].[docID] AS 'docID'
    ,'SV' as 'addDate/@SVMV'
    ,[sv].addDate as 'addDate'
    ,'Email To' as 'To/@DisplayName'
    ,'MV' as 'To/@SVMV'
    ,(SELECT [value] AS 'value'
       FROM  [docMVtext] as [mv]
       WHERE [mv].[sID] = [sv].[sID]
         AND [mv].[fieldID] = '113'
       ORDER BY [mv].[value]
       FOR XML PATH (''), type
     ) AS "To"
    ,'Multi Value Text Sample' as 'MVtext130/@DisplayName'
    ,'MV' as 'MVtext130/@SVMV'      
    ,(SELECT  [value] AS 'value' 
       FROM  [docMVtext] as [mv]
       WHERE [mv].[sID] = [sv].[sID]
         AND [mv].[fieldID] = '130'
       ORDER BY [mv].[value]
       FOR XML PATH (''), type
     ) AS "MVtext130" 
  FROM  [docSVsys] as [sv]
  WHERE [sv].[sID] >= '57' 
  ORDER BY [sv].[sParID], [sv].[sID]
  FOR XML PATH('Document'), root('Documents') 

Solution output:

<Documents>
  <Document sID="57" sParID="57">
    <docID SVMV="SV">3.818919.C41P3UKK00BRICLAY0AR1ET2EBPYSU4SA</docID>
    <addDate SVMV="SV">2011-10-28T12:26:00</addDate>
    <To DisplayName="Email To" SVMV="MV">
      <value>Frank Ermis</value>
      <value>Keith Holst</value>
      <value>Mike Grigsby</value>
    </To>
    <MVtext130 DisplayName="Multi Value Text Sample" SVMV="MV">
      <value>MV test 01</value>
      <value>MV test 02</value>
      <value>MV test 03</value>
      <value>MV test 04</value>
    </MVtext130>
  </Document>
  <Document sID="58" sParID="57">
    <docID SVMV="SV">3.818919.C41P3UKK00BRICLAY0AR1ET2EBPYSU4SA.1</docID>
    <addDate SVMV="SV">2011-10-28T12:26:00</addDate>
    <To DisplayName="Email To" SVMV="MV" />
    <MVtext130 DisplayName="Multi Value Text Sample" SVMV="MV" />
  </Document>
  <Document sID="59" sParID="59">
    <docID SVMV="SV">3.818920.KJKP5LYKTNIODOEI4JDOKJ2BXJI5P0BIA</docID>
    <addDate SVMV="SV">2011-10-28T12:26:00</addDate>
    <To DisplayName="Email To" SVMV="MV">
      <value>Vladimir Gorny</value>
    </To>
    <MVtext130 DisplayName="Multi Value Text Sample" SVMV="MV" />
  </Document>
  <Document sID="60" sParID="59">
    <docID SVMV="SV">3.818920.KJKP5LYKTNIODOEI4JDOKJ2BXJI5P0BIA.1</docID>
    <addDate SVMV="SV">2011-10-28T12:26:00</addDate>
    <To DisplayName="Email To" SVMV="MV" />
    <MVtext130 DisplayName="Multi Value Text Sample" SVMV="MV" />
  </Document>
</Documents>

This is a great solution to my question as asked. The one symptom of adding the attribute is that I get the element even if there are zero value rows. Ideally it will not list the element if there are no values.

Tried a Case statement but even if I set the value to '' it displays the element.

    ,[MVtext130/@DisplayName] = 
     Case (select COUNT(*) FROM [docMVtext] 
                  WHERE [docMVtext].[sID] = [sv].[sID]
                  AND [docMVtext].[fieldID] = '130')
       when '0' then ''
       else 'Multi Value Text Sample'
     end 

Updated solution to not list element with no values:

    SELECT top 4
     [sv].[sID] AS '@sID'
    ,[sv].[sParID] AS '@sParID'
    ,'SV' as 'docID/@SVMV' 
    ,[sv].[docID] AS 'docID'
    ,'SV' as 'addDate/@SVMV'
    ,[sv].addDate as 'addDate'
    ,(select top(1) 'Email To'
      from [docMVtext] as C
      where C.[sID] = [sv].[sID] and c.fieldID = '113' ) as 'To/@DisplayName'
    ,(select top(1) 'MV'
      from [docMVtext] as C
      where C.[sID] = [sv].[sID] and c.fieldID = '113' ) as 'To/@SVMV'
    --,'Email To' as 'To/@DisplayName'
    --,'MV' as 'To/@SVMV'
    ,(SELECT [value] AS 'value'
       FROM  [docMVtext] as [mv]
       WHERE [mv].[sID] = [sv].[sID]
         AND [mv].[fieldID] = '113'
       ORDER BY [mv].[value]
       FOR XML PATH (''), type
     ) AS "To"
    ,(select top(1) 'Multi Value Text Sample'
      from [docMVtext] as C
      where C.[sID] = [sv].[sID] and c.fieldID = '130' ) as 'MVtext130/@DisplayName'
    ,(select top(1) 'MV'
      from [docMVtext] as C
      where C.[sID] = [sv].[sID] and c.fieldID = '130' ) as 'MVtext130/@SVMV'
    --,'Multi Value Text Sample' as 'MVtext130/@DisplayName'
    --,'MV' as 'MVtext130/@SVMV'    
    ,(SELECT  [value] AS 'value' 
       FROM  [docMVtext] as [mv]
       WHERE [mv].[sID] = [sv].[sID]
         AND [mv].[fieldID] = '130'
       ORDER BY [mv].[value]
       FOR XML PATH (''), type
     ) AS "MVtext130" 
  FROM  [docSVsys] as [sv]
  WHERE [sv].[sID] >= '57' 
  ORDER BY [sv].[sParID], [sv].[sID]
  FOR XML PATH('Document'), root('Documents')
like image 329
paparazzo Avatar asked Dec 13 '11 16:12

paparazzo


1 Answers

Not sure I understand what you want and it is hard to do something with your code without the tables you have so I created a sample that I believe is doing what you already have.

Setup table and data:

declare @Main table
(
  MainID int identity,
  Value int
)

declare @Child table
(
  ChildID int identity,
  MainID int,
  Value int
)

insert into @Main values (10),(20),(30)
insert into @Child values (1,100),(2,200),(2,210)

The query that does approximately the same as yours do already.

select M.MainID as '@MainID',
       M.Value as 'MainValue',
       (select C.Value as ChildValue
        from @Child as C
        where C.MainID = M.MainID
        for xml path(''), type) as Child
from @Main as M
for xml path('Document'), root('Documents')

Result:

<Documents>
  <Document MainID="1">
    <MainValue>10</MainValue>
    <Child>
      <ChildValue>100</ChildValue>
    </Child>
  </Document>
  <Document MainID="2">
    <MainValue>20</MainValue>
    <Child>
      <ChildValue>200</ChildValue>
      <ChildValue>210</ChildValue>
    </Child>
  </Document>
  <Document MainID="3">
    <MainValue>30</MainValue>
  </Document>
</Documents>

I believe that your expected output would be something like this with a display name attribute on the child nodes.

<Documents>
  <Document MainID="1">
    <MainValue>10</MainValue>
    <Child DisplayName="Child Display Name">
      <ChildValue>100</ChildValue>
    </Child>
  </Document>
  <Document MainID="2">
    <MainValue>20</MainValue>
    <Child DisplayName="Child Display Name">
      <ChildValue>200</ChildValue>
      <ChildValue>210</ChildValue>
    </Child>
  </Document>
  <Document MainID="3">
    <MainValue>30</MainValue>
  </Document>
</Documents>

To get that you use this query instead:

select M.MainID as '@MainID',
       M.Value as 'MainValue',
       (select top(1) 'Child Display Name'
        from @Child as C
        where C.MainID = M.MainID) as 'Child/@DisplayName',
       (select C.Value as ChildValue
        from @Child as C
        where C.MainID = M.MainID
        for xml path(''), type) as Child
from @Main as M
for xml path('Document'), root('Documents')
like image 126
Mikael Eriksson Avatar answered Nov 15 '22 08:11

Mikael Eriksson