0

我正在尝试编写一个转换以下 cdcatalog.xml 的 xquery 语句,以便将“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>
4

1 回答 1

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
    }
}
于 2013-11-01T15:32:10.950 回答