-2

给定以下示例 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>

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

 <?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 实现上述结果?

4

1 回答 1

1

我要做的是剥离现有元素并用新元素author替换该元素。titleauthor

author元素的值将是原始元素author(如果存在)和当前title值的字符串连接,由|.

例子...

<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注意:如果您替换为身份转换,这也可以作为 XSLT 2.0 工作。

于 2018-02-26T22:18:00.263 回答