XPathTraversableGenerator
Summary
Generates an XML collection listing performing XPath queries on XML sources.
Basic information
| Component type | Generator |
|---|---|
| Cocoon block | core |
| Java class | org.apache.cocoon.generation.XPathTraversableGenerator |
| Name in Sitemap | |
| Cacheable | Yes - Yes |
Documentation
It will perform an XPath query on every XML resource, where "xml resource" is, by default, any resource ending with ".xml", which can be overriden by setting the (regexp) pattern xmlFiles (this has the effect of increasing, not limiting, the scope of the search) as a sitemap parameter, or where the name of the resource has a container-wide mime-type mapping to 'text/xml' such as specified by mime-mapping elements in a web.xml descriptor file. The XPath can be specified in two ways:
- By using an XPointerish syntax in the URL: everything following the pound sign (possiby preceding query string arguments) will be treated as the XPath;
- Specifying it as a sitemap parameter named "xpath"
Note: Since XPathTraversableGemerator is a subclass of
TraversableGenerator, it uses the same set of parameters ( adding some of its
own though). Please reer to the TraversableGenerator documentation for an
explanation on the use of depth, exclude, include,
etc. parameters.
Sample usage:
Sitemap:
<map:match pattern="documents/**">
<map:generate type="xpathtraversable" src="docs/{1}#/article/title|/article/abstract" >
<map:parameter name="xmlFiles" value="\.xml$"/>
</map:generate>
<map:serialize type="xml"/>
</map:match>
Request:
http://www.some.host/documents/test
Result:
<collection:collection
name="test" lastModified="1010400942000"
date="1/7/02 11:55 AM" requested="true"
xmlns:collection="http://apache.org/cocoon/collection/1.0">
<collection:collection name="subdirectory" lastModified="1010400942000" date="1/7/02 11:55 AM" />
<collection:resource name="test.xml" lastModified="1011011579000" date="1/14/02 1:32 PM">
<collection:xpath docid="test.xml" query="/article/title">
<title>This is a test document</title>
<abstract>
<para>Abstract of my test article</para>
</abstract>
</collection:xpath>
</collection:resource>
<collection:resource name="test.gif" lastModified="1011011579000" date="1/14/02 1:32 PM">
</collection:collection>
Note: If you need to use namespaces, you can set them as sitemap
parameters in the form:
<map:parameter name="xmlns:your prefix" value="nsURI"/**">
<map:parameter name="xmlns:your prefix" value="nsURI"/**">
Another sample usage:
<map:match pattern="pages.xml">
<map:generate type="xpathtraversable" src="blockcontext:/">
<map:parameter name="depth" value="4"/>
<map:parameter name="exclude" value="xslt$|xmap$"/>
<map:parameter name="xmlFiles" value="\.jx$"/>
<map:parameter name="xpath" value="/*/content/title"/>
</map:generate>
<map:serialize type="xml"/>
</map:match>
This sitemap fragment will execute the "/*/content/title" XPath
expression for all XML files (including the ones ending with ".jx" but
excluding the ones ending with "xslt" or "xmap") for a depth
of 4 nesting across every block defined in the current block.Errors and Improvements? If you see any errors or potential improvements in this document please help
us: View, Edit or comment on the latest development version (registration required).


