Example 2

Transform all XML files in the directory hierarchy under doc_src using the stylesheet in the file style. Put the results in the directory hierarchy under doc.

JavaScript

// First, parse the stylesheet so that we don't have to do that for each // transformation dt = new DomParseXmlTF(). setSource(style). setNamespaceAware(true).run(); new RecursiveProcessTF(). addSource( new DirectoryAndFilter(doc_src, new EFileNameExtensionFilter("xml"))). setTarget(doc). setTargetStrategy( new ChangeExtensionTargetStrategy(). putExtensionTranslation("xml", "html")). setTaskFactory( new XsltTF(). // dt is a Producer that produces a Document. setStylesheet(dt)).run();

Example 3

Transform all XML files in the directory hierarchy under doc_src using the stylesheet in the file style. Put the results in the directory hierarchy under doc. Use a TaskExecutor for running the tasks.

JavaScript

// First, parse the stylesheet so that we don't have to do that for each // transformation dt = new DomParseXmlTF(). setSource(style). setNamespaceAware(true).run(); new RecursiveProcessTF(). addSource( new DirectoryAndFilter(doc_src, new EFileNameExtensionFilter("xml"))). setTarget(doc). setTargetStrategy( new ChangeExtensionTargetStrategy(). putExtensionTranslation("xml", "html")). setTaskFactory( new XsltTF(). // The DomParseXmlTask created above is a Producer for Document:s. setStylesheet(dt)).run();

Example 4

This example does pretty much the same as the two previous examples, but it uses the XmlCatalogResolver cr to resolve external entities in an XML catalog. In this example, the source document parsing and XSL transformation is split into two separate steps. That is sometimes necessary in order to persuade the various XML processors to resolve entities the way we want. An Iterator is used to iterate over all XML source documents manually. This is perhaps the easiest way to handle dependencies between different tasks in a loop.

JavaScript

// Parse the stylesheet so that we don't have to do that for each // transformation dt = new DomParseXmlTF(). setSource(style). setNamespaceAware(true).run(); itr = new FilteringIterator( Directories.getDepthLastIterator(doc_src), new EFileNameExtensionFilter("xml")); while (itr.hasNext()) { xmlFile = itr.next(); // Use the XmlCatalogResolver cr to resolve external entities when // parsing this document. parseTask = new DomParseXmlTF(). setSource(xmlFile). setNamespaceAware(true). setValidating(true). setEntityResolver(cr).run(); // This is where the resulting document will be put. Meditate a moment over // this... target = new FutureFile( doc, Entities.getRelativeLocation(xmlFile, doc_src). getParentLocation(). getChildLocation( EntityLocations.getBaseName( Entities.getName(xmlFile)) + ".html")); transformTask = new XsltTF(). // The DomParseXmlTask is a Producer for Document:s. setStylesheet(dt). setSource(parseTask). setTarget(target).run(); }

Example 5

Build a Docbook multi-page HTML manual from the XML file manual. Put the manual in doc. The docbook stylesheets are in /usr/share/xml/docbook. tmpDir is a directory for temporary files. te is a TaskExecutor

Note: The next example after this example is the recommended way of accomplishing the same thing. At the time of writing (2007.04), both Saxon and Xalan occasionally stumble on what looks as a threading issue in Xerces DOM classes when running several parallel XSLT tasks that use a DOMSource for the XSL template.

Note 2: The combination Xalan + Docbook XSL stylesheets is not thread safe. Be sure to never run several transformation tasks concurrently in parallel threads.

JavaScript

// Build a XML catalog with the Docbook stylesheets. Java has trouble handling // relative paths in XML documents, see for instance // http://www.sagehill.net/docbookxsl/WriteCatalog.html // The Docbook XSL distribution catalog is referenced in the variable dbRoot // A catalog resolver cr = new XmlCatalogResolver(); // A task for adding entities to the catalog. Entities are added with // URI:s relative to the /xhtml catalog in the Docbook distribution. This makes // it possible to use the XHTML stylesheets in /xhtml since they use relative // locations for files that they include. catalogBuilderTask = new RecursiveActionTF(). setSource(new DirectoryAndFilter(dbRoot, EFileFilter.FILTER)). setTaskFactory( new AddUriToCatalogTF(). setBaseLocation(new AbsoluteLocation("/xhtml")). setXmlCatalog(cr)).run(); // Parse the Docbook chunked HTML stylesheet docbookChunkedParseTask = new DomParseXmlTF(). setNamespaceAware(true). setEntityResolver(cr). setSource( Directories.getFile( dbRoot, new RelativeLocation("xhtml/chunk.xsl"))).run(); // Parse the source XML file sourceParseTask = new DomParseXmlTF(). setNamespaceAware(true). setEntityResolver(cr). setSource(manual).run(); // Run the XSL transformation. At the time of writing, Java's standard XSLT // classes cannot cope with the Docbook stylesheets, so we have to use Saxon 6 // or Xalan instead. // Set a temporary target file. This file will be created, but empty. The // Docbook chunked HTML stylesheets put the files under the catalog referenced // by the base.dir parameter instead. new XsltTF(). setTarget(new FutureFile(tmpDir, "empty.html")). setTransformerFactoryClassName( "org.apache.xalan.processor.TransformerFactoryImpl"). // setTransformerFactoryClassName( // "com.icl.saxon.TransformerFactoryImpl"). setParameter("html.stylesheet", "my_stylesheet.css"). setParameter("img.src.path", "images"). setParameter("use.role.for.mediaobject", "1"). // Have to add a slash to the end of the base.dir parameter, otherwise the // stylesheets interpret the directory name as a file prefix setParameter( "base.dir", ECFileResolvableUtil.getFileObject(doc).getAbsolutePath() + "/"). setUriResolver(cr). // The DomParseXmlTask is a Producer for Document:s. setStylesheet(docbookChunkedParseTask). setSource(sourceParseTask).run();

