Skip to Content.
Sympa Menu

xom-interest - Re: [XOM-interest] XOM + XInclude + XSLT Transform + EntityResolver

xom-interest AT lists.ibiblio.org

Subject: XOM API for Processing XML with Java

List archive

Chronological Thread  
  • From: Michael Kay <mike AT saxonica.com>
  • To: xom-interest AT lists.ibiblio.org
  • Subject: Re: [XOM-interest] XOM + XInclude + XSLT Transform + EntityResolver
  • Date: Tue, 15 Jun 2010 20:46:55 +0100


What should I do to solve my bug ?

Examine the stack trace. If you don't understand it, post it here, where
there's a good chance that someone will.

Michael Kay
Saxonica



On 15/06/2010 20:10, Thomas LEDUC wrote:
Dear list-members,

I would like to benefit from XOM recursive XInclude ability. That's why,
I've given XALAN up. With XALAN, I was able to combine a (single)
XInclude with an EntityResolver (the x-included XML was located in the
JAR file and activated as a ResourceStream trough an EntityResolver) and
a XSLT Transform.

What I want right now is tu use XOM so as to combine 2 or 3 levels of
recursive XInclude with an EntityResolver (so as to x-include the XML
located as resources in the JAR file) and a XSLT transform. To achieve
such an objective, I've tried to follow P. Taylor's intructions (thanks
to him) described in the following post :
https://lists.ibiblio.org/sympa/arc/xom-interest/2009-August/003977.html

To reproduce the process I want to do, I've developed the small use case
presented below. Unfortunately, the following independent class (that
replaces class.getResourceAsStream by ByteArrayInputStream : XML2 is
x-included in XML1) compiles but crashes during execution. Do you know
why ? What should I do to solve my bug ?

Many thanks for your help,

import java.io.*;
import javax.xml.parsers.SAXParserFactory;
import nu.xom.*;
import nu.xom.xinclude.XIncluder;
import nu.xom.xslt.XSLTransform;
import org.xml.sax.*;

public class MyXomTest {
private static final String XML1 = "<?xml version='1.0'><aa
xmlns:xi='http://www.w3.org/2001/XInclude'><xi:include
href='foo.xml'/><dd/></aa>";
private static final String XML2 = "<?xml version='1.0'?><bb><cc
name='foo'/></bb>";
private static final String XSLT_IDENTITY = "<?xml version='1.0'?>"
+ "<xsl:stylesheet version='1.0'
xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\";>"
+ "<xsl:template match='node()|@*'>" +"<xsl:copy>"
+ "<xsl:apply-templates select='@*|node()'/>" +"</xsl:copy>"
+ "</xsl:template>" +"</xsl:stylesheet>";

public static class MyEntityResolver implements EntityResolver {
@Override
public InputSource resolveEntity(String arg0, String arg1)
throws SAXException, IOException {
InputStream inputStream = new
ByteArrayInputStream(XML2.getBytes());
InputSource inputSource = new InputSource(inputStream);
inputSource.setSystemId(AConsoleCommand.USER_DIR);
return inputSource;
}
}

public static void main(String[] args) throws Exception {
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
saxParserFactory.setNamespaceAware(true);
saxParserFactory.setXIncludeAware(true);
XMLReader x = saxParserFactory.newSAXParser().getXMLReader();
x.setEntityResolver(new MyEntityResolver());

Builder builder = new Builder(x);
Document input = builder
.build(new ByteArrayInputStream(XML1.getBytes()));
Document intermed_result = XIncluder.resolve(input, builder);
Document stylesheet = builder.build(new ByteArrayInputStream(
XSLT_IDENTITY.getBytes()));
XSLTransform transform = new XSLTransform(stylesheet);
Nodes output = transform.transform(intermed_result);
Document result = XSLTransform.toDocument(output);
System.out.println(result.toXML());
}
}
_______________________________________________
XOM-interest mailing list
XOM-interest AT lists.ibiblio.org
http://lists.ibiblio.org/mailman/listinfo/xom-interest






Archive powered by MHonArc 2.6.24.

Top of Page