Question

I have to transform some XMLs with XSLT. The XSLT commands look like this for example:

<Transformation>
  <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:user="urn:my-scripts">
   <msxsl:script language="C#" implements-prefix="user">
    <msxsl:using namespace="System.IO" />
    <![CDATA[
        #region Custom-Code
        public static string FileExists(string path)
        {
            FileInfo fi = new FileInfo(path);
            return (fi.Exists && (fi.Length >0)).ToString();
        }
        #endregion
    ]]>
   </msxsl:script>
   <xsl:output method="xml" indent="no" />
   <xsl:template match="@* | node()">
    <xsl:copy>
     <xsl:apply-templates select="@* | node()" />
    </xsl:copy>
   </xsl:template>
   <xsl:template match="Attachement">
    <xsl:choose>
     <xsl:when test="@mimetype='XPS'">
      <xsl:if test="(@type='MANUAL') and (@print='true') and (user:FileExists(File)='True')">
       <xsl:copy-of select="." />
      </xsl:if>
     </xsl:when>
     <xsl:otherwise>
      <xsl:copy-of select="." />
     </xsl:otherwise>
    </xsl:choose>
   </xsl:template>
  </xsl:stylesheet>
 </Transformation>

As you can see there is some C# code included.

Now my problem: I am writing a Java application. Of course the standard Java classes are not able to process the C# code.

I found this tool from Microsoft: http://www.microsoft.com/en-us/download/details.aspx?id=21714

I thougt this should work but you need MSXML 4.0 to run this utility. It is not a problem for myself but I'm developing this application for a company and it would be great if there is a tool which does not have any dependencies. Another problem is that I read somewhere, that this utility is not able to process this c# code too but I am not sure if they used version 1 or 2 of it.

Only XSLT 1.0 transformation are required, but if possible I would choose a tool that supports XSLT 2.0

Was it helpful?

Solution

If you use Java then there are XSLT 1.0 (Xalan, Saxon 6) and XSLT 2.0 (Saxon 9) processors that allow you to use the Java API, for instance to check whether a file exists. See http://saxon.sourceforge.net/saxon6.5.5/extensibility.html for instance for Saxon 6 or http://xalan.apache.org/xalan-j/extensions.html#java-namespace for Xalan or http://www.saxonica.com/documentation/index.html#!extensibility/functions for Saxon 9.

I don't see why MSXML would help, it is a COM software package done in C/C++ and does not support calling into C# nor into Java, it has an extension mechanism allowing to use JScript or VBScript.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top