XML & Web services: Formatting Text Output with XML
I'm curious as to whether anyone has used XSL to format a plaintext output. We started using XSL because it appeared to be a versatile technology, but we are quickly realizing it is only good for our HTML output. We need HTML, plaintext, and PDF. PDF generation via XSL is much too slow. And I'm not sure if it would work for plaintext. What we need is formatting (for e-mail bodies) which may include rows and columns that need to be aligned properly. There is no right or left padding in XSL.
We've already compromised our framework and taken the PDF-generation away from the XSL transformation. Must we do it again for plaintext? The value that XSL brings to the table is seeming less and less...
- Formatting Text Output with XML by Raj Rajen on June 13 2002 12:51 EDT
- Formatting Text Output with XML by Kenny MacLeod on June 15 2002 17:25 EDT
- Formatting Text Output with XML by Tracy Hartley on June 17 2002 05:33 EDT
Jakarta Velocity might be of help to you as far formatting text output goes.
XSL has little value unless you're doing XML-to-XML transformation, IMO.
Like the previous poster, I'd suggest a template engine. I'd suggest Freemarker rather than Velocity, however. Velocity's one of the rare examples of the jakarta project having a decidedly inferior product.
Have you tried Apache FOP for the PDF? I am using this to produce PDF reports from database data. It has several other outputs included text.
I believe I've found a formula that works (at least with the JDK 1.4 included XSL stuff).
<?xml version="1.0" ?>
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:variable name="pad" select="' '" />
<xsl:variable name="pad-length" select="string-length($pad)" />
<xsl:value-of select="substring($pad,1,$pad-length - string-length(//node1))"/><xsl:value-of select="//node1" />
<xsl:value-of select="substring($pad,1,$pad-length - string-length(//node2))"/><xsl:value-of select="//node2" />
<xsl:value-of select="substring($pad,1,$pad-length - string-length(//node3))"/><xsl:value-of select="//node3" />
My output was (with 20 space pad):
Node One Node Two Node Three
My above message has some truncated whitespace. If you want, post a message w/ your email and I'll send you the actual files.