对于XML Explicit SQL复制元素

时间:2011-02-03 14:34:25

标签: sql-server xml tsql xsd

我需要两个元素作为根的子元素出现。它们必须按字母顺序排列,因此elementA必须在elementZ之前。 ElementZ应该只出现一次,而elementA需要多次出现。任何帮助必须赞赏。这大大简化了一个更大的查询,它阻止了“路径”的使用,所以我需要一个使用显式的解决方案。

非常感谢

Declare  @xml xml 

DECLARE @tab table (
                    root_element nvarchar(10),
                    elementA nvarchar(10),
                    elementZ nvarchar(10)
                    )

insert @tab
(root_element, elementA, elementZ)
select 'one' , 'many', 'one' union all
select 'one' , 'many1', 'one' union all
select 'one' , 'many2', 'one' union all
select 'one' , 'many3', 'one' union all
select 'one' , 'many4', 'one' union all
select 'one' , 'many5', 'one' 

SET @xml =(
    SELECT DISTINCT
        TAG, 
        Parent,
        [root_element!1!value],
        [elementA!2!value],
        [elementZ!3!value]
    FROM
    (
    SELECT  DISTINCT
        1   AS TAG, 
        NULL    AS Parent,
        root_element AS [root_element!1!value],
        NULL AS [elementA!2!value],
        NULL AS [elementZ!3!value]
    FROM @tab
    UNION ALL
    SELECT  DISTINCT
        2,      
        1,          
        root_element,
        elementA,
        NULL
    FROM @tab
    UNION ALL
    SELECT  DISTINCT
        3,       
        1,          
        root_element,
        elementA,
        elementZ 
        FROM @tab
    )a
    ORDER BY 
    [root_element!1!value],
        [elementA!2!value],
        [elementZ!3!value]
    FOR XML EXPLICIT
    )

    select  @xml

    --results in this 
    '<root_element value="one">
  <elementA value="many" />
  <elementZ value="one" />
  <elementA value="many1" />
  <elementZ value="one" />
  <elementA value="many2" />
  <elementZ value="one" />
  <elementA value="many3" />
  <elementZ value="one" />
  <elementA value="many4" />
  <elementZ value="one" />
  <elementA value="many5" />
  <elementZ value="one" />
</root_element>'

--but i want this.
'<root_element value="one">
  <elementA value="many" />
  <elementA value="many1" />
  <elementA value="many2" />
  <elementA value="many3" />
  <elementA value="many4" />
  <elementA value="many5" />
  <elementZ value="one" />
</root_element>'
</code>

1 个答案:

答案 0 :(得分:0)

创建3标签时,请勿重新选择ElementA。这将删除重复项。 按标签排序也会将elementA放在顶部

    SELECT DISTINCT 
        TAG, 
        Parent,
        [root_element!1!value],
        [elementA!2!value],
        [elementZ!3!value]
    FROM
    (
    SELECT  DISTINCT
        1   AS TAG, 
        NULL    AS Parent,
        root_element AS [root_element!1!value],
        NULL AS [elementA!2!value],
        NULL AS [elementZ!3!value]
    FROM @tab
    UNION ALL
    SELECT  DISTINCT
        2,      
        1,          
        root_element,
        elementA,
        NULL
    FROM @tab


     UNION SELECT  DISTINCT
        3,       
        1,          
        root_element,
        NULL, --ElementA 
        elementZ 
        FROM @tab


    )a
    ORDER BY 
    TAG,
    [root_element!1!value],
        [elementA!2!value],
        [elementZ!3!value]
        FOR XML EXPLICIT

如果删除XML Explicit并查看表格输出,则更容易看出出现了什么问题

原始

TAG         Parent      root_element!1!value elementA!2!value elementZ!3!value
----------- ----------- -------------------- ---------------- ----------------
1           NULL        one                  NULL             NULL
2           1           one                  many             NULL
3           1           one                  many             one
2           1           one                  many1            NULL
3           1           one                  many1            one
2           1           one                  many2            NULL
3           1           one                  many2            one
2           1           one                  many3            NULL
3           1           one                  many3            one
2           1           one                  many4            NULL
3           1           one                  many4            one
2           1           one                  many5            NULL
3           1           one                  many5            one

修改为Null而不是ElementA

TAG         Parent      root_element!1!value elementA!2!value elementZ!3!value
----------- ----------- -------------------- ---------------- ----------------
1           NULL        one                  NULL             NULL
3           1           one                  NULL             one
2           1           one                  many             NULL
2           1           one                  many1            NULL
2           1           one                  many2            NULL
2           1           one                  many3            NULL
2           1           one                  many4            NULL
2           1           one                  many5            NULL