> -----Original Message-----
> From: icewind [mailto:[EMAIL PROTECTED]]
> Sent: Tuesday, 9 July 2002 03:26
> To: [EMAIL PROTECTED]
> Subject: generating a manifest of xml docs
>
>
> I would like to be able to have cocoon take an entire
> directory of xml files, go through it, grab the
> contents of some tag (a that all the xml documents in
> this directory have) and output a page with the
> following information for each document in that
> directory:
>
>       1. The filename (linked to the file)
>       2. The contents of the tag from the file
>
>
> How would I do this?

Here's what I did. I'm not proud of it, but it works. It's quite
complicated, and I'm not entirely happy with the "layering" (SoC), since it
was done in a hurry, but it should give you an idea.

Actually, I'd be very grateful for any criticism of the code, especially
from the SoC point of view.

Here's the snippet from the sitemap:

<!-- collection of texts in html format -->
<map:match pattern="etexts/index.xhtml">
        <map:generate type="directory" src="nzetc/tei">
                <map:parameter name="include" value="[:alnum:]*.xml"/>
        </map:generate>
        <map:transform
src="nzetc/nzetc-xsl/tei-directory-to-included-titles-html.xsl">
                <!-- the base url to prepend to the tei fileames in the directory
listing -->
                <map:parameter name="source-base-url" value="cocoon:/etexts/title/"/>
                <!-- the base url to prepend to hyperlinks in the output html page -->
                <map:parameter name="output-base-url" value="/etexts/"/>
        </map:transform>
        <map:transform type="xinclude"/>
        <map:serialize type="xml"/>
</map:match>

1) It uses the directory generator to produce a listing of files with
extension .xml in the folder "nzetc/tei". These are "TEI" docs ("e-books")
see http://www.tei-c.org/ for details.

2) The directory listing is transformed directly to an XHTML page of links,
but with <xi:include> elements pointing to the titles of the e-books
embedded inside the html <a> elements (the links).

This is the XSLT which transforms the directory listing
(tei-directory-to-included-titles-html.xsl):

<!--
Convert a DirectoryGenerator document listing TEI docs into an HTML list
with xincludes pointing to the titles of the docs
-->
<xsl:stylesheet
        version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
        xmlns:dir="http://apache.org/cocoon/directory/2.0";
        xmlns:xi="http://www.w3.org/2001/XInclude";
        exclude-result-prefixes="dir" >

        <xsl:param name="source-base-url" select="/"/>
        <xsl:param name="output-base-url" select="/"/>

        <xsl:template match="/">
                <html><head><title><xsl:value-of select="count(.//dir:file)"/>
texts</title></head><body>
                <ul>
                        <xsl:apply-templates/>
                </ul>
                </body></html>
        </xsl:template>


        <xsl:template match="dir:file">
                <li><a
href="{$output-base-url}{substring-before(@name,'.xml')}/index.html">
                        <xi:include
href="{concat($source-base-url,@name)}#xpointer(/title/text())"/>
                </a></li>
        </xsl:template>

</xsl:stylesheet>

Note the "source-base-url" parameter which is passed to the XSLT to specify
how to read the title out of each e-book - the book is not read directly
from the file-system but via another call to the sitemap
("cocoon:/etexts/title/blah.xml") which returns the title. This is the
sitemap snippet involved:

<!-- produce an html fragment for a title -->
<map:match pattern="etexts/title/*.xml">
        <map:generate src="cocoon:/etexts/{1}.xml"/>
        <map:transform src="nzetc/nzetc-xsl/extract-tei-title.xsl"/>
        <map:serialize type="xml"/>
</map:match>

This is the XSLT to extract the title:

<!-- Extract the title of a TEI document -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

        <xsl:template match="/">
                <title>
                        <xsl:copy-of 
select="TEI.2/teiHeader/fileDesc/titleStmt/title/text()"/>
                </title>
        </xsl:template>

        <xsl:template match="node()"/>

</xsl:stylesheet>

This map:match is called from other places in the sitemap, where the books'
titles are needed - it's pretty simple as you can see, and could have been
done entirely by the xinclude processor if the
tei-directory-to-included-titles-html.xsl had inserted:

<xi:include
href="{concat($source-base-url,@name)}#xpointer(TEI.2/teiHeader/fileDesc/tit
leStmt/title)"/>

(I did it this way at first), but having a separate XSLT means I can add the
contents of other elements to the titles easily (e.g. the author's name,
publisher details, etc, could all be part of the title.)

3) Finally the xinclude runs and inserts the /title/text() of each ebook
into the html link for that ebook. The html is now complete. Actually, on
the NZETC site, this html is further processed to have banners and
navigation menu added, but it is workable, complete html at this point.

Phew! It's all a bit more complicated than I remembered! But I'm sure it
contains all the information you need to get your example working.

Cheers all!

Con


---------------------------------------------------------------------
Please check that your question  has not already been answered in the
FAQ before posting.     <http://xml.apache.org/cocoon/faq/index.html>

To unsubscribe, e-mail:     <[EMAIL PROTECTED]>
For additional commands, e-mail:   <[EMAIL PROTECTED]>

Reply via email to