2010-04-09 85 views
2

如何属性的数据在同一列的SQL复制到新的属性如何在一个新的属性中复制属性值

原始数据

<root> 
<child attr='hello'></child> 
</root> 

结果1

<root> 
<child attr='hello' attr2='hello'></child> 
</root> 

结果2(带有修改)

<root> 
<child attr='hello' attr2='**H**ello **W**orld'></child> 
</root> 

我想只有通过SQL XML的Xquery

+0

我不想使用xslt – 2010-04-09 08:38:33

回答

0

假设我们可以使用XSLT来做到这一点,我会做这样的:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="1.0"> 

    <xsl:output method="xml" indent="yes" omit-xml-declaration="no" /> 

    <xsl:template match="root"> 
     <xsl:copy> 
      <xsl:apply-templates/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="child"> 
     <xsl:copy> 
      <xsl:copy-of select="@*"/> 
      <xsl:attribute name="attr2"> 
       <xsl:value-of select="@attr"/> 
      </xsl:attribute> 
     </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 

或者,如果你想在结果2修改,替换<xsl:template match="child">有以下几点:

<xsl:template match="child"> 
    <xsl:copy> 
     <xsl:copy-of select="@*"/> 
     <xsl:attribute name="attr2"> 
      <xsl:value-of> 
       <xsl:text>**H**ello **W**orld</xsl:text> 
      </xsl:value-of> 
     </xsl:attribute> 
    </xsl:copy> 
</xsl:template> 
+0

我只想通过SQL来完成此操作XML Xquery – 2010-04-14 10:34:22

1

我不知道我跟着你想究竟是什么在第二个结果,但我会在它采取了刺:下面的第一个例子将产生你的结果#1 (我把你的或在的test.xml iginal数据和您的真实数据假设的“孩子”和“教师责任观”可能会重复):

<root>{ 
    for $child in doc('test.xml')/root/* 
    return 
    element {name($child)} { 
     for $attr at $index in $child/@* 
     return (
     attribute {name($attr)} {$attr}, 
     attribute {concat(name($attr), 2)} {$attr} 
    ) 
    } 
}</root> 

它可以修改放在一个不同的值,如在结果#2,如如下:

<root>{ 
    for $child in doc('test.xml')/root/* 
    return 
    element {name($child)} { 
     for $attr at $index in $child/@* 
     return (
     attribute {name($attr)} {$attr}, 
     attribute {concat(name($attr), 2)} { 
      '**H**ello **W**orld' 
     } 
    ) 
    } 
}</root> 

希望有所帮助。

+0

我只想通过SQL XML Xquery – 2010-04-14 10:32:50

0
DECLARE @xmlData table (
    data xml 
    ) 


INSERT INTO @xmlData(data) 
VALUES ('<root> 
    <child attr="hello"></child> 
    </root>') 

.modify()执行的所有操作

UPDATE @xmlData 
SET data.modify('insert (attribute attr2 {"hello"}) into (/root/child)[1]') 

验证数据是正确的

SELECT * 
FROM @xmlData 


UPDATE @xmlData 
SET data.modify('replace value of (/root/child/@attr2)[1] with "**H**ello **W**orld" ') 

验证数据是正确的

SELECT * 
FROM @xmlData 

enter image description here

相关问题