如何在XSLT中声明变量?(How to declare variables in XSLT?)

系统教程 行业动态 更新时间:2024-06-14 16:59:47
如何在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 &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>

我收到一个错误:

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 &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>

I get an error:

XSLTProcessor::transformToXml(): Variable 'width' has not been declared. xmlXPathCompiledEval: evaluation failed Variable 'height' has not been declared. xmlXPathCompiledEval: evaluation failed

How 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[. &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中使用最少的部分。

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[. &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>

etc.

Template rules are the most underused part of XSLT.

更多推荐

本文发布于:2023-04-17 09:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/a1efd29eb060523a8cb589dc3180d757.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   声明   如何在   variables   XSLT

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!