title: Using Saxon and XSLT to transform JDOM XML documents
body: <p>I'm trying to convert some XML so that iso8879 entity strings will appear in place of characters. For example the string <code>1234-5678</code> would become <code>1234&amp;hyphen;5678</code>. I've done this using character maps and the stylesheets found at <a href="http://www.w3.org/2003/entities/iso8879doc/overview.html" rel="nofollow">http://www.w3.org/2003/entities/iso8879doc/overview.html</a>. </p>
  
  <p>The first part of my xslt looks like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
      &lt;xsl:import href="iso8879map.xsl"/&gt;  
      &lt;xsl:output omit-xml-declaration = "yes" use-character-maps="iso8879"/&gt;
  </code></pre>
  
  <p>When I run this stylesheet in Eclipse with the Saxon XSLT engine it works fine and outputs an XML file with the hyphen entitiy string in place of the hyphen character. However, I need to automate this process so am using the JDOM package. Unfortunately, the characters are not being replaced during the transformation. The code that does the conversion looks a little like this:</p>
  
  <pre><code>System.setProperty("javax.xml.transform.TransformerFactory",
      "net.sf.saxon.TransformerFactoryImpl");  // use saxon for xslt 2.0 support
  
  
  SAXBuilder builder = new SAXBuilder();
  builder.setExpandEntities(false);       
  XSLTransformer transformer = new XSLTransformer(styleSheet);
  
  Document toTransform = builder.build(Fileref); // transform
  Document transformed = transformer.transform(toTransform);
  </code></pre>
  
  <p>I then write the document out to a file using the following method:</p>
  
  <pre><code>public static void writeXMLDoc(File xmlDoc, Document jdomDoc){
  
      try {
          Format format = Format.getPrettyFormat();
          format.setOmitDeclaration(true);
          format.setEncoding("ISO-8859-1");
          XMLOutputter outputter = new XMLOutputter(format);
          //outputter.output((org.jdom.Document) allChapters, System.out);
          FileWriter writer = new FileWriter(xmlDoc.getAbsolutePath());
          outputter.output((org.jdom.Document) jdomDoc, writer);
          writer.close();
      } 
      catch (java.io.IOException exp) {
          exp.printStackTrace();
      }
  }
  </code></pre>
  
  <p>I've started debugging in Eclipse and it looks like the hyphen character isn't being replaced during the xslt transformation. I've tested this using the Saxon xslt engine on it's own and it does work, so it's likely something to do with using it from Java and Jdom. Can anybody help?</p>
  
  <p>Many thanks.</p>
  
  <p>Jim</p>