[ https://issues.apache.org/jira/browse/XMLBEANS-561?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17526682#comment-17526682 ]
Dimitrios Lagkouvardos commented on XMLBEANS-561: ------------------------------------------------- Hi PJ, Adding the namespace declaration to the xpath expression resolved the XPathException. However, the query returns xml-fragment, which results in ClassCastException: {noformat} java.lang.ClassCastException: org.apache.xmlbeans.impl.values.XmlAnyTypeImpl cannot be cast to test.xb.EmployeeType{noformat} *I've tried xb 5.0.0 and 5.0.3 with Saxon 10.6 and 10.8.* I've used the employeeschema.xsd and employees.xml of [https://xmlbeans.apache.org/samples/XQueryXPath.html] and put together 2 simple test cases using XmlObject.selectPath and XmlCursor.selectPath. {noformat} public static void xpathTest1(final File f) { final String namespaceDeclaration = "declare namespace xq='http://xmlbeans.apache.org/samples/xquery/employees'; "; String pathExpression = "$this/xq:employees/xq:employee"; XmlObject xmlObj = parse(f); XmlObject[] results = xmlObj.selectPath(namespaceDeclaration + pathExpression); if (results.length > 0) { for (XmlObject o : results) { System.out.println(XMLHelper.getXmlBeanAsString(o, null)); EmployeeType e = (EmployeeType) o; // java.lang.ClassCastException } } } public static void xpathTest2(final File f) { final String namespaceDeclaration = "declare namespace xq='http://xmlbeans.apache.org/samples/xquery/employees'; "; String pathExpression = "$this/xq:employees/xq:employee"; XmlObject xmlObj = parse(f); XmlCursor cursor = xmlObj.newCursor(); cursor.selectPath(namespaceDeclaration + pathExpression); while (cursor.toNextSelection()) { System.out.println(XMLHelper.getXmlBeanAsString(cursor.getObject(), null)); EmployeeType e = (EmployeeType) cursor.getObject(); // java.lang.ClassCastException } }{noformat} This is the returned fragment: {noformat} <xml-fragment xmlns:emp="http://xmlbeans.apache.org/samples/xquery/employees"> <emp:name>Fred Jones</emp:name> <emp:address location="home"> <emp:street>900 Aurora Ave.</emp:street> <emp:city>Seattle</emp:city> <emp:state>WA</emp:state> <emp:zip>98115</emp:zip> </emp:address> <emp:address location="work"> <emp:street>2011 152nd Avenue NE</emp:street> <emp:city>Redmond</emp:city> <emp:state>WA</emp:state> <emp:zip>98052</emp:zip> </emp:address> <emp:phone location="work">(425)555-5665</emp:phone> <emp:phone location="home">(206)555-5555</emp:phone> <emp:phone location="mobile">(206)555-4321</emp:phone> </xml-fragment>{noformat} Thank you. > XPath support for namespace prefix to URI mappings > -------------------------------------------------- > > Key: XMLBEANS-561 > URL: https://issues.apache.org/jira/browse/XMLBEANS-561 > Project: XMLBeans > Issue Type: Improvement > Components: XPath > Affects Versions: Version 5.0.0 > Reporter: Dimitrios Lagkouvardos > Priority: Major > > 5.0.0 supports Saxon and an XmlBeans xpath engine. I don't see a way to > configure or supply at runtime namespace prefix to URI mappings. > *Suggestions*: > 1) One solution is to enhance XmlObject.selectPath(String,XmlOptions) to use > the namespaces returned by XmlOptions.getLoadAdditionalNamespaces (or perhaps > add a new method if this is deemed inappropriate). > 2) An alternative is to add an extension to retrieve the mappings to be > passed to the xpath engine (instead of passing it on each call to selectPath). > *Note*: Adding the namespace declaration to the xpath expression as > documented in [http://xmlbeans.apache.org/guide/XPathXQuery.html] does NOT > work: > ``` > java.lang.RuntimeException: net.sf.saxon.trans.XPathException: Namespace > prefix 'table' has not been declared > at > org.apache.xmlbeans.impl.xpath.saxon.SaxonXPath.selectNodes(SaxonXPath.java:128) > at > org.apache.xmlbeans.impl.xpath.saxon.SaxonXPath.selectPath(SaxonXPath.java:133) > at > org.apache.xmlbeans.impl.xpath.saxon.SaxonXPathEngine.next(SaxonXPathEngine.java:60) > at org.apache.xmlbeans.impl.store.Cursor._toSelection(Cursor.java:749) > at org.apache.xmlbeans.impl.store.Cursor._toNextSelection(Cursor.java:736) > at org.apache.xmlbeans.impl.store.Cursor._hasNextSelection(Cursor.java:728) > at org.apache.xmlbeans.impl.store.Cursor.syncWrapHelper(Cursor.java:2520) > at org.apache.xmlbeans.impl.store.Cursor.syncWrap(Cursor.java:2451) > at org.apache.xmlbeans.impl.store.Cursor.hasNextSelection(Cursor.java:2022) > at > org.apache.xmlbeans.impl.values.XmlObjectBase.selectPath(XmlObjectBase.java:496) > ``` > -- This message was sent by Atlassian Jira (v8.20.7#820007) --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@poi.apache.org For additional commands, e-mail: dev-h...@poi.apache.org