XPath函数语法JavaScript

3

我是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函数中的任何一个...我理解如何选择节点的基础知识,但是如何使用函数的语法是什么?

例如,假设我想要订单数量的总和...这就是我卡住的地方,如果我了解了一个函数的语法,其他的就容易获得了! 路径应该是什么呢?我尝试了这个,但我知道这是非常错误的...

path="/Orders/Order/OrderDetails/OrderDetail[sum(quantity)]";

提前感谢你的帮助


你使用的是哪个XPath库? - sphair
3个回答

2
首先,您的 XML 需要有关闭根节点的标签 (</Orders>)。
以下这段使用 XPath sum 函数的 JavaScript 代码可以在 Firefox 中运行,并返回所有 Quantity 标签的总和 76。希望对您有所帮助。
<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函数。还有这里提供了很好的示例语法。


1

使用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和Xpath?换句话说,.numberValue属性是您所调用的Javascript属性还是Xpath属性?例如,如果使用string-length()函数,获取字符串长度的属性是什么? - philbeat
1
numberValue是W3C DOM XPath API定义的XPathResult接口的一个属性,详见http://www.w3.org/TR/DOM-Level-3-XPath/xpath.html#XPathResult-numberValue。您可以在Mozilla、Opera、Chrome等浏览器中使用JavaScript来调用该API,但在Java世界中也有该API的实现,详见http://xml.apache.org/xalan-j/apidocs/org/apache/xpath/domapi/package-summary.html。 - Martin Honnen

1

只是跟进一下...如果你正在使用JavaScript和XPath,那么所有的问题都归结于XpathResult...所以我找到了这个网站,回答了我之前的跟进问题,以防其他人也有同样的问题... 再次感谢 http://help.dottoro.com/ljagksjc.php


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接