I am very new to Xpath and although its concepts are simple for a long-time programmer, I am a bit confused on the syntax for functions .... any help appreciated.
let's say you have (for simplicity this is partial from southwind.xml) an xml file
<Orders>
<Order OrderID="10248">
<CustomerID>VINET</CustomerID>
<EmployeeID>5</EmployeeID>
<OrderDate>1996-07-04T14:25:55</OrderDate>
<RequiredDate>1996-08-01T06:43:44</RequiredDate>
<ShippedDate>1996-07-16T04:00:12</ShippedDate>
<ShipVia>3</ShipVia>
<Freight>32.3800</Freight>
<ShipName>Vins et alcools Chevalier</ShipName>
<ShipAddress>59 rue de l'Abbaye</ShipAddress>
<ShipCity>Reims</ShipCity>
<ShipRegion/>
<ShipPostalCode>51100</ShipPostalCode>
<ShipCountry>France</ShipCountry>
<OrderDetails>
<OrderDetail>
<ProductID>11</ProductID>
<UnitPrice>14.0000</UnitPrice>
<Quantity>12</Quantity>
<Discount>0</Discount>
</OrderDetail>
<OrderDetail>
<ProductID>42</ProductID>
<UnitPrice>9.8000</UnitPrice>
<Quantity>10</Quantity>
<Discount>0</Discount>
</OrderDetail>
<OrderDetail>
<ProductID>72</ProductID>
<UnitPrice>34.8000</UnitPrice>
<Quantity>5</Quantity>
<Discount>0</Discount>
</OrderDetail>
</OrderDetails>
</Order>
<Order OrderID="10249">
<CustomerID>TOMSP</CustomerID>
<EmployeeID>6</EmployeeID>
<OrderDate>1996-07-05T06:39:18</OrderDate>
<RequiredDate>1996-08-16T03:39:38</RequiredDate>
<ShippedDate>1996-07-10T14:39:39</ShippedDate>
<ShipVia>1</ShipVia>
<Freight>11.6100</Freight>
<ShipName>Toms Spezialitäten</ShipName>
<ShipAddress>Luisenstr. 48</ShipAddress>
<ShipCity>Münster</ShipCity>
<ShipRegion/>
<ShipPostalCode>44087</ShipPostalCode>
<ShipCountry>Germany</ShipCountry>
<OrderDetails>
<OrderDetail>
<ProductID>14</ProductID>
<UnitPrice>18.6000</UnitPrice>
<Quantity>9</Quantity>
<Discount>0</Discount>
</OrderDetail>
<OrderDetail>
<ProductID>51</ProductID>
<UnitPrice>42.4000</UnitPrice>
<Quantity>40</Quantity>
<Discount>0</Discount>
</OrderDetail>
</OrderDetails>
</Order>
in Javascript how do I construct a path (what is correct syntax) for using ANY of xpath functions...I understand the basics of selecting nodes, but what is the syntax for usting functions ..
For example let's say I wanted SUM of all Quantity for orders ... this is where I am getting stuck and if I understood the syntax for one function, the rest would be easy to get! What would the path be I tried this, and I know it is very wrong..
path="/Orders/Order/OrderDetails/OrderDetail[sum(quantity)]";
Thank you in advance
First of all, your XML needs the tag closing the root node. (</Orders>)
This piece of javascript code using XPath sum function works in firefox, it returns 76 which is the sum of all Quantity tags in the XML. I hope it helps:
<script>
var xml=document.implementation.createDocument('', 'doc', null);
xml.load('books.xml');
path='sum(/Orders/Order/OrderDetails/OrderDetail/Quantity)';
var result=xml.evaluate(path, xml, null, XPathResult.ANY_TYPE, null);
document.write(result.numberValue);
</script>
You can find XPath functions here. And good example sintaxis here.
Use document.selectNodes (IE) or document.evaluate (Chrome, Firefox, Opera, Safari) functions. You will find the way to use them here: http://www.w3schools.com/xpath/xpath_examples.asp.
Mozilla also provides a useful documentation here: https://developer.mozilla.org/en-US/docs/Introduction_to_using_XPath_in_JavaScript.
Here's how to sum the quantities per article using document.evaluate.
// retrieves xml document
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "southwind.xml", false);
xhttp.send("");
var xml = xhttp.responseXML;
// evaluates xpath expressions
var orders = xml.evaluate("//Order", xml, null, XPathResult.ANY_TYPE, null);
var order = orders.iterateNext();
while (order) {
var result = xml.evaluate("sum(OrderDetails//Quantity)", order, null,
XPathResult.NUMBER_TYPE, null);
console.log(result.numberValue);
order = orders.iterateNext();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With