【发布时间】:2010-12-17 15:30:19
【问题描述】:
有什么方法可以在检查一些条件后导入样式表?
例如,如果变量 $a="1" 的值则导入 1.xsl,否则导入 2.xsl。
【问题讨论】:
有什么方法可以在检查一些条件后导入样式表?
例如,如果变量 $a="1" 的值则导入 1.xsl,否则导入 2.xsl。
【问题讨论】:
大家好,请问有什么方法可以导入 检查一些后的样式表 条件?
例如,如果变量 $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 属性:
将 xsl 样式表解析为 XML 文件
评估确定应导入哪个样式表的条件。
将<xsl:import>声明的href属性的值设置为动态确定的待导入样式表的URI。
使用刚刚修改的内存中 xsl 样式表调用转换。
【讨论】:
xsl:import 或xsl:include 与import 或#include 等其他语言指令的工作方式不同是没有意义的。另外,因为xsl:import/@href 是一个URI,所以该资源也可以动态构建(几乎与@use-when 相同的限制)
我知道这篇文章已经过时了,但我想分享一下我的看法。
每个显示可以使用一个模板而不是两个。值显示将随 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>
我真的为我糟糕的英语道歉。下一篇文章会更好,我希望对某人有所帮助,因为我认为这不是最好的解决方案。
【讨论】: