Re: [xsl] relative path from one node to another (XSLT 2.0 solution)

Subject: Re: [xsl] relative path from one node to another (XSLT 2.0 solution)
From: JBryant@xxxxxxxxx
Date: Thu, 19 May 2005 14:16:51 -0500
Hi, Richard,

I did it this way:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

  <!-- process the root -->
  <xsl:template match="document">
    <xsl:result-document href="root/delete.me"/>
    <xsl:apply-templates>
      <xsl:with-param name="path" select="'root'"/>
    </xsl:apply-templates>
  </xsl:template>

  <!-- process the directories -->
  <xsl:template match="section[section/@display='page']">
    <xsl:param name="path"/>
    <xsl:result-document href="{concat($path, '/', @id, '/delete.me')}"/>
    <xsl:apply-templates>
      <xsl:with-param name="path" select="concat($path, '/', @id)"/>
    </xsl:apply-templates>
  </xsl:template>

  <!-- process the pages -->
  <xsl:template 
match="section[@display='page'][not(section/@display='page')]">
    <xsl:param name="path"/>
    <xsl:result-document href="{concat($path, '/', @id, '.xml')}">
      <chunks>
        <xsl:apply-templates/>
      </chunks>
    </xsl:result-document>
  </xsl:template>

  <!-- process the inline sections -->
  <xsl:template match="section[@display='inline']">
    <content chunk="{@id}"/>
  </xsl:template>

</xsl:stylesheet>

Note that the "delete.me" bit is in there because I couldn't quickly 
figure out how to get result-document to make just a directory. Perhaps 
folks who have more file-system experience (or more time to fiddle) can 
help there.

Also, I relied on the fact that, in the data you presented, directories 
always had child sections with display="page" attributes. You'll have to 
tinker with the algorithm a bit if that's not true in your production 
system.

Anyway, when I applied this stylesheet to your data, I got the desired 
output, except that I got no temporary directory because your data didn't 
specify one. I did get a file called staff/permanent.xml, though. Perhaps 
you have a small error from copying earlier chunks into your sample. I 
didn't create actual HTML files, so I just called them XML files. I'm sure 
you can adjust when you fill out the details.

Interesting problem, by the way.

Tested with Saxon 8.4.

Jay Bryant
Bryant Communication Services
(presently consulting at Synergistic Solution Technologies)

Current Thread