Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

xslt

xsl-fo

I have an XSL style sheet for which I need to add some custom string manipulation using an xsl:function. But I am having trouble trying to work out where to put the function in my document.

My XSL simplified 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 in a simple function, say,

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

But I cannot work out where to put the function, when I put it under the node I get an error saying '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.

like image 866
mattdlong Avatar asked Sep 03 '09 06:09

mattdlong


2 Answers

There is no xsl:function in XSL version 1.0. You have to create a named template

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

(...)

<xsl:template match="something">
  <xsl:call-template name="helloWorld"/>
</xsl:template>
like image 121
Pierre Avatar answered Oct 12 '22 23:10

Pierre


You can upgrade the stylesheet version to 2.0 Then in stylesheet declaration specify as

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

** Your choice you can specify anything as your wish then below this specify your function

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

Then in template you can use it as

<xsl:template match="/">
<xsl:value-of select="func:helloWorld"/>
</xsl:template>
like image 28
Nandha Kumar Avatar answered Oct 13 '22 01:10

Nandha Kumar