我对 Xpath 很陌生,虽然它的概念对于一个长期程序员来说很简单,但我对函数的语法有点困惑......任何帮助表示赞赏。
假设您有一个 XML 文件 (为简单起见,这是部分来自 Southwind.xml)
<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>
在 Javascript 中,我如何构造一个路径(正确的语法)来使用任何 xpath 函数......我了解选择节点的基础知识,但是usting函数的语法是什么..
例如,假设我想要订单的所有数量的总和......这就是我陷入困境的地方,如果我理解一个函数的语法,其余的将很容易获得!我试过这条路会是什么,我知道这是非常错误的..
path="/Orders/Order/OrderDetails/OrderDetail[sum(quantity)]";
提前谢谢你
首先,您的 XML 需要关闭根节点的标记。( </Orders>
)
这段使用 XPath sum 函数的 JavaScript 代码在 Firefox 中工作,它返回 76,这是 XML 中所有 Quantity 标签的总和。我希望它有帮助:
<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>
您可以在此处找到 XPath 函数。这里有很好的例子。
使用document.selectNodes
(IE)或document.evaluate
(Chrome,Firefox,Opera,Safari)函数。您将在此处找到使用它们的方法:http://www.w3schools.com/xpath/xpath_examples.asp。
Mozilla在这里也提供了一个有用的文档:https://developer.mozilla.org/en-US/docs/Introduction_to_using_XPath_in_JavaScript。
以下是使用 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();
}
只是为了跟进...完全有道理的是,如果你将Xpath与Javascript一起使用,这一切都归结为XpathResult。所以我找到了这个网站,它回答了我之前的后续问题,以防万一有它......再次感谢http://help.dottoro.com/ljagksjc.php