[xsl] template processing with document() selected nodes

Subject: [xsl] template processing with document() selected nodes
From: Richard Lewis <richardlewis@xxxxxxxxxxxxxx>
Date: Tue, 17 Oct 2006 17:01:41 +0100
Hello XSLers,

I've just encountered the following problem:

I have two source XML files, one is my main content file and the other is 
a 'map' file which dictates how the structural elements of the main content 
file should map to the new content file. I have an XSLT stylesheet which 
performs the transformation.

The main content file is the content for my website and it has to be mapped to 
my employer's new content management system which uses "department" to mean 
directory and "article" to mean (roughly) file.

I can't do a straightforward XSLT transformation as the hierarchies of my 
original content and the new CMS are different.

Main content file:
<document>
  <section id=".." title="...">
    <!-- mixed element and CDATA content -->
  </section>
  <!-- more sections -->
</document>

Map file:
<map>
  <!-- map original sections to new department/articles -->
  <section id="..">
    <in-cms department="..." article="..." />
  </section>
  <!-- more section mappings -->

  <!-- define the hierarchy of the departments/articles -->
  <hierarchy>
    <department name="..">
      <article name=".." />
      <!-- more articles -->
    </department>
    <!-- more departments>
  </hierarchy>

  <!-- even the names of resources have to change -->
  <files>
    <file orig-src="..." new-filename="..." new-id=".." />
    <!-- more files -->
  </files>
</map>

This stylesheet accepts the map file as its source and uses the document() 
function to retrieve the main content. It includes templates which match 
elements from both the map file /and/ the main content file.

It all works fine except for one thing: where I have a template which matches 
one of the elements from the main content file, e.g. "image", I can't seem to 
access the hierarchy of the map file in XPath expressions:

<xsl:template match="image">
  <!-- image has a @src attribute which needs to be looked up in 
map.xml//file[@orig-src=$src] to find the new filename -->
  <xsl:variable name="src" select="@src" />
  <img src="{//file[@orig-src=$src]/@new-filename" />
</xsl:template>

This doesn't work as the XPath:

//file[@orig-src=$src]/@new-filename

doesn't return anything.

(I checked count(//file) and it returned 0)

I managed to get it to work by using the document() function to pull in my 
main input file again:

document('map.xml')/map/files/file[@orig-src=$src]/@new-filename

Is this expected behaviour?

Cheers,
Richard
-- 
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Richard Lewis
Sonic Arts Research Archive
http://www.sara.uea.ac.uk/
JID: ironchicken@xxxxxxxxxxxxxxx
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Current Thread