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: Thomas LEDUC <thomas.leduc AT cerma.archi.fr>
  • To: XOM API for Processing XML with Java <xom-interest AT lists.ibiblio.org>
  • Subject: Re: [XOM-interest] XOM + XInclude + XSLT Transform + EntityResolver
  • Date: Wed, 16 Jun 2010 17:14:49 +0200

Michael Kay a écrit :
> The fact that it's a ParsingException is a pretty good clue that there's
> something wrong with the XML, and the fact that it's on line
>
> MyXomTest.java:39
>
> [...]
> which should be
>
> <?xml version='1.0'?>
>
Many thanks Michael, yes indeed, it was a syntax error.

Contrarily to what I wrote yesterday, I'm unable to process recursive
X-include. I've modified a bit the use case I sent so as to attest it
(see the independent class below). Here is the crash trace:

[Fatal Error] engine:1:1: Recursive include detected. Document
'file:///home/leduc/solenetb/trunk/platform/engine' was already processed.
[Fatal Error] engine:1:115: Error attempting to parse XML file
(href='lvl_2.xml').
[Fatal Error] :1:99: Error attempting to parse XML file (href='lvl_1.xml').
Exception in thread "main" nu.xom.ParsingException: Error attempting to
parse XML file (href='lvl_1.xml'). at line 1, column 99
at nu.xom.Builder.build(Unknown Source)
at nu.xom.Builder.build(Unknown Source)
at org.solenetb.engine.MyXomTest.main(MyXomTest.java:49)
Caused by: org.xml.sax.SAXParseException: Error attempting to parse XML
file (href='lvl_1.xml').
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown
Source)
... 3 more

And the class content:

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='lvl_1.xml'/><dd/></aa>";
private static final String XML2 = "<?xml version='1.0'?><bb
xmlns:xi='http://www.w3.org/2001/XInclude'><cc name='foo'/><xi:include
href='lvl_2.xml'/></bb>";
private static final String XML3 = "<?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 publicId, String systemId)
throws SAXException, IOException {
InputStream inputStream = null;
if (systemId.endsWith("lvl_1.xml")) {
inputStream = new ByteArrayInputStream(XML2.getBytes());
} else if (systemId.endsWith("lvl_2.xml")) {
inputStream = new ByteArrayInputStream(XML3.getBytes());
}
if (null == inputStream) {
return null;
}
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());
}
}





Archive powered by MHonArc 2.6.24.

Top of Page