Solprovider Lenya Kongregate Registration for Free Flash Games and Chat with solprovider

Using the XpathDirectory Generator for Aggregating Multiple Files

This is about aggregating XML files from a directory and possibly subdirectories. This makes it possible to aggregate an entire publication into a single file.

Start by reading two pages from the official Cocoon documentation. The first defines the XpathDirectory Generator.
You also need to read about the Directory Generator, because the XpathDirectory Generator inherits many of the configuration options, and the documenter did not make the first page complete.

And now the reason this page exists. The XpathDirectory Generator works great, but the programmers forgot to make it usable by regular people.

Depth

There is no method to disable "depth". The default "1" returns a single directory. "0" returns nothing. I set it to "99"; the OS should break before hitting that limit.

Xpath Parameter

The xpath parameter does not understand namespaces. To return:
<map:parameter name="xpath" value="/xhtml:html'/lenya:meta/dc:title"/>
Use:
<map:parameter name="xpath" value="/*[local-name()='html']/*[local-name()='meta']/*[local-name()='title']"/>

This makes the value very long, especially if you want more than one field. The field separator is the pipe '|'.
<map:parameter name="xpath" value="/*[local-name()='html']/*[local-name()='meta']/*[local-name()='title']|/*[local-name()='html']/*[local-name()='meta']/*[local-name()='title']"/>

Using the Results

The XML returned is a mess. The XPath is included in every entry, when it should be in a confiuration section, and you probably do not want it at all. You will want an XSLT to turn the results into something usable before the usual XSLT to make the results displayable. The structure is:
dir:directory/dir:file/dir:xpath/{your fields}
The namespaces (that couldn't be configured in the last comment) will show in the results, so the example would match:
dir:directory/dir:file/dir:xpath/dc:title
Make certain to copy the appropriate namespace headers to your XSLTs.

Getting the File Path

When retrieving subdirectories, the results are even messier. In good XML fashion (and inherited from the Directory Generator), the directories are recursive entries. That is great if you want to display a tree of the files. The path of subdirectores is not stored in their entries, just the current directory name. That is bad if you want to merge the information and remember the path that generated each entry. The following stylesheet filters the files, adds the full path, and flattens the XML so each file is a first-level entry.

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

<xsl:template match="/dir:directory" priority="3">
<files>
<xsl:apply-templates select="dir:directory"/>
</files>
</xsl:template>

<xsl:template match="dir:directory">
<xsl:param name="path" select="''"/>
<!-- Use the next line to filter files, such as "dir:file[contains(@name, '_')]" -->
<xsl:apply-templates select="dir:file">
<xsl:with-param name="path" select="concat($path, '/', @name)"/>
</xsl:apply-templates>
<xsl:apply-templates select="dir:directory">
<xsl:with-param name="path" select="concat($path, '/', @name)"/>
</xsl:apply-templates>
</xsl:template>

<xsl:template match="dir:file">
<xsl:param name="path" select="''"/>
<xsl:element name="file">
<xsl:attribute name="id"><xsl:value-of select="$path"/></xsl:attribute>
<xsl:attribute name="language"><xsl:value-of select="substring-before(substring-after(@name, '_'), '.')"/></xsl:attribute>
<xsl:attribute name="idl"><xsl:value-of select="$path"/>_<xsl:value-of select="substring-before(substring-after(@name, '_'), '.')"/></xsl:attribute>
<xsl:apply-templates select="dir:xpath"/>
</xsl:element>
</xsl:template>

<xsl:template match="dir:xpath">
<!-- Add elements for your information here -->
</xsl:template>
</xsl:stylesheet>


<< SecurityExternal Integration >>

Contact Solprovider
Paul Ercolino