2013-11-01 15 views
0

我正在嘗試編寫一個xquery語句來轉換以下cdcatalog.xml以更改'country '元素作爲'cd'元素的屬性,並將'id'屬性添加到作爲遞增整數(1,2,3 ...)的'cd'元素。 FLOWR是否適用於此,或者我必須使用其他函數才能添加和執行更改? 的cdcatalog.xml如下:編寫一個轉換cdcatalog.xml的xquery,將'country'元素更改爲'cd'元素的屬性

<catalog> 
    <cd> 
    <title>Empire Burlesque</title> 
    <artist>Bob Dylan</artist> 
    <country>USA</country> 
    <company>Columbia</company> 
    <price>10.90</price> 
    <year>1985</year> 
    </cd> 
    <cd> 
    <title>Hide your heart</title> 
    <artist>Bonnie Tyler</artist> 
    <country>UK</country> 
    <company>CBS Records</company> 
    <price>9.90</price> 
    <year>1988</year> 
    </cd> 
    <cd> 
    <title>Greatest Hits</title> 
    <artist>Dolly Parton</artist> 
    <country>USA</country> 
    <company>RCA</company> 
    <price>9.90</price> 
    <year>1982</year> 
    </cd> 
    <cd> 
    <title>Still got the blues</title> 
    <artist>Gary Moore</artist> 
    <country>UK</country> 
    <company>Virgin records</company> 
    <price>10.20</price> 
    <year>1990</year> 
    </cd> 
</catalog> 

和輸出應該是這樣的

<catalog> 
<cd id="1" country="USA"> 
    <title>Empire Burlesque</title> 
    <artist>Bob Dylan</artist> 
    <company>Columbia</company> 
    <price>10.90</price> 
    <year>1985</year> 
</cd> 
<cd id="2" country="UK"> 
    <title>Hide your heart</title> 
    <artist>Bonnie Tyler</artist> 
    <company>CBS Records</company> 
    <price>9.90</price> 
    <year>1988</year> 
</cd> 

回答

2
element catalog { 
    for $cd at $pos in doc('cdcatalog.xml')/catalog/cd 
    return 
    element cd { 
     attribute id { $pos }, 
     attribute country { $cd/country }, 
     $cd/* except $cd/country 
    } 
} 
+0

謝謝!真的有幫助的迴應。 – Loic