0

画像を取得して 2 つのテンプレートのいずれかに出力するテンプレートがあります。

個々の画像を追跡し、それぞれの値に基づいて出力したいと思います。現在、1 つの画像がワイドの場合、すべてワイド テンプレートに従って出力されます。むしろ両方のテンプレートを利用したいと思います。

<xsl:template name="get-images">
    <xsl:param name="image-entry"/>
    <xsl:choose>

        <xsl:when test="($image-entry/image/meta/@width) &gt; ($image-entry/image/meta/@height)">
        <xsl:apply-templates select="$image-entry/image" mode="wide">
            <xsl:with-param name="image-class" select="'full-width'"/>
            <xsl:with-param name="caption-class" select="'full-width-caption'"/>
        </xsl:apply-templates>
        </xsl:when>

        <xsl:otherwise>
            <xsl:apply-templates select="$image-entry/image" mode="tall">
            <xsl:with-param name="image-class" select="'center'"/>
            <xsl:with-param name="caption-class" select="'full-width-caption'"/>
            </xsl:apply-templates>
        </xsl:otherwise>

    </xsl:choose>
</xsl:template>

<xsl:template match="image" mode="wide">
    <xsl:param name="image-class" />
    <xsl:param name="caption-class" />

    <a href="{$root}/image/full{@path}/{filename}">
        <img src="{$root}/image/wide{@path}/{filename}" alt="{../description}" class="{$image-class}"/>
    </a>

    <p class="{$caption-class}">
        Image courtesy of: <a href="{../source}"><xsl:value-of select="../title"/></a>
    </p>
</xsl:template>

<xsl:template match="image" mode="tall">
    <xsl:param name="image-class" />
    <xsl:param name="caption-class" />

    <span class="centered">
        <a href="{$root}/image/full{@path}/{filename}">
            <img src="{$root}/image/tall{@path}/{filename}" alt="{../description}" class="{$image-class}"/>
        </a>
    </span>

    <p class="{$caption-class}">
        Image courtesy of: <a href="{../source}"><xsl:value-of select="../title"/></a>
    </p>
</xsl:template>

おまけの質問: 値のソースが存在しない場合、キャプションを無視するにはどうすればよいですか?

            <article-images field-id="24" subsection-id="5" items="1">
                <item id="109" creation-date="2014-04-24T05:16:52+01:00">
                    <image size="317 KB" path="/uploads" type="image/jpeg">
                        <filename>funny-lolcat.jpg</filename>
                        <meta creation="2014-04-24T05:16:52+01:00" width="1600" height="1200" />
                    </image>
                    <description mode="formatted"><p>Aww!</p>
</description>
                    <title handle="" />
                    <source handle="" />
                </item>
            </article-images>
4

1 に答える 1

1

2 つのモードの代わりに、1 つのモードを使用して、ワイド vs ノット ワイド ロジックをテンプレート マッチ式に移動します。

<xsl:template match="image[meta/@width &gt; meta/@height]">
  <!-- logic for wide image -->
</xsl:template>

<xsl:template match="image">
  <!-- logic for not-wide image -->
</xsl:template>

そして今、テンプレートをすべての画像に一度に適用することができますchoose:

<xsl:apply-templates select="$image-entry/image"/>

ソースがない場合にキャプションを無視するには、キャプション ロジックをsource要素に一致する別のテンプレートに移動します。

<xsl:template match="source" mode="caption">
  <p class="full-width-caption">
      Image courtesy of: <a href="{.}"><xsl:value-of select="../title"/></a>
  </p>
</xsl:template>

次に、メイン テンプレートで次のようにします。

<xsl:apply-templates select="../source" mode="caption"/>

ソースがある場合はキャプションが作成され、ない場合は何も作成されません。

source質問に追加したばかりの例を考えると、要素が「存在しない」場合はキャプション no を除外したいようですが、値がない場合は除外します。apply-templates上記を次のように変更することでこれを行うことができます

<xsl:apply-templates select="../source[string()]" mode="caption" />

これは のキャプションを追加しますが、 のキャプションは追加し<source handle="">something</source>ません<source handle="" />

これが行っているのはフィルタリングであるため、述語が真../sourceの場合にのみ要素を選択します。[string()]このstring()関数は、コンテキスト要素 (この場合は the) の「文字列値」を返しますsource。boolean コンテキストの文字列は、空の場合は false として扱われ、それ以外の場合は true として扱われます。したがって、この場合の効果は、source空でない値を持つ場合にのみテンプレートを要素に適用することです。

于 2014-05-02T08:14:31.860 に答える