2012-10-21 19 views
1

我正在使用返回屬性信息的API。一些文本信息存儲在子節點中,我想將它連接成單個字符串(VARCHAR)。將子節點的文本連接到單個列中

我的過程是,我通過web服務獲取xml,然後將其傳遞給提取xml值並將它們插入到視圖中的proc,這是我返回的xml代碼片段:

<properties> 
    <property propertyid="1234"> 
     <bullets> 
      <bullet>nice garden</bullet> 
      <bullet>it smells a bit</bullet> 
      <bullet>body under the patio</bullet> 
     </bullets> 
    </property> 
    ... 
</properties> 

這是窺探到XML被查詢如何從中提取值:

INSERT   
INTO   VProperty 
(   PropertyId, 
       Description 
) 
SELECT  P.value('@propertyid', 'INT'), 
       NULL -- extract all the bullet text values into a single string 
FROM   @xml.nodes('/properties/property') 

在這個例子中,我希望能夠從XML所以它提取信息結果如下:

PropertyId Description 
1234   'nice garden\r\nit smells a bit\r\nbody under the patio 

這是可能的純sql/xml或我需要在xml執行一些預處理之前,我進入SQL土地?

任何幫助非常感謝(一如既往)。

回答

3

這是否適合您?

DECLARE @XML XML = 
('<properties> 
    <property propertyid="1234"> 
     <bullets> 
      <bullet>nice garden</bullet> 
      <bullet>it smells a bit</bullet> 
      <bullet>body under the patio</bullet> 
     </bullets> 
    </property> 
    <property propertyid="2345"> 
     <bullets> 
      <bullet>bullet 2345_1</bullet> 
      <bullet>bullet 2345_2</bullet> 
      <bullet>bullet 2345_3</bullet> 
     </bullets> 
    </property> 
</properties>'); 

SELECT X.node.value('@propertyid', 'INT'), 
     STUFF((SELECT '\r\n' + B.bullet.value('.', 'NVARCHAR(MAX)') 
       FROM X.node.nodes('./bullets/bullet') B (bullet) 
       FOR XML PATH(''),TYPE).value('.', 'NVARCHAR(MAX)'), 
       1, 4, '') AS Description 
FROM @xml.nodes('/properties/property') X (node); 
+0

繁榮。我在尋找解決方案時遇到了類似的情況,但無法完成工作 - 完美地工作,謝謝。 –

0

您可以使用此XQuery的(不知道SQL Server 2008中的XQuery的話,就是與它完全兼容):

for $pr in /*/property, 
    $id in $pr/@propertyid, 
    $desc in string-join($pr/*/*, '&#xD;&#xA;') 
    return 
    (<PropertyId>{string($id)}</PropertyId>, 
    <Description>{$desc}</Description>) 

隨着撒克遜和XQSharp(XmlPrime)應用的結果所提供的XML文檔此查詢:

<properties> 
    <property propertyid="1234"> 
     <bullets> 
      <bullet>nice garden</bullet> 
      <bullet>it smells a bit</bullet> 
      <bullet>body under the patio</bullet> 
     </bullets> 
    </property> 
    ... 
</properties> 

<PropertyId>1234</PropertyId> 
<Description>nice garden&#xD; 
it smells a bit&#xD; 
body under the patio</Description> 
+0

這看起來非常優雅,但我一直無法將它放入我的示例 - 可能是像您提到的兼容性問題。感謝您的時間。 –

+0

@PhilCooper,知道SQL Server XQuery的人可以提供幫助。 :) –

0
DECLARE @XML XML 
SET @XML = 
'<properties> 
    <property propertyid="1234"> 
    <bullets> 
     <bullet>nice garden</bullet> 
     <bullet>it smells a bit</bullet> 
     <bullet>body under the patio</bullet> 
    </bullets> 
    </property> 
    <property propertyid="2345"> 
    <bullets> 
     <bullet>bullet 2345_1</bullet> 
     <bullet>bullet 2345_2</bullet> 
     <bullet>bullet 2345_3</bullet> 
    </bullets> 
    </property> 
</properties>' 

SET @XML = @XML.query(' 
    for $prop in /properties/property 
    return 
    <property propertyid="{string($prop/@propertyid)}"> 
     <description> 
     { 
      for $bullet in $prop/bullets[1]/bullet 
      return 
       if ($bullet is $prop/bullets[1]/bullet[1] and $bullet << $prop/bullets[1]/bullet[last()]) 
       then concat(string($bullet), "\r\n") 
       else 
        if ($bullet is $prop/bullets[1]/bullet[last()]) 
        then string($bullet) 
        else concat(string($bullet), "\r\n") 
     }  
     </description> 
    </property> 
') 

SELECT 
Tab.ColXML.value('@propertyid', 'INTEGER') AS PropertyId, 
Tab.ColXML.value('description[1]', 'NVARCHAR(128)') AS Description 
FROM @XML.nodes('//property') AS Tab(ColXML) 

,因爲在MS SQL Server 2005中沒有讓條款,我不得不四處找工作。

在query()方法的表達式中,我使用了2個嵌套的「類似循環」的子句來選擇需要的元素。 構建[property]元素的第一個[for子句]。第二個[for子句]從[bullet]元素列表中構建[description]元素,[property]的子元素。

的[說明]將獲得來自像下面的方式[子彈]元素列表內置序列值:

首不見尾頁[子彈]將「\ r \ n追加」。

不是最後一個[bullet]也會附加「\ r \ n」。

我們保持其他[bullet]完好無損。

我們將所有爲每個字符串構建的字符串連接起來,以便爲相關的[描述]創建值。

相關問題