Re: [xsl] Checking external files based on the attribute value

Subject: Re: [xsl] Checking external files based on the attribute value
From: Peter Hickman <peter@xxxxxxxxxxxxx>
Date: Wed, 13 Aug 2008 14:37:56 +0100
This is a bit clunky but assuming your source data is in source.xml

<?xml version="1.0"?>
<doc>
 <graphic picfile="I970106058X_bulb"/>
 <graphic picfile="I970106058X_dummy"/>
</doc>

and your filelist is in filelist.xml

<?xml version="1.0"?>
<files>
 <file>./970106058X_c01_0002.tif</file>
 <file>./970106058X_c01_0001.tif</file>
 <file>./970106058X_bulb.tif</file>
 <file>./970106058X_0000.tif</file>
 <file>./970106058X_c01_0003.tif</file>
</files>

Then this seems to work.

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="1.0">
<xsl:output method="xml" indent="yes"/>


<xsl:variable name="files" select="document('filelist.xml')"/>

 <xsl:template match="/">
   <doc>
     <xsl:apply-templates/>
   </doc>
 </xsl:template>

<xsl:template match="graphic">
<xsl:variable name="file" select="substring-after(concat(@picfile,'.tif'),'I')"/>


   <node picfile="{@picfile}">
     <xsl:attribute name="matched">
       <xsl:choose>
         <xsl:when test="$files/files/file[contains(.,$file)]">
           <xsl:value-of select="true()"/>
         </xsl:when>
         <xsl:otherwise>
           <xsl:value-of select="false()"/>
         </xsl:otherwise>
       </xsl:choose>
     </xsl:attribute>
   </node>
 </xsl:template>
</xsl:stylesheet>

peter@radish:~/xxx$ xsltproc process.xsl source.xml
<?xml version="1.0"?>
<doc>
 <node picfile="I970106058X_bulb" matched="true"/>
 <node picfile="I970106058X_dummy" matched="false"/>
</doc>
peter@radish:~/xxx$

The $files/files/file[contains(.,$file)] does not seem to want to be converted into a boolean and I couldn't construct a way of getting it to match.

There has got to be a better way!

Current Thread