You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xmlbeans.apache.org by Chethan C <ch...@gmail.com> on 2008/11/27 09:33:34 UTC

Help needed on selectPath

I am not sure how the declare namespace works i did so much research into
this but still not working....Here are my schema and xml files :
http://www.nabble.com/file/p20715252/employeeschema.xsd employeeschema.xsd 
http://www.nabble.com/file/p20715252/employees.xml employees.xml 

the code i used to run is:
 public boolean updateWorkPhone(XmlObject empDoc)
    { final static String m_namespaceDeclaration = 
    	 "declare namespace
xq='http://xmlbeans.apache.org/samples/xquery/employees ';";    

        boolean hasResults = false;

        // Print the XML received.
        System.out.println("XML as received by updateWorkPhone method: \n\n"
                + empDoc.toString());
 
        // Create a variable with the query expression.
        String pathExpression = 
            "$this//xq:employees/xq:employee/xq:phone[@location='work']";
        XmlObject[] results = empDoc.selectPath(m_namespaceDeclaration 
+pathExpression    );



test class for running above code:
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlObject;

import com.sun.xml.ws.security.opt.crypto.dsig.XMLObject;

import java.io.*;


public class Test {

	
	public static void main(String args[]) 
	{	File fo=new File("C:\\employees.xml");
		XmlObject xobj=null;
		try {
			xobj = XmlObject.Factory.parse(fo);
		} catch (XmlException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	SelectPath sp=new SelectPath();
		sp.updateWorkPhone(xobj);
		
	}
	
	
	
	
}
 and the output contains the text from xml and then the expected query , but
it fails like on query like below, I think I might have gone wrong in
declare namespace in the above method or may be in the employeeschema or
employees xml file , I am not sure where.Please point me in the right
direction:
The output:XML as received by updateWorkPhone method: 

<!--Copyright 2004 The Apache Software Foundation

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.-->
<employees xmlns="http://xmlbeans.apache.org/samples/xquery/employees">
  <employee>
    <name>Fred Jones</name>
    <address location="home">
      <street>900 Aurora Ave.</street>
      <city>Seattle</city>
      <state>WA</state>
      <zip>98115</zip>
    </address>
    <address location="work">
      <street>2011 152nd Avenue NE</street>
      <city>Redmond</city>
      <state>WA</state>
      <zip>98052</zip>
    </address>
    <phone location="work">(425)555-5665</phone>
    <phone location="home">(206)555-5555</phone>
    <phone location="mobile">(206)555-4321</phone>
  </employee>
  <employee>
    <name>Sally Smith</name>
    <address location="home">
      <street>1430 Oak Place</street>
      <city>Salem</city>
      <state>OR</state>
      <zip>97125</zip>
    </address>
    <address location="work">
      <street>765 Main St.</street>
      <city>Keizer</city>
      <state>OR</state>
      <zip>97303</zip>
    </address>
    <phone location="work">(503)555-3856</phone>
    <phone location="home">(503)555-6951</phone>
    <phone location="mobile">(503)555-5152</phone>
  </employee>
  <employee>
    <name>Gladys Kravitz</name>
    <address location="home">
      <street>1313 Mockingbird Lane</street>
      <city>Seattle</city>
      <state>WA</state>
      <zip>98115</zip>
    </address>
    <address location="work">
      <street>2011 152nd Avenue NE</street>
      <city>Redmond</city>
      <state>WA</state>
      <zip>98052</zip>
    </address>
    <phone location="work">(425)555-6897</phone>
    <phone location="home">(206)555-6594</phone>
    <phone location="mobile">(206)555-7894</phone>
  </employee>
</employees>
Exception in thread "main" java.lang.RuntimeException:  Trying XBeans path
engine... Trying XQRL... Trying delegated path engine... FAILED on declare
namespace xq='http://xmlbeans.apache.org/samples/xquery/employees
';$this//xq:employees/xq:employee/xq:phone[@location='work']
	at org.apache.xmlbeans.impl.store.Path.getCompiledPath(Path.java:173)
	at org.apache.xmlbeans.impl.store.Path.getCompiledPath(Path.java:130)
	at org.apache.xmlbeans.impl.store.Cursor._selectPath(Cursor.java:902)
	at org.apache.xmlbeans.impl.store.Cursor.selectPath(Cursor.java:2634)
	at
org.apache.xmlbeans.impl.values.XmlObjectBase.selectPath(XmlObjectBase.java:462)
	at
org.apache.xmlbeans.impl.values.XmlObjectBase.selectPath(XmlObjectBase.java:446)
	at SelectPath.updateWorkPhone(SelectPath.java:74)
	at Test.main(Test.java:25)



Regarding jars i have used xmlbeans , xbean_xpath,saxon8 ,
saxon8-xpath,jsr173_1_0 and i use eclipse ide to run the program

-- 
View this message in context: http://www.nabble.com/Help-needed-on-selectPath-tp20715252p20715252.html
Sent from the Xml Beans - Dev mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@xmlbeans.apache.org
For additional commands, e-mail: dev-help@xmlbeans.apache.org