使用 xslt-3 将元素从 xml 文件移动到另一个 xml 元素

move element from an xml file to another xml element using xslt-3

给定以下示例 xml 文件:

<?xml version="1.0"?>
        <catalog>
           <book id="bk101">
              <author>Gambardella, Matthew</author>
              <title>XML Developer's Guide</title>
              <genre>Computer</genre>
              <price>44.95</price>
              <publish_date>2000-10-01</publish_date>
              <description>An in-depth look at creating applications 
              with XML.</description>
           </book>
           <book id="bk102">
              <author>Ralls, Kim</author>
              <title>Midnight Rain</title>
              <genre>Fantasy</genre>
              <price>5.95</price>
              <publish_date>2000-12-16</publish_date>
              <description>A former architect battles corporate zombies, 
              an evil sorceress, and her own childhood to become queen 
              of the world.</description>
           </book>
           <book id="bk103">
              <title>Maeve Ascendant</title>
              <genre>Fantasy</genre>
              <price>5.95</price>
              <publish_date>2000-11-17</publish_date>
              <description>After the collapse of a nanotechnology 
              society in England, the young survivors lay the 
              foundation for a new society.</description>
           </book>
        </catalog>

我需要将所有 title 元素合并到 author 元素中,规则如下: 如果 author 元素已经存在,那么 title 元素的值将被插入到 author 记录中,使用分隔符即 |特点。 如果记录中没有 author 元素,则 title 元素将成为 author 元素。 期望的输出:

 <?xml version="1.0"?>
        <catalog>
           <book id="bk101">
              <author>Gambardella, Matthew | XML Developer's Guide</author>
              <genre>Computer</genre>
              <price>44.95</price>
              <publish_date>2000-10-01</publish_date>
              <description>An in-depth look at creating applications 
              with XML.</description>
           </book>
           <book id="bk102">
              <author>Ralls, Kim | Midnight Rain</author>
              <genre>Fantasy</genre>
              <price>5.95</price>
              <publish_date>2000-12-16</publish_date>
              <description>A former architect battles corporate zombies, 
              an evil sorceress, and her own childhood to become queen 
              of the world.</description>
           </book>
           <book id="bk103">
              <author>Maeve Ascendant</author>
              <genre>Fantasy</genre>
              <price>5.95</price>
              <publish_date>2000-11-17</publish_date>
              <description>After the collapse of a nanotechnology 
              society in England, the young survivors lay the 
              foundation for a new society.</description>
           </book>
        </catalog>

如何使用 xslt-3 实现上述结果?

我要做的是去除现有的 author 元素并用新的 author 元素替换 title 元素。

author 元素的值将是原始 author(如果存在)的 string-join 和由 [= 分隔的当前 title 值17=].

示例...

<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:template match="author"/>

  <xsl:template match="title">
    <author>
      <xsl:value-of select="string-join((../author,.),' | ')"/>
    </author>
  </xsl:template>

</xsl:stylesheet>

注意:如果您将 xsl:mode 替换为 identity transform,这也可以用作 XSLT 2.0。