jaspernewuser Posted August 26, 2008 Share Posted August 26, 2008 Hi, I am trying to generate a jasper report for which I am using xml as data source . I am using JRXmlDataSource for this purpose. The problem here is that my xml has tags which uses namespace and because of this I am getting the error. below is the sample code String outFileName = "C:/addressbook11.pdf"; String xmlFileName = "C:/mydatasource.xml"; String recordPath = "/osm:Envelope/osm:OSMObjectConfiguration"; JasperDesign jasperDesign = JRXmlLoader.load("C:/OSM.jrxml"); JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign); JRXmlDataSource jrxmlds = new JRXmlDataSource(xmlFileName,recordPath); HashMap hm = new HashMap(); JasperPrint print = JasperFillManager.fillReport(jasperReport,hm,jrxmlds); JRExporter exporter = new JRPdfExporter(); exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,outFileName); exporter.setParameter(JRExporterParameter.JASPER_PRINT,print); exporter.exportReport(); the XML which I am using <?xml version="1.0" encoding="UTF-8"?> <osm:Envelope xmlns:osm="http://www.my.com/com/osm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.myserver.com/com/osm ../CommonDataModel.xsd" subsystem="OSMR_123" variant="5678"> <osm:OSMObjectConfiguration name="valve_abc" uuid="ec3a0500-4c21-11dd-ae16-0800200c9a66" objectType="valve" validFrom="2006-01-01T00:00:00" validTo=""> <osm:AttributeGroup validFrom="2006-01-01T00:00:00" validTo="2008-02-01T00:00:00"> <osm:Attribute name="Name" type="string" value="Valve" /> <osm:Attribute name="Location" type="string" value="Reckrod" /> <osm:Attribute name="SAP" type="string" value="SAP_123" /> <osm:Attribute name="NetzknotenID" type="int" value="423463546" /> </osm:AttributeGroup> <osm:PropertyGroup name="SCADA"> <osm:Property name="T1" type="boolean" value="1"/> <osm:Property name="ThresholdMin" type="int" value="10" /> </osm:PropertyGroup> <osm:PropertyGroup name="Simulation"> <osm:Property name="GaskonstX" type="int" value="10" /> </osm:PropertyGroup> <osm:PropertyGroup name="Forecast"> <osm:Property name="KonstA" type="double" value="9.81" /> </osm:PropertyGroup> </osm:OSMObjectConfiguration> </osm:Envelope> and below is the error am getting Exception in thread "main" at net.sf.jasperreports.engine.util.xml.XalanXPathExecuter.selectNodeList( at net.sf.jasperreports.engine.data.JRXmlDataSource.moveFirst( at net.sf.jasperreports.engine.data.JRXmlDataSource.<init>( at net.sf.jasperreports.engine.data.JRXmlDataSource.<init>( at test.XmlDataSourceTest.main( Caused by: at org.apache.xpath.compiler.XPathParser.error( at org.apache.xpath.compiler.Lexer.mapNSTokens( at org.apache.xpath.compiler.Lexer.tokenize( at org.apache.xpath.compiler.Lexer.tokenize( at org.apache.xpath.compiler.XPathParser.initXPath( at org.apache.xpath.XPath.<init>( at org.apache.xpath.CachedXPathAPI.eval( at org.apache.xpath.CachedXPathAPI.selectNodeList( at org.apache.xpath.CachedXPathAPI.selectNodeList( at net.sf.jasperreports.engine.util.xml.XalanXPathExecuter.selectNodeList( ... 4 more net.sf.jasperreports.engine.JRException: XPath selection failed. Expression: /osm:Envelope/osm:OSMObjectConfigurationXalanXPathExecuter.java:62)JRXmlDataSource.java:263)JRXmlDataSource.java:178)JRXmlDataSource.java:222)XmlDataSourceTest.java:41)javax.xml.transform.TransformerException: Prefix must resolve to a namespace: cdmXPathParser.java:602)Lexer.java:638)Lexer.java:265)Lexer.java:96)XPathParser.java:108)XPath.java:162)CachedXPathAPI.java:276)CachedXPathAPI.java:209)CachedXPathAPI.java:189)XalanXPathExecuter.java:60) Link to comment Share on other sites More sharing options...
viggonavarsete Posted July 7, 2010 Share Posted July 7, 2010 Hi, what's the status of this? I'm also facing problems using XPath to XML elements with namespaces. I'm using iReport 3.7.2. The XML document I'm using looks like this:<QueryResultsxmlns:ns2="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader"xmlns:ns3="urn:epcglobal:epcis-query:xsd:1" xmlns:ns4="urn:epcglobal:epcis-masterdata:xsd:1"xmlns:ns5="urn:epcglobal:epcis:xsd:1"><results><queryName>/query/SimpleEventQuery</queryName><resultsBody><EventList><ObjectEvent><eventTime>2010-07-02T13:24:00.000+02:00</eventTime><recordTime>2010-07-06T14:13:18.028+02:00</recordTime><eventTimeZoneOffset>+02:00</eventTimeZoneOffset><epcList><epc>Lot2</epc></epcList><action>OBSERVE</action><ns6:weight xmlns:ns6="http://www.tracefood.org/schema/epcis">1000</ns6:weight></ObjectEvent><ObjectEvent><eventTime>2010-07-02T13:24:00.000+02:00</eventTime><recordTime>2010-07-06T14:13:18.029+02:00</recordTime><eventTimeZoneOffset>+02:00</eventTimeZoneOffset><epcList><epc>Lot2</epc></epcList><action>OBSERVE</action><ns6:moisture xmlns:ns6="http://www.tracefood.org/schema/epcis">20</ns6:moisture></ObjectEvent></EventList></resultsBody></results></QueryResults> and I want to access the weight and moisture elements. Ireport generates the following XPath's for me:/QueryResults/results/resultsBody/EventList/ObjectEvent/ns6:weightbut I get this exception back when I try to preview it:Error filling print... XPath selection failed. Expression: /QueryResults/results/resultsBody/EventList/ObjectEvent/ns6:weightnet.sf.jasperreports.engine.JRException: XPath selection failed. Expression: /QueryResults/results/resultsBody/EventList/ObjectEvent/ns6:weight at net.sf.jasperreports.engine.util.xml.JaxenXPathExecuter.selectObject(JaxenXPathExecuter.java:128) at net.sf.jasperreports.engine.data.JRXmlDataSource.getFieldValue(JRXmlDataSource.java:302) at net.sf.jasperreports.engine.fill.JRFillDataset.setOldValues(JRFillDataset.java:823) at net.sf.jasperreports.engine.fill.JRFillDataset.next(JRFillDataset.java:787) at net.sf.jasperreports.engine.fill.JRBaseFiller.next(JRBaseFiller.java:1474) at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:125) at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:938) at net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:118) at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:435) at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:271) at com.jaspersoft.ireport.designer.compiler.IReportCompiler.run(IReportCompiler.java:970) at org.openide.util.RequestProcessor$Task.run(RequestProcessor.java:572) at org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:997) Caused by: org.jaxen.UnresolvableException: Cannot resolve namespace prefix 'ns6' at org.jaxen.expr.DefaultNameStep.matches(DefaultNameStep.java:358) at org.jaxen.expr.DefaultNameStep.evaluate(DefaultNameStep.java:285) at org.jaxen.expr.DefaultLocationPath.evaluate(DefaultLocationPath.java:140) at org.jaxen.expr.DefaultAbsoluteLocationPath.evaluate(DefaultAbsoluteLocationPath.java:113) at org.jaxen.expr.DefaultXPathExpr.asList(DefaultXPathExpr.java:102) at org.jaxen.BaseXPath.selectNodesForContext(BaseXPath.java:674) at org.jaxen.BaseXPath.selectNodes(BaseXPath.java:213) at org.jaxen.BaseXPath.evaluate(BaseXPath.java:172) at net.sf.jasperreports.engine.util.xml.JaxenXPathExecuter.selectObject(JaxenXPathExecuter.java:102) ... 12 more Link to comment Share on other sites More sharing options...
lucianc Posted July 9, 2010 Share Posted July 9, 2010 viggonavarseteWrote: Hi, what's the status of this? Not yet implemented, see http://jasperforge.org/projects/jasperreports/tracker/view.php?id=4500Have you tried khill's workaround?Regards,Lucian Link to comment Share on other sites More sharing options...
tonynys Posted January 3, 2011 Share Posted January 3, 2011 This is the workaround and it works !,for the moment I hardcode namespace mappings,but this could be set by using a threadlocale set by the client java program Code:import javax.xml.transform.TransformerException;import net.sf.jasperreports.engine.JRException;import net.sf.jasperreports.engine.util.xml.JRXPathExecuter;import net.sf.jasperreports.engine.util.xml.JRXPathExecuterFactory;import org.apache.xml.utils.PrefixResolver;import org.apache.xpath.CachedXPathAPI;import org.apache.xpath.objects.XObject;import org.w3c.dom.Node;import org.w3c.dom.NodeList;/** * custom xpath exector factory to bypass the bug in jasper for namespace support in xml * datasources; namespaces are not supported at all. * * * run this to use a custom properties file: * System.setProperty("net.sf.jasperreports.properties", "/tmp/tony.jasperreports.properties"); * set this line in the jasper report properties file: * net.sf.jasperreports.xpath.executer.factory=MyXalanXPathExecuterFactory * * @author tony nys * */public class MyXalanXPathExecuterFactory implements JRXPathExecuterFactory{ public MyXalanXPathExecuterFactory(){ System.out.println("MyXalanXPathExecuterFactory............."); } @Override public JRXPathExecuter getXPathExecuter() { return new MyJRXPathExecuter(); } /** * source copied from XalanXPathExecuter from jasperreports * and then tweeked for namespace support * */ public class MyJRXPathExecuter implements JRXPathExecuter{ // XPath API facade private CachedXPathAPI xpathAPI = new CachedXPathAPI(); PrefixResolver resolver=null; public MyJRXPathExecuter(){ System.out.println("MyJRXPathExecuter............."); //maybe link resolver through threadlocale from calling client through a map, //so namespaces can be set dynamically by client program resolver=new MyPrefixResolver(); } public NodeList selectNodeList(Node contextNode, String expression) throws JRException { //System.out.println(",,,selectNodeList"); xpathAPI.getXPathContext().setNamespaceContext(resolver); try { return xpathAPI.selectNodeList(contextNode, expression); } catch (TransformerException e) { throw new JRException("XPath selection failed. Expression: " + expression, e); } } public Object selectObject(Node contextNode, String expression) throws JRException { try { //System.out.println(",,,selectObject"); //xpathAPI.getXPathContext().setNamespaceContext(resolver); Object value; XObject object = xpathAPI.eval(contextNode, expression,resolver); switch (object.getType()) { case XObject.CLASS_NODESET: value = object.nodeset().nextNode(); break; case XObject.CLASS_BOOLEAN: value = object.bool() ? Boolean.TRUE : Boolean.FALSE; break; case XObject.CLASS_NUMBER: value = new Double(object.num()); break; default: value = object.str(); break; } return value; } catch (TransformerException e) { throw new JRException("XPath selection failed. Expression: " + expression, e); } } } //public class MyPrefixResolver extends JAXPPrefixResolver{ public class MyPrefixResolver implements PrefixResolver{ public MyPrefixResolver(){ //super(); } @Override public String getBaseIdentifier() { //System.out.println("///////////////getBaseIdentifier"); return null; } @Override public String getNamespaceForPrefix(String arg0) { //System.out.println("///////////////getNamespaceForPrefix"); //TODO: use threadlocale here... if(arg0.equalsIgnoreCase("v210")){ return "http://wpm.ac.com/solutions/fl/l/core/types/v210"; }else if(arg0.equalsIgnoreCase("wpm-core-common")){ return "http://wpm.ac.com/core/common/types/v210"; }else if(arg0.equalsIgnoreCase("wpm-business-common")){ return "http://wpm.ac.com/business/common/types/v210"; }else if(arg0.equalsIgnoreCase("v2101")){ return "http://wpm.ac.com/solutions/fl/l/configuration/types/v210"; } return null; } @Override public String getNamespaceForPrefix(String arg0, Node arg1) { //System.out.println("///////////////getNamespaceForPrefix2"); //TODO: use threadlocale here... if(arg0.equalsIgnoreCase("v210")){ return "http://wpm.ac.com/solutions/fl/l/core/types/v210"; }else if(arg0.equalsIgnoreCase("wpm-core-common")){ return "http://wpm.ac.com/core/common/types/v210"; }else if(arg0.equalsIgnoreCase("wpm-business-common")){ return "http://wpm.ac.com/business/common/types/v210"; }else if(arg0.equalsIgnoreCase("v2101")){ return "http://wpm.ac.com/solutions/fl/l/configuration/types/v210"; } return null; } @Override public boolean handlesNullPrefixes() { return false; } }} Link to comment Share on other sites More sharing options...
clandestino Posted March 10, 2011 Share Posted March 10, 2011 I tried the solution from the above link (putting additional parameters in jasperreports.properties file) and it didn't work for me.Does anyone know is this bug fixed in verision 4.0.1? This is one of mine xml files with namespaces: <?xml version="1.0" encoding="UTF-8"?><rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:exslt="http://exslt.org/common" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dcmiBox="http://dublincore.org/documents/2000/07/11/dcmi-box/" xmlns:dct="http://purl.org/dc/terms/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rim="urn:oasis:names:tc:ebxml-regrep:xsd:rim:3.0"><rdf:Description rdf:about="http://saleserver:8080/geoportal/csw?Request=GetCapabilities&Service=CSW&Version=2.0.2"><dc:title> ArcGIS Server Geoportal Extension 10 - OGC CSW 2.0.2 ISO AP </dc:title><dc:description> A catalogue service that conforms to the HTTP protocol binding of the OpenGIS Catalogue Service ISO Metadata Application Profile specification (version 2.0.2) </dc:description><dc:format>text/xml</dc:format><dc:format>application/xml</dc:format><dc:contributor>provider name</dc:contributor><dc:identifier>http://saleserver:8080/geoportal/csw?Request=GetCapabilities&Service=CSW&Version=2.0.2</dc:identifier><dc:type>CSW</dc:type><dc:creator>name</dc:creator><dc:subject>CSW</dc:subject><dc:subject>keyword1</dc:subject><dc:subject>keyword2</dc:subject><dct:references>http://saleserver:8080/geoportal/csw?Request=GetCapabilities&Service=CSW&Version=2.0.2</dct:references><dct:references>http://saleserver:8080/geoportal/csw</dct:references><dct:references>http://saleserver:8080/geoportal/csw</dct:references></rdf:Description></rdf:RDF> Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now