1

多くの場合、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要素として返します
4

1 に答える 1

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>
于 2013-06-22T21:25:50.953 に答える