如何在XSLT中声明变量?(How to declare variables in 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"> <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 > 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 < 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>我收到一个错误:
XSLTProcessor::transformToXml(): Variable 'width' has not been declared. xmlXPathCompiledEval: evaluation failed Variable 'height' has not been declared. xmlXPathCompiledEval: evaluation failed这怎么可能?
我是否以错误的方式声明了变量的width和height ?
谢谢你的帮助。
When I run the following code...
<?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 > 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 < 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>I get an error:
XSLTProcessor::transformToXml(): Variable 'width' has not been declared. xmlXPathCompiledEval: evaluation failed Variable 'height' has not been declared. xmlXPathCompiledEval: evaluation failedHow is this possible?
Did I declare the variables width and height in a wrong way?
Thanks for any help.
最满意答案
我有时更喜欢使用模板规则:
<xsl:variable name="width"> <xsl:apply-templates select="datei/meta/@width" mode="width"/> </xsl:variable> <xsl:template match="@width[. > ../@height]" mode="width">600</xsl:template> <xsl:template match="@width[. < ../@height]" mode="width">600</xsl:template> <xsl:template match="@width" mode="width">600</xsl:template>等等
模板规则是XSLT中使用最少的部分。
I sometimes prefer to use template rules for this:
<xsl:variable name="width"> <xsl:apply-templates select="datei/meta/@width" mode="width"/> </xsl:variable> <xsl:template match="@width[. > ../@height]" mode="width">600</xsl:template> <xsl:template match="@width[. < ../@height]" mode="width">600</xsl:template> <xsl:template match="@width" mode="width">600</xsl:template>etc.
Template rules are the most underused part of XSLT.
更多推荐
发布评论