将Namespace添加到Java(Apache FOP的 - JSP)文件,通过XSL样式表进行解析

时间:2013-06-27 15:42:44

标签: java xml xslt xsl-fo apache-fop

我正在尝试使用Apache FOP为我正在处理的应用程序创建PDF文档。

这需要我创建一个有效的XSL-FO文件,FOP引擎然后解析该文件以格式化它创建的文档的样式。

XSL文件具有自定义命名空间,使FOP引擎在尝试解析文件时抛出错误。我得到的错误,

org.w3c.dom.DOMException: NAMESPACE_ERR: An attempt is made to create or change an object in a way which is incorrect with regard to namespaces.
at     com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl.checkDOMNSErr(CoreDocumentImpl.java:2526)
at com.sun.org.apache.xerces.internal.dom.AttrNSImpl.setName(AttrNSImpl.java:113)
    ....

XSL / XML文件中的命名空间示例:

<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:altova="http://www.altova">

<x:xmpmeta xmlns:x="adobe:ns:meta/">
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">

通过XSL解析的FOP JSP文件代码:

<%@ page import='java.io.*'%>
<%@ page import='org.xml.sax.InputSource'%>
<%@ page import='org.apache.fop.apps.Driver'%>
<%@ page import='org.apache.fop.apps.Options'%>
<%@ page import='oracle.xml.parser.v2.XMLDocument'%>
<%@ page import='oracle.xml.parser.v2.XSLProcessor'%>
<%@ page import='oracle.xml.parser.v2.XSLStylesheet'%>
<%@ page import='oracle.xml.parser.v2.DOMParser'%>
<%
response.setContentType("application/pdf");

XMLDocument   v_doc;
XSLStylesheet v_xsl = null;
String        v_fop;
DOMParser     parser = new DOMParser();
XSLProcessor processor = new XSLProcessor();
// set the encoding for the XML Processing
String        v_encode = "UTF-8";

// get the XSL
v_xsl = new XSLStylesheet(new    java.io.StringReader(request.getParameter("template")),null);

// get the XML String from the form which was posted
parser.parse(new java.io.StringReader(request.getParameter("xml")));
// get the XML Document
v_doc = parser.getDocument();

// create an output stream to get the transformed results
ByteArrayOutputStream v_out = new ByteArrayOutputStream();
// transform the xml and xsl to get an FOP
processor.processXSL(v_xsl, v_doc, v_out);
// convert the FOP byte array to a string with encoding set above
v_fop = new String(v_out.toByteArray(),v_encode);

//
// Now call the apache FOP processing
//
Driver driver = new Driver();
// set the desired output
// see http://xml.apache.org/fop/output.html for all output types
driver.setRenderer(Driver.RENDER_PDF);

// set the input for the FOP engine
driver.setInputSource(new InputSource(new StringReader(v_fop)));
// set the output to stream to the browser
driver.setOutputStream(response.getOutputStream());
// process
driver.run();
%>

现在,     Adding namespace to an already created XML document 在我需要添加类似.setAttributeNS(“http://www.w3.org/2000/xmlns/”,“xmlns:ns2”,“http://”)的代码的相同行上说些什么;到我的JSP,它可以理解XML命名空间并克服NAMESPACE_ERR错误。

我不是一个Java人员,非常感谢我对需要使用的代码提供任何帮助。

谢谢, 手套。

1 个答案:

答案 0 :(得分:0)

使用样式表的顶级元素来定义所有名称空间:

<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:altova="http://www.altova" xmlns:x="adobe:ns:meta/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">

然后删除内联命名空间:

<x:xmpmeta>
<rdf:RDF>

<强>参考