如何在XSLT中声明变量?

如何在XSLT中声明变量?,xslt,Xslt,当我运行以下代码时 <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="images"> <ul id="tiles"> <xsl:for-each select="entry"> &l

当我运行以下代码时

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="images">
<ul id="tiles">
    <xsl:for-each select="entry">
        <li>
            <xsl:if test="position() mod 4 = 0">
                <xsl:attribute name="class">fourth</xsl:attribute>
            </xsl:if>

            <xsl:choose>
                <xsl:when test="datei/meta/@width &gt; datei/meta/@height">         
                    <xsl:variable name="width">600</xsl:variable>
                    <xsl:variable name="height">450</xsl:variable>                              
                </xsl:when>
                <xsl:when test="datei/meta/@width &lt; datei/meta/@height">                                 
                    <xsl:variable name="width">600</xsl:variable>
                    <xsl:variable name="height">800</xsl:variable>                      
                </xsl:when>
                <xsl:otherwise>
                    <xsl:variable name="width">600</xsl:variable>
                    <xsl:variable name="height">600</xsl:variable>  
                </xsl:otherwise>
            </xsl:choose>   

            <a href="{$root}/image/2/{$width}/{$height}/5{datei/@path}/{datei/filename}" class="fresco" data-fresco-caption="{titel}" data-fresco-group="event">
                <img src="{$root}/image/2/320/320/5{datei/@path}/{datei/filename}"/>
            </a>
        </li>
    </xsl:for-each>
</ul>
</xsl:template>

</xsl:stylesheet>
这怎么可能

我是否以错误的方式声明了变量宽度和高度


感谢您的帮助。

这是XSLT变量范围的问题。您已经在希望使用变量的范围之外声明了这些变量。重新编写变量声明,使xsl:choose语句位于声明中,而不是相反。

这是XSLT变量范围的问题。您已经在希望使用变量的范围之外声明了这些变量。重新编写变量声明,使xsl:choose语句位于声明中,而不是相反。

您可以声明变量高度,例如:

<xsl:variable name="height">
<xsl:choose>
    <xsl:when test="datei/meta/@width &gt; datei/meta/@height">         
         <xsl:value-of select="'450'"/>                           
    </xsl:when>
    <xsl:when test="datei/meta/@width &lt; datei/meta/@height">
         <xsl:value-of select="'800'"/>                         
    </xsl:when>
    <xsl:otherwise>
         <xsl:value-of select="'600'"/>   
    </xsl:otherwise>
 </xsl:choose>
 </xsl:variable>
由于宽度始终为600,因此无需将其声明为变量,但这可能仅适用于您提供的示例,在其他情况下,宽度可能会有所不同


在语句内声明的变量超出了此范围之外。由于类似问题中已经提供了很好的解释,正如一个参考答案:

您可以声明可变高度,例如:

<xsl:variable name="height">
<xsl:choose>
    <xsl:when test="datei/meta/@width &gt; datei/meta/@height">         
         <xsl:value-of select="'450'"/>                           
    </xsl:when>
    <xsl:when test="datei/meta/@width &lt; datei/meta/@height">
         <xsl:value-of select="'800'"/>                         
    </xsl:when>
    <xsl:otherwise>
         <xsl:value-of select="'600'"/>   
    </xsl:otherwise>
 </xsl:choose>
 </xsl:variable>
由于宽度始终为600,因此无需将其声明为变量,但这可能仅适用于您提供的示例,在其他情况下,宽度可能会有所不同


在语句内声明的变量超出了此范围之外。由于类似问题中已经提供了很好的解释,正如一个参考答案所示:

我有时更喜欢使用模板规则:

<xsl:variable name="width">
  <xsl:apply-templates select="datei/meta/@width" mode="width"/>
</xsl:variable>

<xsl:template match="@width[. &gt; ../@height]" mode="width">600</xsl:template>
<xsl:template match="@width[. &lt; ../@height]" mode="width">600</xsl:template>
<xsl:template match="@width" mode="width">600</xsl:template>
等等


模板规则是XSLT中使用最不充分的部分。

我有时更喜欢使用模板规则:

<xsl:variable name="width">
  <xsl:apply-templates select="datei/meta/@width" mode="width"/>
</xsl:variable>

<xsl:template match="@width[. &gt; ../@height]" mode="width">600</xsl:template>
<xsl:template match="@width[. &lt; ../@height]" mode="width">600</xsl:template>
<xsl:template match="@width" mode="width">600</xsl:template>
等等


模板规则是XSLT中使用最不充分的部分。

好的,谢谢。但这到底是什么样子呢?我对XSL还是新手,所以我不太清楚您的意思。好的,谢谢。但这到底是什么样子呢?我对XSL还是新手,所以我不太清楚你的意思。我刚刚意识到这是解决我问题的更好方法。我刚刚意识到这是解决我问题的更好方法。