很多时候,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语言元素。
这是我通常会接近它:
- 计算第一的位置“ - ”
- 如果没有找到,返回
title
元素是和一个空的artist
元 - 如果在位置0中,然后将其删除
title
元素,然后返回其余title
标签作为新title
元素和一个空的artist
元 - 如果在位置上发现长度为3,然后从删除它
title
元素,则返回的其余部分title
标签作为新artist
元件和一个空的title
元素 - 它是在大于0的位置发现,所有内容复制直到位置
artist
元,之后返回一切如新title
元素
除了谈“删除”等,不适用(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>