Is there any way to call XSLT templates inline

こ雲淡風輕ζ 提交于 2019-12-01 05:36:03

问题


How to call XSLT templates inline? For instance, instead of :

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

Can I use XSLT built-in function-call style, like this:

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

回答1:


In XSLT 2.0 you can define your own custom functions using xsl:function

An article on XML.com describing how to write your own functions in 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>



回答2:


The syntax of XSLT is correct in the first example. You could also write

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

I am not sure what you are trying to do in the second code snippet (the 'val' is missing and there are two typos (xls, and myTeplate)) but it is not valid XSLT.I n

UPDATE If I now understand your question it was not "is there an alternative syntax for XSLT templates?" but "can I write my own functions in XSLT?".

Yes, you can. Here is a useful introduction. Note that you have to provide your Java code in a library and this may not be easy to distribute (e.g. in a browser). Try http://www.xml.com/pub/a/2003/09/03/trxml.html




回答3:


Use a processing-instruction and a matching template that applies the parameters to do this:

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

References

  • Is there a way to get processing instructions passed through from the xml source into html (using xsl)?

  • XSLT Variable Design

  • Looking for a (pseudo) XSLT preprocessor/templater to make it less verbose



来源:https://stackoverflow.com/questions/1792707/is-there-any-way-to-call-xslt-templates-inline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!