views:

23

answers:

2

Hello, I found solution for subj related to the Visual Studio XSLT processor:

   public class XsltListFilesExtension
    {
        public XPathNodeIterator ListFiles(string directoryPath)
        {
            XmlDocument doc = new XmlDocument();
            doc.AppendChild(doc.CreateElement("files"));

            DirectoryInfo di = new DirectoryInfo(directoryPath);
            foreach (FileInfo fi in di.GetFiles())
            {
                XmlElement file = doc.CreateElement("file");
                file.SetAttribute("name", fi.Name);
                file.SetAttribute("size", fi.Length.ToString());
                doc.DocumentElement.AppendChild(file);
            }
            return doc.DocumentElement.CreateNavigator().SelectChildren("file", "");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            XslTransform t = new XslTransform();
            t.Load("transform.xslt");

            XsltArgumentList xsltArgs = new XsltArgumentList();
            xsltArgs.AddExtensionObject("urn:list-files-extension", new XsltListFilesExtension());

            XPathDocument input = new XPathDocument("input.xml");
            using (FileStream output = new FileStream("output.txt", FileMode.Create))
            {
                t.Transform(input, xsltArgs, output);
            }
        }
    }

The XSLT trasform.xslt:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
    xmlns:lfe="urn:list-files-extension">
    <xsl:output method="text" indent="yes"/>

    <xsl:template match="/">
        <xsl:for-each select="lfe:ListFiles('D:\temp')">
            <xsl:value-of select="@name"/>
            <xsl:text> - </xsl:text>
            <xsl:value-of select="@size"/>
            <xsl:text>bytes
</xsl:text>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

Can I to do solution like this but for common scope, for instance: the same xslt file can execute using java xslt processor, C# and etc. ? Thanks.

+1  A: 

The extension mechanism is general and part of the xslt specification.

You will need to find out with any xsl processor you are using what the exact mechanism is and how to import it into the xslt sheet, but in general the approach is similar.

This is possible with java, and the example you posted is already in C#. This should be possible with any xslt processor that supports xslt extensions.

Oded
ok, I understood, but can I to do it without extension only using common xslt features?
jitm
@jitm - xsl does not support file system access directly. The only way to get a list of files to xsl is to provide it as a parameter, or through an extension.
Oded
ok, Thanks ,I'll try to use extension solution.
jitm
A: 

No, I don't think so. lfe:ListFiles('D:\temp') is not a standard Xpath expression. It depends on the XSLT processor you are using. In Java you have to import the library for the processor which support this. You can define it as default library.

If you want to write standard xsl files, download Xalan or Saxon XSLT and peform your transform with help of them.

Govan
Yes, I understand that ListFiles is not standard expression, and I asked this question for find answer can I to do it using standard expression....
jitm