emacs lisp text processing example (html5 figure/figcaption)

S.Mandl StefanMandl at web.de
Tue Jul 5 23:30:13 CEST 2011


> haven't used XSLT, and don't know if there's one in emacs...
>
> it'd be nice if someone actually give a example...
>

Hi Xah, actually I have to correct myself. HTML is not XML. If it
were, you
could use a stylesheet like this:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="p[@class='cpt']">
  <figcaption>
    <xsl:value-of select="."/>
  </figcaption>
</xsl:template>

<xsl:template match="div[@class='img']">
  <figure>
    <xsl:apply-templates select="@*|node()"/>
  </figure>
</xsl:template>

<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>


</xsl:stylesheet>

which applied to this document:

<?xml version="1.0" encoding="ISO-8859-1"?>
<doc>
<h1>Just having fun</h1>with all the
<div class="img">
  <img src="cat1.jpg" alt="my cat" width="200" height="200"/>
  <img src="cat2.jpg" alt="my cat" width="200" height="200"/>
  <p class="cpt">my 2 cats</p>
</div>
cats here:
<h1>Just fooling around</h1>
<div class="img">
  <img src="jamie_cat.jpg" alt="jamie's cat" width="167" height="106"/
>
  <p class="cpt">jamie's cat! Her blog is <a href="http://example.com/
jamie/">http://example.com/jamie/</a></p>
</div>
</doc>

would yield:

<?xml version="1.0"?>
<doc>
<h1>Just having fun</h1>with all the
<figure class="img">
  <img src="cat1.jpg" alt="my cat" width="200" height="200"/>
  <img src="cat2.jpg" alt="my cat" width="200" height="200"/>
  <figcaption>my 2 cats</figcaption>
</figure>
cats here:
<h1>Just fooling around</h1>
<figure class="img">
  <img src="jamie_cat.jpg" alt="jamie's cat" width="167" height="106"/
>
  <figcaption>jamie's cat! Her blog is http://example.com/jamie/</figcaption>
</figure>
</doc>

But well, as you don't have XML as input ... there really was no point
to my remark.

Best,
Stefan



More information about the Python-list mailing list