Tutorial :xsl:for-each file in directory(common solution)


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.


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.


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.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »