0

我有一个模板,它获取图像,然后将它们输出到两个模板之一。

我希望它根据每个图像的值跟踪每个单独的图像和输出。目前,如果一张图像是宽的,它会根据宽模板将它们全部输出。我宁愿同时使用这两个模板。

<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

而不是两种模式,而是使用一种模式并将宽与非宽逻辑移动到模板匹配表达式中:

<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 而如果它没有 value。您可以通过将上述更改apply-templates

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

这将添加一个标题为<source handle="">something</source>但不为<source handle="" />

这样做是过滤,所以我们只在谓词为真时才选择../source元素。[string()]string()函数返回上下文元素(在本例中为 the)的“字符串值”,source如果布尔上下文中的字符串为空,则将其视为 false,否则将其视为 true。source所以在这种情况下,效果是仅当元素具有非空值时才将模板应用于元素。

于 2014-05-02T08:14:31.860 回答