生成相同名称节点时出现SQL Server FOR XML显式错误

时间:2015-12-04 14:56:58

标签: sql sql-server tsql for-xml-explicit

我正在尝试使用for XML Explicit生成以下XML结构

<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
<Worksheet ss:Name="1">
    <Table>
        <Row>
            <Cell>
                <Data ss:Type="String">231548</Data>
            </Cell>
            <Cell>
                <Data ss:Type="String">2014-11-03T20:03:30</Data>
            </Cell>
        </Row>
    </Table>
</Worksheet>

我能够生成

 <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
  <Worksheet ss:Name="1">
    <Table>
      <Row>
        <Cell>
          <Data ss:Type="String">231548</Data>
        </Cell>
      </Row>
    </Table>
  </Worksheet>
</Workbook>

我无法生成上述结构。 我用来生成结构的代码如下。

SELECT 
    Tag, 
    Parent,
    [Workbook!1!xmlns],
    [Workbook!1!xmlns:ss],
    [Worksheet!2!ss:Name],
    [Table!3!],
    [Row!4!],
    [Cell!5!],
    [Data!6!ss:Type],
    [Data!6!]
FROM ( 
SELECT top 5
    1 AS Tag,
    0 AS Parent,
    0 as SORT,
    'urn:schemas-microsoft-com:office:spreadsheet' AS [Workbook!1!xmlns],
    'urn:schemas-microsoft-com:office:spreadsheet' AS [Workbook!1!xmlns:ss],
    NULL AS [Worksheet!2!ss:Name],
    NULL AS [Table!3!],
    NULL AS [Row!4!],
    NULL AS [Cell!5!],
    NULL AS [Data!6!ss:Type],
    NULL AS [Data!6!]

UNION ALL
SELECT top 5
    2 AS Tag,
    1 AS Parent,
    AuditID * 100  as SORT,
    NULL ,
    NULL ,
    Null,
    NULL ,
    NULL ,
    NULL ,
    NULL,
    NULL 
From TempAudits
UNION ALL
SELECT top 5
    3 AS Tag,
    2 AS Parent,
    AuditID * 100+1  as SORT,
    NULL ,
    NULL ,
    NULL ,
    NULL ,
    NULL ,
    NULL ,
    NULL,
    NULL 
From TempAudits
UNION ALL
SELECT top 5
    4 AS Tag,
    3 AS Parent,
    AuditID * 100+2  as SORT,
    NULL ,
    NULL ,
    NULL ,
    NULL ,
    NULL ,
    NULL ,
    NULL,
    NULL 
    From TempAudits
UNION ALL
SELECT top 5
    5 AS Tag,
    4 AS Parent,
    AuditID * 100+3  as SORT,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL
FROM TempAudits
UNION ALL
SELECT top 5
    6 AS Tag,
    5 AS Parent,
    AuditID * 100+4  as SORT,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    'String' AS TYPE,
    AuditID AS Data
FROM TempAudits) Aa
Order by Sort
FOR XML EXPLICIT

任何人都可以帮助我。 任何帮助将不胜感激。  我正在使用此代码生成一个可以在Excel中打开的xml。

1 个答案:

答案 0 :(得分:0)

假设您执行想要使用XML Explicit来生成此输出,您可以为Cell和Data以及两个后续Union Queries添加两个标记。

编辑:我在工作表中添加了1!2!ss:标记2的名称列,以确保输出符合您的要求。

SELECT 
    Tag, 
    Parent,
    [Workbook!1!xmlns],
    [Workbook!1!xmlns:ss],
    [Worksheet!2!ss:Name],
    [Table!3!],
    [Row!4!],
    [Cell!5!],
    [Data!6!ss:Type],
    [Data!6!],
    [Cell!7!],
    [Data!8!ss:Type],
    [Data!8!]

