Skip to content

Instantly share code, notes, and snippets.

@tts
Last active December 13, 2015 21:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tts/4977107 to your computer and use it in GitHub Desktop.
Save tts/4977107 to your computer and use it in GitHub Desktop.
Transform HTML img element attributes
<!--
Transform the img element attributes in an HTML file.
Tuija Sonkkila 17.2.2013
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/ | @* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
<!--
Replace the blog site part of the URL to nothing, and append the file name with 'images/'.
The idea is to include all images locally to an EPUB bundle.
-->
<xsl:template match="img[starts-with(@src, 'https://blogs.aalto.fi/suoritin/files')]">
<xsl:variable name="f" select="replace(@src, 'https://blogs\.aalto\.fi/suoritin/files/\d{4}/\d{2}/(.*)', '$1')"/>
<xsl:copy>
<xsl:attribute name="src">
<xsl:value-of select="concat('images/', $f)"/>
</xsl:attribute>
<xsl:copy-of select="@* except @src"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment