如何有条件地在xslt中导入样式表?

7
有没有办法在检查某些条件后导入样式表?
比如,如果变量 $a 的值为“1”,则导入 1.xsl,否则导入 2.xsl。
2个回答

17

大家好,有没有办法在检查一些条件后导入样式表呢?

比如说,如果变量$a的值为“1”,那么就导入1.xsl,否则导入2.xsl。

不行。<xsl:import>指令只能在编译时使用。

XSLT 2.0 可以使用use-when属性进行有限的条件编译。

例如:

<xsl:import href="module-A.xsl" 
     use-when="system-property('xsl:vendor')='vendor-A'"/>
use-when属性的限制在于,在评估该属性时没有动态上下文——特别是这意味着没有定义任何范围内变量。一个非XSLT的解决方案是在启动转换之前动态更改<xsl:import>声明的href属性:
  1. 将xsl样式表解析为XML文件。
  2. 评估确定应导入哪个样式表的条件。
  3. <xsl:import>声明的href属性的值设置为动态确定的要导入的样式表的URI。
  4. 使用刚刚修改的内存中的xsl样式表调用转换。

1
+1 有用的答案,这正是我一直在思考的问题!我相信其他人也会在某个时候感到好奇。 - Treemonkey
1
+1 好答案。期望 xsl:importxsl:include 与其他语言指令如 import#include 的工作方式不同是没有意义的。此外,由于 xsl:import/@href 是一个 URI,该资源也可以动态构建(几乎具有与 @use-when 相同的限制)。 - user357812

4

我知道这篇文章已经有些年头了,但我还是想分享一下我的看法。

每个显示屏只需要使用一个模板而不是两个。值的显示将通过VB应用程序进行更改。

breakfast_menu.xml

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="conditionDisplay.xsl" ?>
<data>
    <breakfast_menu>
        <food>
            <name>Belgian Waffles</name>
            <price>$5.95</price>
            <description>Two of our famous Belgian Waffles with plenty of real maple syrup</description>
            <calories>650</calories>
        </food>

        <food>
            <name>Strawberry Belgian Waffles</name>
            <price>$7.95</price>
            <description>Light Belgian waffles covered with strawberries and whipped cream</description>
            <calories>900</calories>
        </food>


        <food>
            <name>Homestyle Breakfast</name>
            <price>$6.95</price>
            <description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
            <calories>950</calories>
        </food>
    </breakfast_menu> 
    <display>1</display>
</data>

在这个文件中,我导入了我的显示器,并且使用条件语句告诉模板我需要什么。 conditionDisplay.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
    <xsl:import href="display1.xsl"/>
    <xsl:import href="display2.xsl"/>
    <xsl:template match="/">
        <xsl:variable name="display"><xsl:value-of select= "data/display"/></xsl:variable>
        <xsl:choose>
            <xsl:when test="$display='1'">
                <xsl:call-template name="display1" />
            </xsl:when>
            <xsl:otherwise>
                <xsl:call-template name="display2 />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

display1.xsl:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template name="display1">
        <html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
            <body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
                <xsl:for-each select="data/breakfast_menu/food">
                    <div style="background-color:teal;color:white;padding:4px">
                        <span style="font-weight:bold"><xsl:value-of select="name"/> - </span>
                        <xsl:value-of select="price"/>
                    </div>
                    <div style="margin-left:20px;margin-bottom:1em;font-size:10pt">
                        <p>
                            <xsl:value-of select="description"/>
                            <span style="font-style:italic"> (<xsl:value-of select="calories"/> calories per serving)</span>
                        </p>
                    </div>
                </xsl:for-each>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

display2.xsl:

<?xml version="1.0" encoding="UTF-8"?>futur
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template name="display2">
        <html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
            <body style="font-family:Arial;font-size:12pt;background-color:#222222">
                <xsl:for-each select="data/breakfast_menu/food">
                    <div style="background-color:teal;color:white;padding:4px">
                        <span style="font-weight:bold"><xsl:value-of select="name"/> - </span>
                        <xsl:value-of select="price"/>
                    </div>
                    <div style="margin-left:20px;margin-bottom:1em;font-size:10pt">
                        <p>
                            <xsl:value-of select="description"/>
                            <span style="font-style:italic"> (<xsl:value-of select="calories"/> calories per serving)</span>
                        </p>
                    </div>
                </xsl:for-each>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

我非常抱歉我的英语很糟糕。下一篇文章会更好,并且我希望能够帮助到某些人,因为我认为这不是最好的解决方案。


我曾经在模式方面做过类似的事情。不错的第一回答。+1 欢迎来到stackoverflow! - Daniel Haley

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接