FROM 
( 
    SELECT top 5
        1 AS Tag,
        0 AS Parent,
        0 as SORT,
        'urn:schemas-microsoft-com:office:spreadsheet' AS [Workbook!1!xmlns],
        'urn:schemas-microsoft-com:office:spreadsheet' AS [Workbook!1!xmlns:ss],
        NULL AS [Worksheet!2!ss:Name],
        NULL AS [Table!3!],
        NULL AS [Row!4!],
        NULL AS [Cell!5!],
        NULL AS [Data!6!ss:Type],
        NULL AS [Data!6!],
        NULL AS [Cell!7!],
        NULL AS [Data!8!ss:Type],
        NULL AS [Data!8!]

    UNION ALL
    SELECT top 5
        2 AS Tag,
        1 AS Parent,
        AuditID * 100  as SORT,
        NULL AS [Workbook!1!xmlns],
        NULL AS [Workbook!1!xmlns:ss],
        1 AS [Worksheet!2!ss:Name],
        NULL AS [Table!3!],
        NULL AS [Row!4!],
        NULL AS [Cell!5!],
        NULL AS [Data!6!ss:Type],
        NULL AS [Data!6!],
        NULL AS [Cell!7!],
        NULL AS [Data!8!ss:Type],
        NULL AS [Data!8!]
    From TempAudits

    UNION ALL
    SELECT top 5
        3 AS Tag,
        2 AS Parent,
        AuditID * 100+1  as SORT,
         NULL AS [Workbook!1!xmlns],
        NULL AS [Workbook!1!xmlns:ss],
        NULL AS [Worksheet!2!ss:Name],
        NULL AS [Table!3!],
        NULL AS [Row!4!],
        NULL AS [Cell!5!],
        NULL AS [Data!6!ss:Type],
        NULL AS [Data!6!],
        NULL AS [Cell!7!],
        NULL AS [Data!8!ss:Type],
        NULL AS [Data!8!]
    From TempAudits

    UNION ALL
    SELECT top 5
        4 AS Tag,
        3 AS Parent,
        AuditID * 100+2  as SORT,
        NULL AS [Workbook!1!xmlns],
        NULL AS [Workbook!1!xmlns:ss],
        NULL AS [Worksheet!2!ss:Name],
        NULL AS [Table!3!],
        NULL AS [Row!4!],
        NULL AS [Cell!5!],
        NULL AS [Data!6!ss:Type],
        NULL AS [Data!6!],
        NULL AS [Cell!7!],
        NULL AS [Data!8!ss:Type],
        NULL AS [Data!8!]
        From TempAudits

    UNION ALL
    SELECT top 5
        5 AS Tag,
        4 AS Parent,
        AuditID * 100 + 3  as SORT,
        NULL AS [Workbook!1!xmlns],
        NULL AS [Workbook!1!xmlns:ss],
        NULL AS [Worksheet!2!ss:Name],
        NULL AS [Table!3!],
        NULL AS [Row!4!],
        NULL AS [Cell!5!],
        NULL AS [Data!6!ss:Type],
        NULL AS [Data!6!],
        NULL AS [Cell!7!],
        NULL AS [Data!8!ss:Type],
        NULL AS [Data!8!]
    FROM TempAudits

    UNION ALL
    SELECT top 5
        6 AS Tag,
        5 AS Parent,
        AuditID * 100 + 4 as SORT,
        NULL AS [Workbook!1!xmlns],
        NULL AS [Workbook!1!xmlns:ss],
        NULL AS [Worksheet!2!ss:Name],
        NULL AS [Table!3!],
        NULL AS [Row!4!],
        NULL AS [Cell!5!],
        'String' AS [Data!6!ss:Type],
        AuditID AS [Data!6!],
        NULL AS [Cell!7!],
        NULL AS [Data!8!ss:Type],
        NULL AS [Data!8!]
    FROM 
    TempAudits

    UNION ALL
    SELECT top 5
       7 AS Tag,
        4 AS Parent,
        AuditID * 100 + 5 as SORT,
        NULL AS [Workbook!1!xmlns],
        NULL AS [Workbook!1!xmlns:ss],
        NULL AS [Worksheet!2!ss:Name],
        NULL AS [Table!3!],
        NULL AS [Row!4!],
        NULL AS [Cell!5!],
        NULL AS [Data!6!ss:Type],
        NULL AS [Data!6!],
        NULL AS [Cell!7!],
        NULL AS [Data!8!ss:Type],
        NULL AS [Data!8!]
    FROM TempAudits

    UNION ALL
    SELECT top 5
        8 AS Tag,
        7 AS Parent,
        AuditID * 100 + 6 as SORT,
        NULL AS [Workbook!1!xmlns],
        NULL AS [Workbook!1!xmlns:ss],
        NULL AS [Worksheet!2!ss:Name],
        NULL AS [Table!3!],
        NULL AS [Row!4!],
        NULL AS [Cell!5!],
        NULL AS [Data!6!ss:Type],
        NULL AS [Data!6!],
        NULL AS [Cell!7!],
        'String' AS [Data!8!ss:Type],
        AuditDate AS [Data!8!]
    FROM 
    TempAudits 
) Aa
Order by Sort
FOR XML EXPLICIT