• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need help with XQuery expression with XMLBeans

 
Ranch Hand
Posts: 563
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have this XML document that i want to query using XQuery :

<?xml version="1.0" encoding="UTF-8"?>
<parcautomobile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="D:\someSchema.xsd">
<vehicule modele="coupe">
<marque>
<nom>Renault</nom>
<puissance chevaux="8" vitessemax="280"/>
<nombre>18</nombre>
<couleur>noir</couleur>
</marque>
<marque>
<nom>Peugeot</nom>
<puissance chevaux="11" vitessemax="320"/>
<nombre>10</nombre>
<couleur>rouge</couleur>
</marque>
</vehicule>
<vehicule modele="berline">
<marque>
<nom>Mercedes</nom>
<puissance chevaux="10" vitessemax="330"/>
<nombre>9</nombre>
<couleur>gris</couleur>
</marque>
<marque>
<nom>BMW</nom>
<puissance chevaux="12" vitessemax="300"/>
<nombre>7</nombre>
<couleur>bleu</couleur>
</marque>
</vehicule>
</parcautomobile>

Let's say i want to query the value of the element <nombre> for the <marque>
element COUPE and the <nom> element PEUGEOT:
so it should return 10

I want to write a little program that returns it using XMLBeans and
XQuery.Here is what i got so far :

import java.io.File;
import noNamespace.ParcautomobileDocument;
import org.apache.xmlbeans.XmlCursor;
import org.apache.xmlbeans.XmlObject;

public class XqueryXML {

public static void main(String[] args) throws Exception
{
XmlObject resultXml =null;

ParcautomobileDocument parcautomobile =
ParcautomobileDocument.Factory.parse(new File ("d:/parcAuto.xml"));
//String expression =
"$this/parcautomobile/vehicule[@modele='berline']/marque/nom";

String expression =
"for $e in //parcautomobile/vehicule " +
"let $s := $e/@modele " +
"where $s = 'Peugeot' " +
"return $e//couleur";

XmlCursor monCursor = parcautomobile.newCursor().execQuery(expression);

if (monCursor.getSelectionCount() > 0)
{
System.out.println("monCursor is not empty");
resultXml = monCursor.getObject();
System.out.println(resultXml.toString());
monCursor.dispose();
}
}
}


It does not return anything.
What XQuery expression exactly should i write in order to return 10, the
number of PEUGEOT vehicules ?

Thanks for your help.
 
Bring out your dead! Or a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic