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.