Software is like sex... It's better when it's free.

Blog - Test the existence of a file in XSL

Test the existence of a file in XSL

When you want to display images dynamically, if you want to make sure they actually exist, you can do fairly easily directly in XSL:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:php="http://php.net/xsl" version="1.0">
<xsl:output method="xml" encoding="UTF-8" indent="yes" />
  <xsl:template match="root">
    <html>
      <head></head>
      <body>
        <!-- We suppose node 'id' contains part of the filename -->
        <xsl:variable name="imgSrc" select="boolean(document(string(concat('http://www.sillysmart.org/img/users/', id, '.jpg'))))" />
        <xsl:choose>
          <xsl:when test="$imgSrc">
            <img src="{concat('http://www.sillysmart.org/img/users/', id, '.jpg')}" alt="{id}" />
          </xsl:when>
          <xsl:otherwise>
            <img src="http://www.sillysmart.org/img/404.jpg" alt="Not found" />
          </xsl:otherwise>
        </xsl:choose>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>
Tags:  xmlxsl.
Posted the Tuesday 08 september 2009 18:03:04