4

Below is the XML part of my data.

<A>
 <a><Type>Fruit</Type><Name>Banana</Name></a>
 <a><Type>Fruit</Type><Name>Orange</Name></a>
 <a><Type>Fruit</Type><Name>Apple</Name></a>
 <a><Type>Fruit</Type><Name>Lemon</Name></a>
 <a><Type>Cars</Type><Name>Toyota</Name></a>
 <a><Type>Cars</Type><Name>Lamborghini</Name></a>
 <a><Type>Cars</Type><Name>Renault</Name></a>
</A>

Out put as -

<a>Fruits-Banana,Orange,Apple,Lemon</a>
<a>Cars-Toyota,Lamborghini,Renault</a>

I tried to get the required output by all in vain. I tried 'group by` clause too, but getting errors.

any help?

4

1 回答 1

4
let $x:=
<A>
  <a><Type>Fruit</Type><Name>Banana</Name></a>
  <a><Type>Fruit</Type><Name>Orange</Name></a>
  <a><Type>Fruit</Type><Name>Apple</Name></a>
  <a><Type>Fruit</Type><Name>Lemon</Name></a>
  <a><Type>Cars</Type><Name>Toyota</Name></a>
  <a><Type>Cars</Type><Name>Lamborghini</Name></a>
  <a><Type>Cars</Type><Name>Renault</Name></a>
</A>

  for $z in distinct-values($x//a/Type)
    let $c := $x//a[Type=$z]/Name
    return
       <a>{concat($z, "-", string-join($c, ","))}</a>

首先for是获取 tag 的不同值Type,然后对于 this 的每个不同值,Name导出所有标签的各自值。

然后使用concat我将Type文本与生成的字符串连接起来的函数string-join,用于添加/附加Name,(逗号)。

HTH :)

于 2014-01-22T13:42:13.330 回答