XSLT分裂MP3标签为艺术家和标题(xslt split mp3 tag into artist

2019-10-18 06:36发布

很多时候,MP3标签是“艺术家 - 标题”的形式,而是存储在标题字段。

我想值分成艺术家+标题字段。

的前实施例/分裂后:

<title>Artist - Title</title>
<title> - Title</title>
<title>Artist - </title>
<title>A title</title>

后:

<artist>Artist</artist><<title>Title</title>
<artist /><title>Title</title>
<artist>Artist</artist><title />
<artist /><title>A title</title>

我没有做过在XSLT编程了,所以我不知道如果我在正规语言使用成语会适合,如果确实如此,将最好用什么XSLT语言元素。

这是我通常会接近它:

  1. 计算第一的位置“ - ”
  2. 如果没有找到,返回title元素是和一个空的artist
  3. 如果在位置0中,然后将其删除title元素,然后返回其余title标签作为新title元素和一个空的artist
  4. 如果在位置上发现长度为3,然后从删除它title元素,则返回的其余部分title标签作为新artist元件和一个空的title元素
  5. 它是在大于0的位置发现,所有内容复制直到位置artist元,之后返回一切如新title元素

Answer 1:

除了谈“删除”等,不适用(XSLT程序读取输入和产生输出;他们不改变自己的输入),你的描述是一个不错的比赛。 这里(未测试)是一个会怎么写呢(除了我不会评论它相当这里戒备):

<xsl:template match="title">
  <!--* input often has artist - title in title element *-->
  <!--* So emit an artist element and populate it with
      * the string value preceding the hyphen.
      * (If there is no hyphen, string-before(.,'-') returns ''.)
      * Normalize space to lose the pre-hyphen blank.
      * If hyphens can appear in normal titles, change '-'
      * to ' - '.
      *-->
  <xsl:element name="artist">
    <xsl:value-of select="normalize-space(
                          substring-before(.,'-'))"/>
  </xsl:element>

  <!--* Now emit a title with the rest of the value. *-->
  <xsl:element name="title">
    <xsl:choose>
      <xsl:when test="contains(.,'-')">
        <xsl:value-of select="normalize-space(
                              substring-after(.,'-'))"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:element>
</xsl:template>


文章来源: xslt split mp3 tag into artist and title
标签: xslt text split