1

I am trying to implement a .NET xslt extension object the checks for the existence of an xml file following examples from here and there. I also pondered some related SO posts which weren't much help.

When I try to find the extension with the following code snippet, it displays "Extension function is not available":

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:XsltExtensionObject="urn:XsltExtensionObject"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
exclude-result-prefixes="msxsl XsltExtensionObject">

...

<xsl:choose>
     <xsl:when test="function-available('XsltExtensionObject:getFile')">
          Extension function is available
     </xsl:when>
     <xsl:otherwise>
          Extension function  is not available
     </xsl:otherwise>
</xsl:choose>

...

I created a separate class library called XsltExtensionObject within the same VS 2012 solution as follows (did t in VB to be consistent with the first example):

Public Class FileExist
    Private exist As Boolean
    Public Sub New()
        exist = False
    End Sub
    Public Function getFile(ByVal myFile As String) As Boolean
        exist = System.IO.File.Exists(myFile)
        Return exist
    End Function
End Class

I execute the xsl transform as follows:

    protected void Page_Load(object sender, EventArgs e)
    {
        XmlDocument xmldoc = new XmlDocument();
        XslCompiledTransform xslt = new XslCompiledTransform();
        xslt.Load(MapPath("homepage.xslt"));
        XsltArgumentList xslArgs = new XsltArgumentList();
        XsltExtensionObject.FileExist obj = new XsltExtensionObject.FileExist();
        xslArgs.AddExtensionObject("urn:XsltExtensionObject", obj);
        xmldoc.Load(MapPath("lists.xml"));
        StringWriter sw = new StringWriter();
        xslt.Transform(new XmlNodeReader(xmldoc), null, sw);
        content.InnerHtml = sw.ToString();
    }

Why can't it find the extension? What am I missing here? An assembly reference?

ron tornambe
  • 10,452
  • 7
  • 33
  • 60

1 Answers1

0

There are two points I would like to present to others baffled by Xslt Object Extensions. Firstly, when I opened the xml document using XPathDocument not XmlDocument as shown in the this example the extension function was found.

Secondly, the class containing the extension (FileExist above) does NOT need to be created as a class library. Here's the C# code that worked:

    protected void Page_Load(object sender, EventArgs e)
    {
        XPathDocument xmldoc = new XPathDocument(MapPath("lists.xml"));
        XslCompiledTransform xslt = new XslCompiledTransform();
        xslt.Load(MapPath("homepage.xslt"));
        XsltArgumentList xslArgs = new XsltArgumentList();
        FileExist obj = new FileExist();
        xslArgs.AddExtensionObject("urn:XsltExtensionObject", obj);
        XmlWriterSettings settings = new XmlWriterSettings();
        settings.OmitXmlDeclaration = true;
        settings.ConformanceLevel = ConformanceLevel.Fragment;
        settings.CloseOutput = false;
        using (var sw = new StringWriter())
        {
            using (var xw = XmlWriter.Create(sw, settings))
            {
                xslt.Transform(xmldoc, xslArgs, xw, null);
            }
            content.InnerHtml = sw.ToString();
        }            
    }
    public class FileExist
    {
        bool exist = false;
        public bool getFile(string myFile)
        {
            exist = File.Exists(HttpContext.Current.Server.MapPath(myFile));
            return exist;
        }
    }
ron tornambe
  • 10,452
  • 7
  • 33
  • 60