编写一个转换cdcatalog.xml的xquery,将'country'元素更改为'cd'元素的属性

时间:2013-11-01 15:24:08

标签: xquery

我正在尝试编写一个xquery语句,该语句转换以下cdcatalog.xml,以便将'country'元素更改为'cd'元素的属性,并将'id'属性添加到'cd'元素这是一个递增的整数(1,2,3 ......)。 FLOWR是否适用于此处,或者我必须使用其他功能才能添加和执行更改? cdcatalog.xml如下:

<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>

1 个答案:

答案 0 :(得分: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
    }
}