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?

有帮助吗?

解决方案

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>

First for is taking the distinct values of the tag Type, then for each distinct value of this, the respective values of all the Name tags are derived.

Then using the concat function I have concatenated the Type text with the string generated by string-join, used to add/append the Name and , (comma).

HTH :)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top