Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Design patterns 有没有办法内联调用XSLT模板_Design Patterns_Xslt_Anonymous Function_Processing Instruction - Fatal编程技术网

Design patterns 有没有办法内联调用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的语法是正确的。你也可

如何内联调用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>

欢迎光临

多谢各位

参考资料


要调用normalize spaces内置函数,我们将参数传递为(param1,param2,paramn),是否有任何方法可以这样调用我的模板:否。内置函数(例如normalize spaces)的语法与模板的语法不同。因此,有任何方法可以在XSLT中创建函数?不要将XPath函数误认为是XSLT函数;normalize-space()是一个XPath函数。谢谢,很抱歉我犯了一个错误,因为我是XSLT的新手:)谢谢。令人难以置信的是,这个答案已经有11年历史了,XSLT 2在任何浏览器中都不是一个东西。
<?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>