【问题标题】:Is there any way to call XSLT templates inline有什么方法可以内联调用 XSLT 模板
【发布时间】:2009-11-24 20:27:52
【问题描述】:

如何内联调用 XSLT 模板?例如,而不是:

<xsl:call-template name="myTemplate" >
<xsl:with-param name="param1" select="'val'" />
</xsl:call-template>

我可以像这样使用 XSLT 内置函数调用样式吗:

<xls:value-of select="myTeplate(param1)" />

【问题讨论】:

    标签: design-patterns xslt anonymous-function processing-instruction


    【解决方案1】:

    在 XSLT 2.0 中,您可以使用 xsl:function 定义自己的自定义函数

    XML.com 上的一篇文章描述了如何在 XSLT 2.0 中编写自己的函数:http://www.xml.com/pub/a/2003/09/03/trxml.html

    <xsl:stylesheet version="2.0" 
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:foo="http://whatever">
    
      <!-- Compare two strings ignoring case, returning same
           values as compare(). -->
      <xsl:function name="foo:compareCI">
        <xsl:param name="string1"/>
        <xsl:param name="string2"/>
        <xsl:value-of select="compare(upper-case($string1),upper-case($string2))"/>
      </xsl:function>
    
      <xsl:template match="/">
    compareCI red,blue: <xsl:value-of select="foo:compareCI('red','blue')"/>
    compareCI red,red: <xsl:value-of select="foo:compareCI('red','red')"/>
    compareCI red,Red: <xsl:value-of select="foo:compareCI('red','Red')"/>
    compareCI red,Yellow: <xsl:value-of select="foo:compareCI('red','Yellow')"/>
      </xsl:template>
    
    </xsl:stylesheet>
    

    【讨论】:

    • 令人难以置信的是,这个答案已经 11 岁了,而 XSLT 2 在任何浏览器中仍然不是一件事。
    【解决方案2】:

    XSLT 的语法在第一个示例中是正确的。你也可以写

    <xsl:call-template name="myTemplate" >
    <xsl:with-param name="param1">val</xsl:with-param>
    </xsl:call-template>
    

    我不确定您在第二个代码 sn-p 中尝试做什么(缺少“val”,并且有两个拼写错误(xls 和 myTeplate)),但它不是有效的 XSLT.I n

    更新如果我现在理解您的问题,那不是“XSLT 模板是否有替代语法?”但是“我可以在 XSLT 中编写自己的函数吗?”。

    是的,你可以。这是一个有用的介绍。请注意,您必须在库中提供 Java 代码,这可能不容易分发(例如在浏览器中)。试试http://www.xml.com/pub/a/2003/09/03/trxml.html

    【讨论】:

    • 要调用 normalize-spaces 内置函数,我们将参数传递为 (param1, param2, paramn),有没有办法这样调用我的模板:
    • 没有。内置函数(例如 normalize-spaces)的语法与模板的语法不同。
    • 那么,有什么方法可以在 XSLT 中创建函数?
    • 不要把 XPath 函数误认为是 XSLT 函数; normalize-space() 是一个 XPath 函数。
    • 谢谢,抱歉,我是 XSLT 的新手 :) 谢谢。
    【解决方案3】:

    使用processing-instruction 和应用参数的匹配模板来执行此操作:

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

    参考文献

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-12
      • 2010-10-27
      • 1970-01-01
      • 1970-01-01
      • 2013-10-08
      • 2018-02-05
      相关资源
      最近更新 更多