0

我有这样的数据:

<?xml version="1.0" encoding="UTF-8"?>
<colors>
   <color id="ff0000">
      <label language="en">red</label>
      <label language="de">rot</label>
      <label language="es">rojo</label>
   </color>
   <color id="008000">
      <label language="en">green</label>
      <label language="de">gruen</label>
      <label language="es">verde</label>
   </color>
   <color id="0000ff">
      <label language="en">blue</label>
      <label language="de">blau</label>
      <label language="es">azul</label>
   </color>
</colors>

我想把它转换成这样的表格

+--------+----------------------------+
| id     | labels                     |
+--------+----------------------------+
| ff0000 | en:red|de:rot|es:rojo      |
+--------+----------------------------+
| 008000 | en:green|de:gruen|es:verde |
+--------+----------------------------+
| 0000ff | en:blue|de:blau|es:azul    |
+--------+----------------------------+

我知道如何从同一个父元素的上下文中连接多个值,我不知道如何粘贴一个元素的属性和数据,然后将这些组合粘贴到父元素中。

declare option output:method "csv";
declare option output:csv "header=yes, separator=tab";

for $color in doc(
  'color_words'
)/colors/color

let $id := data($color/@id)
let $language := fn:normalize-space(string-join($color/label/@language,'|'))
let $label := fn:normalize-space(string-join($color/label,'|'))

return

<csv>
  <record>
    <id>
      {$id}
    </id>
    <language>
      {$language}
    </language>
    <label>
      {$label}
    </label>
  </record>
</csv>
+--------+----------+-------------------+
| id     | language | label             |
+--------+----------+-------------------+
| ff0000 | en|de|es | red|rot|rojo      |
+--------+----------+-------------------+
| 008000 | en|de|es | green|gruen|verde |
+--------+----------+-------------------+
| 0000ff | en|de|es | blue|blau|azul    |
+--------+----------+-------------------+
4

1 回答 1

1

你可以使用这个 let 表达式:

let $label := fn:normalize-space(string-join($color/label/concat(@language,':',.),'|'))

它将创建所需的输出。另一个 let 表达式不是必需的。

于 2021-07-16T13:35:14.330 回答