checking a files existence

Questions about XML that are not covered by the other forums should go here.
melvis
Posts: 2
Joined: Thu Jan 08, 2009 2:17 am

checking a files existence

Post by melvis »

Complete noob with oXygen and not very familiar with XML to boot so bear with me. An application (that I cannot control) performs a data import using XML and external graphic files. The app goes line-by-line through the XML, finds the files then imports them from their stated location (typically a relative path bundled within a zip). The problem comes when a file is missing. At this point the application throws an exception and stops.

I'd like to get a report of all the missing files so I can make placeholders and continue with the import. An example of a media reference is below:

Code: Select all

<media display="visible">
<label><![CDATA[New: Figure]]></label>
<description/>
<comments/>
<keywords/>
<mediafiles>
<mediafile>
<filename><![CDATA[./media/PD/bigpicture.png]]></filename>
<dpi/>
<width>200</width>
<mediatype>web</mediatype>
</mediafile>
</mediafiles>
<alttext/>
</media>
I'm not a programmer but have (years ago) used js and vbscript in the past to do projects like this. Am I right in assuming that oXygen can perform the task of creating the offending file report? Either way is better than looking through about 1 million lines and checking manually :( Any help is appreciated.
Dan
Posts: 501
Joined: Mon Feb 03, 2003 10:56 am

Re: checking a files existence

Post by Dan »

After unzipping the content into a folder, you can apply the following XSL file over the XML document. It will generate a report with all the files that do not exist. Make sure you edit the first parameter from the "fpath" to match your folder.

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:file="java:java.io.File">
<xsl:output indent="yes"/>
<xsl:template match="filename">

<xsl:variable name="fname" select="string(.)"/>
<!-- Change the first argument the your folder.-->
<xsl:variable name="fpath" select="concat('/Users/dan/Desktop/',$fname)"/>
<xsl:variable name="exists" select="boolean(file:exists(file:new($fpath)))"/>

<xsl:if test="not($exists)">
<not-existing>
<xsl:value-of select="$fname"/>
</not-existing>
</xsl:if>
</xsl:template>

<xsl:template match="/">
<report>
<xsl:apply-templates select="//filename"/>
</report>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
All the best,
Dan
melvis
Posts: 2
Joined: Thu Jan 08, 2009 2:17 am

Re: checking a files existence

Post by melvis »

I'll try it. Cheeers!
Post Reply