3

XSLT テンプレートをインラインで呼び出す方法は? たとえば、代わりに:

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

次のように、XSLT 組み込みの関数呼び出しスタイルを使用できますか?

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

3 に答える 3

4

XSLT 2.0 では、xsl:functionを使用して独自のカスタム関数を定義できます。

XSLT 2.0 で独自の関数を作成する方法を説明している XML.com の記事: 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>
于 2009-11-25T01:07:55.250 に答える
1

XSLTの構文は、最初の例では正しいです。あなたも書くことができます

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

2番目のコードスニペットで何をしようとしているのかわかりません(「val」がなく、2つのタイプミス(xlsとmyTeplate)があります)が、有効なXSLTではありません。

更新あなたの質問を理解した場合、それは「XSLTテンプレートの代替構文はありますか?」ではありませんでした。しかし、「XSLTで独自の関数を記述できますか?」

はい、できます。ここに便利な紹介があります。ライブラリでJavaコードを提供する必要があり、これを配布するのは簡単ではない可能性があることに注意してください(ブラウザなど)。http://www.xml.com/pub/a/2003/09/03/trxml.htmlをお試しください

于 2009-11-24T20:40:28.847 に答える
1

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>

参考文献

于 2012-01-06T19:02:30.887 に答える