1

いくつかのヘッダー情報と一連の項目を持つ XML のデータがあります。私は XSLT を使用して、ヘッダー領域と一連の項目を含む別の形式に変換しています。

ただし、翻訳後の結果では、同じ値を繰り返すだけであっても、ヘッダーにのみ含まれる 1 つのデータをアイテムのすべてのインスタンスに含める必要があります。(この値は変更される可能性があるため、ハードコーディングできません)

サンプルデータ (大幅に簡略化)

<rss>
  <channel>
    <title>Playlist One</title>
    <items>
      <item>
        <title>Video One</title>
      </item>
      <item>
        <title>Video Two</title>
      </item>
      <item>
        <title>Video Three</title>
      </item>
    </items>
  </channel>
</rss>

私の望む結果は次のようなものです:

    playlist_header_title=Playlist One

    playlist_title=Playlist One
    video_title=Video One

    playlist_title=Playlist One
    video_title=Video Two

    playlist_title=Playlist One
    video_title=Video Three

私の XSLT は非常に複雑です (残念ながら、他の人から継承したものなので、すべてが何をするのかわかりません。オンラインで独学しましたが、少し頭がいっぱいです)。

大まかに重要な部分は次のようになります。

    <xsl:template name="rss" match="/">
      <xsl:variable name="playlist_title">
        <xsl:value-of select="string(/rss/channel/title)"/>
      </xsl:variable>
      <xsl:for-each select="rss">   
        <xsl:apply-templates name="item" select="channel/items/item"/>
      </xsl:for-each>   
    </xsl:template>

次に、ここには含めない「item」という巨大なテンプレートがありますが、基本的にはすべてのアイテム データを必要に応じて出力します。「playlist_title」にアクセスする方法がわかりません。

(テンプレート「item」内から)呼び出そうとすると

    <xsl:value-of select="string($playlist_title)"/>

空白を返します。これは、その変数が for-each ループの外で作成されたため、使用できないためだと思います。(ヘッダーの結果バージョンの for-each ループの前に出力すると、データが正しく表示されますが、それでは十分ではありません)

apply-template で with-param を使用してみました。また、with-param を使用して別のループ内で call-template に変更しようとしましたが、空白も表示されます。

テンプレートに任意の値を渡すことができることを確認するためだけに、XML からプレイリスト_タイトルを取得するのではなく、文字列を送信しようとしましたが、それらも空白になります。

例えば:

<xsl:for-each select="channel/items/item">  
    <xsl:call-template name="item">
        <xsl:with-param name="playlist_title">blah</xsl:with-param>
    </xsl:call-template>
</xsl:for-each> 


<xsl:template name="item">  
    <xsl:param name="playlist_title" />
            playlist_title=<xsl:value-of select="string($playlist_title)"/>
            video_title=...
    ...
    </xsl:template>

これは値「何とか」を返さず、空白だけを返しました。(私の希望は、「何とか」をXMLから引き出されたプレイリスト_タイトルの値に置き換えることでしたが、どれもうまくいきません)

私は困惑しています!助けてくれてありがとう。

4

1 に答える 1

0

最初の例では、変数がそのテンプレート内に存在しないため、$playlist_titleテンプレートの内部を参照しようとすると失敗します。itemのテンプレート マッチ内で作成され、rssローカル スコープです。

を他のテンプレートで使用できるようにする場合は、テンプレートの外側で「グローバル スコープ」で$playlist_title宣言する必要があります。rss

例えば:

<xsl:variable name="playlist_title">
    <xsl:value-of select="string(/rss/channel/title)"/>
</xsl:variable>
<xsl:template name="rss" match="/">
    <xsl:for-each select="rss">   
        <xsl:apply-templates name="item" select="channel/items/item"/>
    </xsl:for-each>   
</xsl:template>

を参照する 2 回目の試みについてはxsl:param playlist_title、うまくいきました。宣言されたパラメーターの名前と参照しているパラメーターの名前の間にタイプ o またはその他の違いがないことを再確認してください。


を使用して、次のようなことを試して、目的の出力を実現できますxsl:variable

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" indent="yes"/>

    <xsl:variable name="playlist_title" select="/rss/channel/title" />

    <xsl:template match="/">
        <xsl:apply-templates select="rss/channel"/>         
    </xsl:template>

    <xsl:template match="channel">
        <xsl:text>playlist_header_title=</xsl:text>
        <xsl:value-of select="title"/>
        <xsl:text>&#xA;&#xA;</xsl:text>
        <xsl:apply-templates select="items/item"/> 
    </xsl:template>

    <xsl:template match="item">  
        <xsl:text>playlist_title=</xsl:text>
        <xsl:value-of select="$playlist_title"/>
        <xsl:text>&#xA;</xsl:text>
        <xsl:text>video_title=</xsl:text>
        <xsl:value-of select="title"/>
        <xsl:text>&#xA;&#xA;</xsl:text>
    </xsl:template>

</xsl:stylesheet>

または、 を削除してxsl:variableを使用できxsl:paramます。

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" indent="yes"/>

    <xsl:template match="/">
        <xsl:apply-templates select="rss/channel"/>         
    </xsl:template>

    <xsl:template match="channel">
        <xsl:text>playlist_header_title=</xsl:text>
        <xsl:value-of select="title"/>
        <xsl:text>&#xA;&#xA;</xsl:text>
        <xsl:apply-templates select="items/item">
            <xsl:with-param name="playlist_title" select="title"/>
        </xsl:apply-templates> 
    </xsl:template>

    <xsl:template match="item">
        <xsl:param name="playlist_title" />
        <xsl:text>playlist_title=</xsl:text>
        <xsl:value-of select="$playlist_title"/>
        <xsl:text>&#xA;</xsl:text>
        <xsl:text>video_title=</xsl:text>
        <xsl:value-of select="title"/>
        <xsl:text>&#xA;&#xA;</xsl:text>
    </xsl:template>

</xsl:stylesheet>
于 2013-06-08T19:00:21.547 に答える