Using functions in XSLT

18.3k Views Asked by At

I'm learning XSLT. These questions may be obvious, but I'm really stuck now. Oxygen returns the following two kind of errors:

  1. Namespace is not declared for 'ownFunction()'. ("undeclared namespace prefix {xs}")

  2. unknown system function index-of-string()
    The XSLT function index-of-string I got from this website doesn't seems to be recognized

This is a simplified version of the XSL file:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"     xmlns:foo="http://www.wathever.com">
<xsl:output method="xml" />

  <xsl:function name="foo:ownFunction" as="xs:string">
    <xsl:param name="string" as="xs:string"/>

        <xsl:choose>

          <xsl:when test='contains($string,"src=")'>
            <xsl:variable name="position"><xsl:value-of select="index-of-string($string,'src=')"/>+<xsl:number value="10"/></xsl:variable>
            <xsl:variable name="partString"><xsl:value-of select="substring($string,$position)"/></xsl:variable>
            <xsl:variable name="length"><xsl:value-of select="index-of-string($partString,'quot;')"/> - <xsl:number value="2"/></xsl:variable>
            <xsl:value-of select="substring($partString,1,$length)"/>
          </xsl:when>

          <xsl:otherwise>
            <xsl:value-of select="hotpot-jmatch-file/data/title"/>
          </xsl:otherwise>

        </xsl:choose>
  </xsl:function>

  <xsl:template match="/">
    <data>
      <title>
        <xsl:variable name="string"><xsl:value-of select="hotpot-jmatch-file/data/title"/></xsl:variable>
        <xsl:value-of select="foo:ownFunction($string)"/>
      </title>
    </data>
  </xsl:template>
</xsl:stylesheet>
2

There are 2 best solutions below

0
On BEST ANSWER

Oxygen returns the following two kind of errors:

1) Namespace is not declared for 'ownFunction()'. ("undeclared namespace prefix {xs}")

This is actually an XML issue. Any XSLT stylesheet myst be a well-formed XML document. Among other requirements for well-formedness, any namespace prefix used must be bound to a namespace URI in a namespace declaration.

To correct this bind the "xs" prefix to "http://www.w3.org/2001/XMLSchema" -- this means to add xmlns:xs="http://www.w3.org/2001/XMLSchema" to an element (usually the top element is a good choice for this namespace.

You have the same problem with "foo:ownFunction". You must have the prefix "foo" bound/defined and visible, before using it. Just add xmlns:foo="my:foo" to the top element of your stylesheet.

2) "unknown system function index-of-string()". The XSLT function "index-of-string" I got from this website doesn't seems to be recognized: http://www.xsltfunctions.com/xsl/functx_index-of-string.html

You have forgotten to either copy and paste the function from Priscilla Walmsley's site or to save it in a separate file (recommended) and then use <xsl:import> or <xsl:include> to import/include this stylesheet file to your transformation.

Finally, such issues show that you need a more systematic introduction of XSLT. Get a good book and read it well. You won't be sorry. This answer may be useful in listing what I consider good XSLT and XPath learning resources.

1
On

Use

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"     xmlns:foo="http://www.wathever.com"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs functx""
xmlns:functx="http://www.functx.com">

<xsl:import href="location-of-functx-library.xsl"/>

...

<xsl:value-of select="functx:index-of-string($partString,'quot;')"/>

That samples defines the schema namespace and binds it to the prefix xs, defines the namespace of the function library you linked to. You will also need to download the function library implementation and import it as shown.