commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From k...@dotech.com
Subject Re: Digester:
Date Thu, 09 May 2002 21:06:03 GMT
So, I gave XSLT a whirl and thought I'd share in case it's of interest.
I've attached a sample stylesheet that should be easy to adapt.
Essentially, there's a block that looks like:

<xsl:when test="$attr-name='proc-type'">
  <xsl:attribute name="procedureTypeString">
    <xsl:value-of select="$attr-node"/>
  </xsl:attribute>
</xsl:when>

that will transform the attribute proc-type into the attribute
procedureTypeString, which matches one of my bean properties. If you want
to try it on your own stuff, just create as many of these xsl:when blocks
as you have attributes to transform.

Here's some sample code (no exception handling) for how to perform the
transform and then hand off to the Digester:

import javax.xml.transform.*;
import javax.xml.transform.stream.*;
...
InputStream xmlIn;  // original xml file
InputStream xslIn;  // stylesheet file
...
Source xmlSource = new StreamSource(xmlIn);
Source xslSource = new StreamSource(xslIn);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
Result result = new StreamResult(bout);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer(xslSource);
transformer.transform(xmlSource, result);
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
digester.parse(bin);

I tested with Xalan 2.3.1 and Xerces 2.0.1, but it should work with
different processors/parsers. If you've got any suggestions for improving
the stylesheet, I'd love to hear them.

Quoting Kris Schneider <kris@dotech.com>:

> Being new to Digester, I hadn't really taken the xmlrules stuff into
> account. After a quick look, I came away thinking that perhaps I'm
> attacking the problem at the wrong point. The existing core rules will
> work just fine as long as I can transform attribute names to match bean
> properties. I admit I haven't worked much with XSLT, but isn't this
> right up its alley? Perhaps the more general-purpose solution is to
> apply a stylesheet and then have the digester parse the transformed
> output. Any thoughts?

-- 
Kris Schneider <mailto:kris@dotech.com>
D.O.Tech       <http://www.dotech.com/>

Mime
View raw message