2009-09-03 99 views
5

我有一个XSL样式表,我需要使用xsl:function来添加一些自定义字符串操作。但是我在尝试着解决将函数放在文档中的位置时遇到了困难。我在哪里可以将XSL函数放在XSL文档中?

我的XSL简化这个样子的,

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:my="myFunctions" xmlns:d7p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:import href="Master.xslt"/> 
    <xsl:template match="/"> 
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> 
     <!-- starts actual layout --> 
     <fo:page-sequence master-reference="first"> 
     <fo:flow flow-name="xsl-region-body"> 
      <!-- this defines a title level 1--> 
      <fo:block xsl:use-attribute-sets="heading"> 
      HelloWorld 
      </fo:block> 
     </fo:flow> 
     </fo:page-sequence> 
    </fo:root> 
    </xsl:template> 
</xsl:stylesheet> 

我想提出一个简单的功能,比方说,

<xsl:function name="my:helloWorld"> 
    <xsl:text>Hello World!</xsl:text> 
    </xsl:function> 

但我不能工作了哪里放的功能,当我将它放在节点下面我得到一个错误,说'xsl:function'不能是'xsl:stylesheet'元素的子元素。,如果我把它放在节点下面,我会得到一个类似的错误。

我应该在哪里放置这个功能? Idealy我想将我的功能放在一个外部文件中,并将它们导入到我的xsl文件中。

回答

18

XSL版本1.0中没有xsl:函数。你必须创建一个命名模板

<xsl:template name="helloWorld"> 
    <xsl:text>Hello World!</xsl:text> 
</xsl:template> 

(...) 

<xsl:template match="something"> 
    <xsl:call-template name="helloWorld"/> 
</xsl:template> 
+0

谢谢Peirre!那就是诀窍。 – mattdlong 2009-09-03 07:15:09

7

您可以将样式表版本升级到2.0 然后在样式声明中指定为

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:func="http://www.**.com"> 

**你选择,你可以根据你的愿望 低于任何指定然后这说明你的函数

<xsl:function name="func:helloWorld"> 
    <xsl:text>Hello World!</xsl:text> 
</xsl:function> 

然后在模板中,你可以使用它作为

<xsl:template match="/"> 
<xsl:value-of select="func:helloWorld"/> 
</xsl:template> 
+1

做这里写的东西,我得到一个“命名空间”http://www.**.com'不包含任何功能。'错误。可能是什么问题? – ALOToverflow 2013-04-19 15:45:55

+1

这是因为MSXML不支持XSLT 2。 – 2014-09-02 19:35:45

相关问题