[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Command Line


Subject: Re: [xsl] Command Line
From: Syd Bauman <Syd_Bauman@xxxxxxxxx>
Date: Mon, 18 Jan 2010 10:12:48 -0500

I realize this is not a fully-XSL reply, but if you're already on the
commandline, you might find it easier to use some tool other than
XSLT to generate a directory listing. 

When I want to operate on the list of files in a directory, I
typically start with `xml ls` (called `xmlstarlet ls` on some
distributions), and process the output thereof with my XSLT to mimic
file selection and get the output I want. 

I leave it to more experienced XSLTers to address the pros & cons of
an all-XSLT solution vs this sort of assembly line. I developed this
mechanism before using XSLT 2 was a reasonable option for me, and
am happy enough with it I haven't really looked at switching very
seriously (but could certainly be convinced).

Something like

--------- fns4sahoo.xslt ---------
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	>
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="/dir">
    <files>
      <xsl:apply-templates select="f[contains(@n,'.html')]"/>
    </files>
  </xsl:template>
  <xsl:template match="f">
    <filename>
      <xsl:value-of select="@n"/>
    </filename>
  </xsl:template>
</xsl:stylesheet>
--------- end fns4sahoo.xslt ---------

when used (along with the xmlstarlet and xsltproc commandline tools)
as follows:

  $ cd /tmp/
  $ xmlstarlet ls | xsltproc fns4sahoo.xslt - > fns4sahoo.xml

produces 
--------- fns4sahoo.xml ---------
<?xml version="1.0"?>
<files>
  <filename>ch.html</filename>
  <filename>oops.this.html.file.too.txt</filename>
  <filename>td.doc.html</filename>
  <filename>td1.doc.html</filename>
</files>
--------- end fns4sahoo.xml ---------

Two things worth noting:

* I stuck in a root <files> element for you, which was not mentioned
  in the original post

* My code erroneously picks up any file that has ".html" in the
  filename, not only files that end in ".html", because I was being
  lazy in the selection in my stylesheet (in the predicate in the
  <apply-templates>). Obviously this isn't very hard to fix.

Also, of course, David Lee's `xmlsh` environment probably does this
sort of stuff in its sleep.


Hope this is at least interesting, if not helpful :-)


Current Thread
Keywords