Example 6

This example does the same Docbook transformation as the previous example. The difference is that this example compiles the stylesheet using the TemplateCompilerTF task before doing the transformation, whereas the previous example only parsed it. This is the recommended way of running an XSL transformation.

Note: The combination Xalan + Docbook XSL stylesheets is not thread safe. Be sure to never run several transformation tasks concurrently in parallel threads.

JavaScript

// Build a XML catalog with the Docbook stylesheets. Java has trouble handling // relative paths in XML documents, see for instance // http://www.sagehill.net/docbookxsl/WriteCatalog.html // The Docbook XSL distribution catalog is referenced in the variable dbRoot // A catalog resolver cr = new XmlCatalogResolver(); // A task for adding entities to the catalog. Entities are added with // URI:s relative to the /xhtml catalog in the Docbook distribution. This makes // it possible to use the XHTML stylesheets in /xhtml since they use relative // locations for files that they include. catalogBuilderTask = new RecursiveActionTF(). setSource(new DirectoryAndFilter( dbRoot, EFileFilter.FILTER)). setTaskFactory( new AddUriToCatalogTF(). setBaseLocation(new AbsoluteLocation("/xhtml")). setXmlCatalog(cr)).run(); // Compile the Docbook chunked HTML stylesheet docbookChunkedCompileTask = new TemplateCompilerTF(). // setTransformerFactoryClassName( // "com.icl.saxon.TransformerFactoryImpl"). setTransformerFactoryClassName( "org.apache.xalan.processor.TransformerFactoryImpl"). setXslUriResolver(cr). setSource( Directories.getFile( dbRoot, new RelativeLocation("xhtml/chunk.xsl"))).run(); // Parse the source XML file sourceParseTask = new DomParseXmlTF(). setNamespaceAware(true). setEntityResolver(cr). setSource(manual).run(); // Run the XSL transformation. At the time of writing, Java's standard XSLT // classes cannot cope with the Docbook stylesheets, so we have to use Saxon 6 // or Xalan instead. // Set a temporary target file. This file will be created, but empty. The // Docbook chunked HTML stylesheets put the files under the catalog referenced // by the base.dir parameter instead. new XsltTF(). setTarget(new FutureFile(tmpDir, "empty.html")). setTransformerFactoryClassName( "org.apache.xalan.processor.TransformerFactoryImpl"). // setTransformerFactoryClassName( // "com.icl.saxon.TransformerFactoryImpl"). setParameter("html.stylesheet", "my_stylesheet.css"). setParameter("img.src.path", "images"). setParameter("use.role.for.mediaobject", "1"). // We have to add a slash to the end of the base.dir parameter, otherwise the // stylesheets interpret the directory name as a file prefix setParameter( "base.dir", ECFileResolvableUtil.getFileObject(doc).getAbsolutePath() + "/"). setUriResolver(cr). // The DomParseXmlTask is a Producer for Document:s. setTemplates(docbookChunkedCompileTask). setSource(sourceParseTask).run();

JRuby

