0

xlst 変換から不要な出力を除外する際に問題があります。一致などの背後にあるデフォルトのルールについては既に知っていますが、テンプレート/適用テンプレートで適切に一致を使用できません。
これを修正するのを手伝ってもらえますか?

だから私はこのように構造化されたXMLファイルを持っています:

<movies>
    <movie id="0">
        <title>Title</title>
        <year>2007</year>
        <duration>113</duration>
        <country>Country</country>
        <plot>Plot</plot>
        <poster>img/posters/0.jpg</poster>
        <genres>
            <genre>Genre1</genre>
            <genre>Genre2</genre>
        </genres>
        ...
    </movie>
    ...
</movies>

そして、ジャンル '#######' (実行時に私の perl スクリプトに置き換えられます) に属する各映画の LI を含む html UL リストを作成したいと思います。 )。

今、私はこのようにしています:

<xsl:template match="/">
    <h2> List </h2>
    <ul>
        <xsl:apply-templates match="movie[genres/genre='#######']"/>
            <li>
                <a>
                    <xsl:attribute name="href">     
                        /movies/<xsl:value-of select= "@id" />.html
                    </xsl:attribute>
                    <xsl:value-of select= "title"/>
                </a>
            </li>
    </ul>
</xsl:template>

明らかに、このようにして、選択したジャンルに一致する映画のすべての要素が表示されます。<xsl:template match="...">余分な出力をすべて削除するには、大量に追加する必要がありますか?
このような HTML スニペットを作成する正しい方法を教えてください。

リスト

前もって感謝します!

4

2 に答える 2

4

ダッシュの解決策は正しいです。

より簡潔にするために、ムービーテンプレートに少しバリエーションを加えることをお勧めします...

<xsl:template match="movie">
  <li>
    <a href="/movies/{@id}.html">
      <xsl:value-of select= "title"/>
    </a>
  </li>
</xsl:template>
于 2012-08-15T13:14:20.613 に答える
1

あなたはもうすぐそこにいます - apply-templates の使用が問題を引き起こしています。

代わりに、次のように XSLT を構成してください。

  <xsl:template match="/">
    <h2> List </h2>
    <ul>
      <xsl:apply-templates select="movie[genres/genre='#######']"/>
    </ul>
  </xsl:template>

  <xsl:template match="movie">
    <li>
      <a>
        <xsl:attribute name="href">/movies/<xsl:value-of select= "@id" />.html</xsl:attribute>
        <xsl:value-of select= "title"/>
      </a>
    </li>
  </xsl:template>

特定のテンプレート (match="movie") を movie 要素に適用します。最初の試行では、movie 要素に含まれるすべてのものを戻すデフォルトのテンプレートを使用します。

于 2012-08-15T12:23:06.730 に答える