document() loses relative path ...
"Rui Paiva" <[email protected]>
| Newsgroups | gmane.comp.mozilla.devel.xml |
|---|---|
| Organization | Another Netscape Collabra Server User |
| Message-ID | <[email protected]> |
Hi everyone,
I am having a kind of complicated problem....
In trying to write an application that works exclusively on a browser, I am
hitting a wall when it comes to the
usage of the document() XPath function.
I am using a xslt that opens several xml documents.
I am doing several passes on demand, depending of user selections, in order
not to load all the xml files on startup (there are too many files and it
would slow things down).
The xml data I want to process is organised as follows:
dir1/d1.xml
dir1/dir2/d2.xml
dir1/dir3/d3.xml
dir1/dir3/dir4/d4.xml
What I am doing:
Loading the XSLT and the first XML document into 2 DOMs
(document.implementation.createDocument( "", "", null );)
transforming the XML into another XML using ( processor.transformToDocument
(xmlDom, document);.)
The resulting xml document includes
<d1>
<file>d1.xml</file>
<relative>.</relative>
<d2>
<file>d2.xml</file>
<relative>./dir2</relative>
</d2>
<d3>
<file>d3.xml</file>
<relative>./dir3</relative>
</d3>
</d1>
This result works fine... It seems that the first execution of the
document() function's relative path is relative to the base (first
(d1))document I am using.
But when I try to expand the D3 node, executing the xsl again, the processor
has lost it's base path pointer, and I can't open another document unless I
use absolute paths, something I wanted to avoid...
Any idea how the parses is calculating the relative paths ?
Here's a copy of the XSL...
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:output method="xml" version="1.0" encoding="ISO-8859-1" />
<!-- Name of the selected library -->
<xsl:param name="selectedRelativePath" />
<xsl:param name="selectedLibraryName" />
<xsl:template match="/" >
<xsl:if test="PictureFileList" >
<FileList xml:base="." currentFileList="true" expanded="true" >
<RelativePath>
<xsl:value-of select="$selectedRelativePath" />
</RelativePath>
<RelativeLocation>
<xsl:value-of select="$selectedRelativePath" />
</RelativeLocation>
<FileName>
<xsl:value-of select="concat ( $selectedLibraryName, '.xml' )" />
</FileName>
<xsl:copy-of select="PictureFileList/Name" />
<xsl:call-template name="expandChildren" >
<xsl:with-param name="childList"
select="PictureFileList/PictureFileList" />
<xsl:with-param name="relativePath" select="'.'" />
</xsl:call-template >
</FileList >
</xsl:if >
<xsl:if test="not ( PictureFileList ) " >
<xsl:variable name="selectedNode" select="//FileList[ ( Name/text() =
$selectedLibraryName ) and ( RelativePath/text() =
$selectedRelativePath ) ]" />
<xsl:call-template name="recurseFileLists" >
<xsl:with-param name="currentNode" select="node()" />
<xsl:with-param name="selectedNode" select="$selectedNode" />
</xsl:call-template >
</xsl:if >
</xsl:template >
<xsl:template name="recurseFileLists" >
<xsl:param name="currentNode" />
<xsl:param name="selectedNode" />
<xsl:variable name="isSelected" select=" $currentNode = $selectedNode " />
<xsl:variable name="isAncestorOfSelected" select=" count (
$currentNode/descendant::FileList [. = $selectedNode ] ) > 0 " />
<xsl:variable name="hasPictureFileListChildren" select=" count (
$currentNode/child::PictureFileList ) > 0 " />
<FileList >
<xsl:if test="$isSelected" >
<xsl:if test="$selectedNode/@expanded = 'true'" >
<xsl:attribute name="expanded">false</xsl:attribute>
<xsl:copy-of select="$selectedNode/child::* |
$currentNode/@currentFileList | $currentNode/@xml:base" />
</xsl:if >
<xsl:if test="not ( $selectedNode/@expanded = 'true' ) " >
<xsl:attribute name="expanded">true</xsl:attribute>
<xsl:if test="$hasPictureFileListChildren" >
<xsl:variable name="relativePath" select="concat (
$selectedNode/parent::FileList/RelativePath, '/',
$selectedNode/RelativeLocation )" />
<RelativePath>
<xsl:value-of select="$relativePath" />
</RelativePath>
<xsl:call-template name="expandChildren" >
<xsl:with-param name="childList"
select="$selectedNode/PictureFileList" />
<xsl:with-param name="relativePath" select="$relativePath" />
</xsl:call-template >
</xsl:if >
<xsl:if test="not ( $hasPictureFileListChildren ) " >
<xsl:copy-of select="$currentNode/child::* |
$currentNode/@currentFileList | $currentNode/@xml:base" />
</xsl:if >
</xsl:if >
</xsl:if >
<xsl:if test="$isAncestorOfSelected" >
<xsl:copy-of select="$currentNode/RelativePath | $currentNode/FileList |
$currentNode/Name" />
<xsl:for-each select="$currentNode/FileList" >
<xsl:call-template name="recurseFileLists" >
<xsl:with-param name="currentNode" select="." />
<xsl:with-param name="selectedNode" select="$selectedNode" />
</xsl:call-template >
</xsl:for-each >
</xsl:if >
<xsl:if test=" not ( $isAncestorOfSelected or $isSelected ) " >
<xsl:copy-of select="$currentNode/RelativePath | $currentNode/FileList |
$currentNode/Name" />
</xsl:if >
</FileList >
</xsl:template >
<xsl:template name="expandChildren" >
<xsl:param name="childList" />
<xsl:param name="relativePath" />
<xsl:for-each select="$childList" >
<FileList expanded="false">
<xsl:attribute name="xml:base" >
<xsl:value-of select="RelativeLocation " />
</xsl:attribute >
<RelativePath>
<xsl:value-of select="concat ($relativePath, '/', RelativeLocation) "
/>
</RelativePath>
<xsl:copy-of select="Name | RelativeLocation | FileName" />
<xsl:variable name="fileLocation" >
<xsl:element name="fileLocation" >
<xsl:value-of select="concat ($relativePath, '/', RelativeLocation,
'/', FileName) " />
</xsl:element >
</xsl:variable >
<xsl:if test="count (ancestor::*) < 2" >
<xsl:copy-of
select="document($fileLocation)/PictureFileList/PictureFileList" />
</xsl:if >
<xsl:if test="count (ancestor::*) > 1" >
<xsl:copy-of
select="document('br2.xml')/PictureFileList/PictureFileList" />
</xsl:if >
</FileList >
</xsl:for-each >
</xsl:template >
</xsl:stylesheet>