[vox-tech] XSLT questions

Micah J. Cowan vox-tech@lists.lugod.org
Tue, 16 Dec 2003 18:52:12 -0800


On Tue, Dec 16, 2003 at 01:46:50PM -0800, Jonathan McPherson wrote:
> Micah,
> 
> > That would not make valid XHTML (assuming you're generating XHTML
> > 1.). Your <xsl:stylesheet> opening tag should contain something like:
> >
> >   xmlns="http://www.w3.org/1999/xhtml"
> 
> It was, in fact, the XHTML validator that made me realize that I had a
> problem in the first place.  I had the xmlns string in the stylesheet
> earlier, but had removed it for some reason.  I put it back and, like
> magic, I have validating XHTML code.  Thanks!
> 
> > Some processors provide extensions to convert a result fragment into a
> > node set; but you should be able to do the same thing in another way
> > (XSLT is Turing complete, after all). You haven't really given much
> > information on what it is you want to achieve: can you give an example
> > of what you're trying to accomplish?
> 
> Well, it isn't a case in which I can't get the result that I want--it's a
> case of not being able to do things in the way I want to, so the fact
> that XHTML is Turing-complete does not guarantee anything.
> 
> Let's say, for instance, that have an XML tag for a project
> description...
> here's a very contrived example:
> 
> <project name="TTP">
> The TTP Project is the only project in our company with a fully
> recursive acronym.
> </project>
> 
> And I want to convert that to the following XHTML:
> 
> <hr />
> <img class="sep" src="pretty.png" alt="" />
> <hr />
> <p><strong>Project name: TTP</strong></p>
> <q>The TTP Project is the only project in our company with a fully
>    recursive acronym.</p>
> 
> This is fine, but suppose that I soon realize that the "sep" image
> surrounded by two <hr /> tags is something I use in many places in my
> stylesheet, and I want to make it a parameterized entity that I can use
> in many settings, like <sepimage src="pretty.png" />.  I don't know how
> to do this in XSLT; it seems that I am doomed to repeat the XHTML in each
> case, since <sepimage ... /> is not part of the source document.  Suppose
> I change my mind and want all these separator images (both those
> separating project descriptions and otherwise) to have one <hr /> rather
> than two--I'll have to go through my stylesheet and find each place where
> I used a separator image and fix it.
> 
> Basically, I want subroutines that I can use to abstract common but
> parametric elements of my stylesheets, and after going through a number
> of XSLT tutorials, I still can't find 'em.

The XSLT equivalent of called subroutines is named templates. You can
do something like (untested):

  <xsl:template name="sep-image">
    <xsl:param name="src"/>
    <hr />
    <img class="sep" src="{$src}" alt="" />
    <hr />
  </xsl:template>

And then you can invoke it with:

  <xsl:call-template name="sep-image">
    <xsl:with-param name="src" value="pretty.png"/>
  </xsl:call-template>

Hope that's what you were looking for. BTW, if you can avoid empty alt
attributes, you should probably parameterize that, too, if your source
XML format supports something that can be used for it. In my custom
DocBook stylesheets, I generate most <img/> elements from docbook
<graphic/> elements; and I use the xreflabel attribute's contents when
available; otherwise the contents of the <title/> sub-element to
<graphic/>.

-- 
Micah J. Cowan
micah@cowan.name