# Build a XML catalog with the Docbook stylesheets. Java has trouble handling # relative paths in XML documents, see for instance # http://www.sagehill.net/docbookxsl/WriteCatalog.html # The Docbook XSL distribution catalog is referenced in the variable dbRoot # A catalog resolver cr = Schmant::XmlCatalogResolver.new # A task for adding entities to the catalog. Entities are added with # URI:s relative to the /xhtml catalog in the Docbook distribution. This makes # it possible to use the XHTML stylesheets in /xhtml since they use relative # locations for files that they include. catalogBuilderTask = Schmant::RecursiveActionTF.new. setSource(Schmant::DirectoryAndFilter.new( $dbRoot, Schmant::EFileFilter::FILTER)). setTaskFactory( Schmant::AddUriToCatalogTF.new. setBaseLocation(Schmant::AbsoluteLocation.new "/xhtml"). setXmlCatalog cr).run # Compile the Docbook chunked HTML stylesheet docbookChunkedCompileTask = Schmant::TemplateCompilerTF.new. # setTransformerFactoryClassName( # "com.icl.saxon.TransformerFactoryImpl"). setTransformerFactoryClassName( "org.apache.xalan.processor.TransformerFactoryImpl"). setXslUriResolver(cr). setSource( Schmant::Directories.getFile( $dbRoot, Schmant::RelativeLocation.new("xhtml/chunk.xsl"))).run # Parse the source XML file sourceParseTask = Schmant::DomParseXmlTF.new. setNamespaceAware(true). setEntityResolver(cr). setSource($manual).run # Run the XSL transformation. At the time of writing, Java's standard XSLT # classes cannot cope with the Docbook stylesheets, so we have to use Saxon 6 # or Xalan instead. # Set a temporary target file. This file will be created, but empty. The # Docbook chunked HTML stylesheets put the files under the catalog referenced # by the base.dir parameter instead. Schmant::XsltTF.new. setTarget(Schmant::FutureFile.new($tmpDir, "empty.html")). setTransformerFactoryClassName( "org.apache.xalan.processor.TransformerFactoryImpl"). # setTransformerFactoryClassName( # "com.icl.saxon.TransformerFactoryImpl"). setParameter("html.stylesheet", "my_stylesheet.css"). setParameter("img.src.path", "images"). setParameter("use.role.for.mediaobject", "1"). # We have to add a slash to the end of the base.dir parameter, otherwise the # stylesheets interpret the directory name as a file prefix setParameter( "base.dir", Schmant::ECFileResolvableUtil.getFileObject($doc).absolutePath + "/"). setUriResolver(cr). # The DomParseXmlTask is a Producer for Document:s. setTemplates(docbookChunkedCompileTask). setSource(sourceParseTask).run

Jython

# Build a XML catalog with the Docbook stylesheets. Java has trouble handling # relative paths in XML documents, see for instance # http://www.sagehill.net/docbookxsl/WriteCatalog.html # The Docbook XSL distribution catalog is referenced in the variable dbRoot # A catalog resolver cr = XmlCatalogResolver() # A task for adding entities to the catalog. Entities are added with # URI:s relative to the /xhtml catalog in the Docbook distribution. This makes # it possible to use the XHTML stylesheets in /xhtml since they use relative # locations for files that they include. catalogBuilderTask = RecursiveActionTF(). \ setSource(DirectoryAndFilter( dbRoot, \ EFileFilter.FILTER)). \ setTaskFactory( AddUriToCatalogTF(). \ setBaseLocation(AbsoluteLocation("/xhtml")). \ setXmlCatalog(cr)).run() # Compile the Docbook chunked HTML stylesheet docbookChunkedCompileTask = TemplateCompilerTF(). \ setTransformerFactoryClassName( "org.apache.xalan.processor.TransformerFactoryImpl"). \ setXslUriResolver(cr). \ setSource( Directories.getFile( dbRoot, \ RelativeLocation("xhtml/chunk.xsl"))).run() # Parse the source XML file sourceParseTask = DomParseXmlTF(). \ setNamespaceAware(True). \ setEntityResolver(cr). \ setSource(manual).run() # Run the XSL transformation. At the time of writing, Java's standard XSLT # classes cannot cope with the Docbook stylesheets, so we have to use Saxon 6 # or Xalan instead. # Set a temporary target file. This file will be created, but empty. The # Docbook chunked HTML stylesheets put the files under the catalog referenced # by the base.dir parameter instead. # # We have to add a slash to the end of the base.dir parameter, otherwise the # stylesheets interpret the directory name as a file prefix # # The DomParseXmlTask is a Producer for Document:s. XsltTF(). \ setTarget(FutureFile(tmpDir, "empty.html")). \ setTransformerFactoryClassName( "org.apache.xalan.processor.TransformerFactoryImpl"). \ setParameter("html.stylesheet", "my_stylesheet.css"). \ setParameter("img.src.path", "images"). \ setParameter("use.role.for.mediaobject", "1"). \ setParameter( "base.dir", ECFileResolvableUtil.getFileObject(doc).getAbsolutePath() + "/"). \ setUriResolver(cr). \ setTemplates(docbookChunkedCompileTask). \ setSource(sourceParseTask).run()


* An EntityFS-aware task is implemented using EntityFS. This means that it uses the filter settings of DirectoryView:s and also that it often can work with other file system implementations than File-based, such as the RAM file system.