Search code examples
xmlxsltlibxslt

xslt transformation removes CDATA


Hi I'm using lib xsltproc to convert a set of old xml, xls files in a current project. When trying to transform my project with the tool I get the following error:

Warning: xmlXPathCompOpEval: function testFunc not found
XPath error : Unregistered function
runtime error: file test.xsl line 25 element value-of
XPath evaluation returned no result.

I have searched the files imports and found that there is an import containing this function in a CDATA tag. The imported file looks like so:

<!-- importMe.xsl -->

<?xml version="1.0"?>
<!DOCTYPE xsl:stylesheet >

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    xmlns:user="http://www.test.se/test">

<msxsl:script language="JavaScript" implements-prefix="user">
<![CDATA[
  function testFunc(str){
    return str;
  }
]]>
</msxsl:script>
</xsl:stylesheet>

The file that imports that file looks like so:

<!-- testImport.xsl -->

<?xml version="1.0"?>
<!DOCTYPE xsl:stylesheet [
  <!ENTITY nbsp "&#160;">
]>

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    xmlns:user="http://www.test.se/test">

<xsl:import href="importMe.xsl"/>
<xsl:output method="html" encoding="utf-8"/>
<msxsl:script language="JavaScript" implements-prefix="user">
  function someFunc(value) {
    return "";
  }
</msxsl:script>
<xsl:template match="/">
<xsl:value-of select="user:testFunc('')"/>
<HTML>
<HEAD>
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>

I did not include the xml file for brevity and because it does not seem to be part of the problem.

What seems to happen is that the CDATA does not is removed in the transformation. I have tried renaming the importMe.xsl file and I then get a different error message complaining about the missing file so I am fairly sure it is imported.

Is there any way to prevent the CDATA from disappearing?

I have tried adding the following row to both files in various combinations

<xsl:output method="html" encoding="utf-8" cdata-section-elements="msxsl:script"/>

Using both method="html" and "xml" and also different cdata-section-elements.


Solution

  • I'm using lib xsltproc

    But your stylesheet was obviously written for a Microsoft processor. msxsl:script is meaningless to the libxslt processor, and thus:

    <xsl:value-of select="user:testFunc('')"/>
    

    refers to an undefined function (as your error message clearly says).