This is the mail archive of the docbook-apps@lists.oasis-open.org mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

XSLT for text files ?(Re: XSL from DocBook refentry ->man)


Rafael,

> You can't use XSL stylesheets to generate man pages, not without a lot of
> klugery anyhow.  XSLT is designed to transform XML documents into other
> XML documents, and any attempt to make XSLT generate non-XML output is
> extremely troublesome, and in some cases XSLT will not allow you to make
> certain output constructions that you need to do because they violate the
> rules of XML.

I've been learning a whole lot about XSLT lately, so I couldn't let this one 
pass by without comment.  Actually, going from XML to XML is *one* of the
things that XSLT can do, but it is designed for much more.  In fact, the
<xsl:output> element has three defined methods:

  <xsl:output method="xml"/>
  <xsl:output method="html"/>
  <xsl:output method="text"/>

To learn more, I wrote a rather lame stylesheet that would take an
XML file and write it out to a comma-separated-value file. For those
interested, the rest of this message gives that example.  For those
who are not, now would be a good time to stop reading this message. :-)

Okay, the goal was to take an XML file with a list of books and output
a CSV file with the title, author and publisher.

So I grabbed three books off my shelf and made a file called 'test.xml'
that just uses some XML elements I made up (no relation to any DTD I
know of) and looks like this:

    <?xml version="1.0"?>
    <booklist>
      <book>
        <title>Vi IMproved-Vim</title>
        <author>Steve Oualline</author>
        <publisher>New Riders</publisher>
      </book>
      <book>
        <title>XSLT Programmer's Reference - 2nd Edition</title>
        <author>Michael Kay</author>
        <publisher>Wrox Press</publisher>
      </book>
      <book>
        <title>DocBook: The Definitive Guide</title>
        <author>Norman Walsh &amp; Leonard Muellner</author>
        <publisher>O'Reilly &amp; Associates</publisher>
      </book>
    </booklist>

Nothing overly fancy.  Just something simple.  Now here's a basic
XSLT stylesheet (csv.xsl) that processes this:

    <xsl:stylesheet
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
      version = "1.0">
    
    <xsl:output method="text"/>
    
    <xsl:template match="booklist">
    <xsl:apply-templates select="book"/>
    </xsl:template> 
    
    <xsl:template match="book">
    <xsl:value-of select="title"/>,<xsl:value-of select="author"/>,<xsl:value-of select="publisher"/><xsl:text>
    </xsl:text>
    </xsl:template>
    
    </xsl:stylesheet>

Now there certainly might be better ways of doing this with XSLT, but
this was what I came up with.  The <xsl:text> element in the "book" template
was the only way I could figure out to get the end-of-line in there. I had
to put the <xsl:output method="text"/> in there or I got the 
'<?xml version="1.0"?>' header on top of the file.

In any event, processing it with 'xsltproc' (from the 'libxslt' package
at http://www.xmlsoft.org/XSLT/ ) gets me the following:

    $ xsltproc csv.xsl test.xml 
    Vi IMproved-Vim,Steve Oualline,New Riders
    XSLT Programmer's Reference - 2nd Edition,Michael Kay,Wrox Press
    DocBook: The Definitive Guide,Norman Walsh & Leonard Muellner,O'Reilly &
    Associates
    $

(to dump it to a file, I did 'xsltproc -o test.csv csv.xsl test.xml'. The
'-o' is for the output file.)

So there we are... a CSV file from a XML file by way of an XSLT stylesheet.
Not really overly useful, perhaps, but it was an interesting learning
experiment I did.

I personally have never written a man page, so I don't know what's exactly
involved... but this is the (very basic) way you could get a stylesheet going.

Regards,
Dan

-- 
Dan York, Director of Training        dyork@e-smith.com
Ph: +1-613-751-4401  Mobile: +1-613-263-4312 Fax: +1-613-564-7739 
e-smith, inc. 150 Metcalfe St., Suite 1500, Ottawa,ON K2P 1P1 Canada
http://www.e-smith.com/            open source, open mind


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]