Where can I put an XSL function in an XSL document?

I have an XSL stylesheet for which I need to add some custom string manipulations using the xsl: function. But I am having trouble trying to determine where to place this function in my document.

My simplified XSL looks like this:

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

And I want to put a simple function, let's say

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

But I can’t decide where to put this function, when I put it under the node, I get the error message: "xsl: function" cannot be a child of the "xsl: stylesheet". Element, and if I put it under the node, I get a similar error.

Where should I put the function? Idealy I would like to put my functions in an external file and import them into my xsl files.

+5
2

XSL 1.0 xsl: function.

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

(...)

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

2.0 stylesheet

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

All Articles