Design patterns 有没有办法内联调用XSLT模板
如何内联调用XSLT模板?例如,而不是:Design patterns 有没有办法内联调用XSLT模板,design-patterns,xslt,anonymous-function,processing-instruction,Design Patterns,Xslt,Anonymous Function,Processing Instruction,如何内联调用XSLT模板?例如,而不是: <xsl:call-template name="myTemplate" > <xsl:with-param name="param1" select="'val'" /> </xsl:call-template> 我是否可以使用XSLT内置函数调用样式,如下所示: <xls:value-of select="myTeplate(param1)" /> 在第一个示例中,XSLT的语法是正确的。你也可
<xsl:call-template name="myTemplate" >
<xsl:with-param name="param1" select="'val'" />
</xsl:call-template>
我是否可以使用XSLT内置函数调用样式,如下所示:
<xls:value-of select="myTeplate(param1)" />
在第一个示例中,XSLT的语法是正确的。你也可以写
<xsl:call-template name="myTemplate" >
<xsl:with-param name="param1">val</xsl:with-param>
</xsl:call-template>
瓦尔
我不确定您在第二个代码段中试图做什么(缺少“val”并且有两个输入错误(xls和myTeplate)),但它不是有效的XSLT
更新如果我现在理解了你的问题,那不是“XSLT模板有其他语法吗?”而是“我可以用XSLT编写自己的函数吗?”
是的,你可以。这里是一个有用的介绍。请注意,您必须在库中提供Java代码,这可能不容易分发(例如在浏览器中)。在XSLT2.0中,您可以使用 XML.com上的一篇文章介绍了如何在XSLT 2.0中编写自己的函数:
对比红色、蓝色:
比较红色,红色:
比较红色,红色:
对比红色、黄色:
使用处理指令
和应用参数的匹配模板来执行此操作:
<?xml version="1.0" encoding="utf-8"?>
<!-- Self-referencing Stylesheet href -->
<?xml-stylesheet type="text/xsl" href="dyn_template_param.xml"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml"
>
<!--HTML5 doctype generator-->
<xsl:output method="xml" encoding="utf-8" version="" indent="yes" standalone="no" media-type="text/html" omit-xml-declaration="no" doctype-system="about:legacy-compat" />
<!--Macro references-->
<?foo param="hi"?>
<?foo param="bye"?>
<!--Self-referencing template call-->
<xsl:template match="xsl:stylesheet">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="/">
<!--HTML content-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
</head>
<body>
<!--Macro template calls-->
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="processing-instruction('foo')">
<xsl:param name="arg" select="substring-after(.,'=')"/>
<xsl:if test="$arg = 'hi'">
<p>Welcome</p>
</xsl:if>
<xsl:if test="$arg = 'bye'">
<p>Thank You</p>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
欢迎光临
多谢各位
参考资料
<?xml version="1.0" encoding="utf-8"?>
<!-- Self-referencing Stylesheet href -->
<?xml-stylesheet type="text/xsl" href="dyn_template_param.xml"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml"
>
<!--HTML5 doctype generator-->
<xsl:output method="xml" encoding="utf-8" version="" indent="yes" standalone="no" media-type="text/html" omit-xml-declaration="no" doctype-system="about:legacy-compat" />
<!--Macro references-->
<?foo param="hi"?>
<?foo param="bye"?>
<!--Self-referencing template call-->
<xsl:template match="xsl:stylesheet">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="/">
<!--HTML content-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
</head>
<body>
<!--Macro template calls-->
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="processing-instruction('foo')">
<xsl:param name="arg" select="substring-after(.,'=')"/>
<xsl:if test="$arg = 'hi'">
<p>Welcome</p>
</xsl:if>
<xsl:if test="$arg = 'bye'">
<p>Thank You</p>
</xsl:if>
</xsl:template>
</xsl:stylesheet>