Another applet, this one dedicated to my beloved XSLT. It allows you to quickly test your XSLT stylesheet against a XML document.
Run the XSLT Tester Applet now!
Behind the scenes
Most of the code for this application is Swing-related stuff. The only two interesting pieces of code are those that deal with prettifying and transforming the XML document.
Prettifying
This method formats the XML so that it is easier to read, using the dom4j library:
public static String prettify(final String xml)
{
StringWriter output = new StringWriter();
try
{
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter(output, format);
Document document = DocumentHelper.parseText(xml);
writer.write(document);
writer.flush();
}
catch (Exception e)
{
return xml;
}
return output.toString();
}
Transforming
This method applies a XSLT stylesheet to a XML document, using the javax.xml.transform.Transformer class:
public static String transform(final Source xml, final Source xslt,
final Map params) throws TransformerException
{
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transform = factory.newTransformer(xslt);
// optional parameters for the XSLT
if (params != null)
{
for (Iterator iter = params.entrySet().iterator(); iter.hasNext();)
{
Entry param = (Entry) iter.next();
transform.setParameter((String) param.getKey(), param.getValue());
}
}
StreamResult result = new StreamResult(new StringWriter());
transform.transform(xml, result);
return result.getWriter().toString();
}
Check out the source code for the XSLT Tester Applet on GitHub!
Comments