【问题标题】:xpath function syntax javascriptxpath 函数语法 javascript
【发布时间】:2014-02-21 06:06:23
【问题描述】:

我对 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 库?

标签: javascript xpath


【解决方案1】:

首先,您的 XML 需要关闭根节点的标记。 (&lt;/Orders&gt;)

这段使用 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 函数here。还有很好的例子 sintaxis here.

【讨论】:

    【解决方案2】:

    使用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() 函数,获取字符串长度值的属性是什么?
    • numberValue 是 W3C DOM XPath API 定义的XPathResult 接口的属性,请参阅w3.org/TR/DOM-Level-3-XPath/xpath.html#XPathResult-numberValue。您可以在 Mozilla、Opera、Chrome 等浏览器中将该 API 与 Javascript 一起使用,但在 Java 世界中也有该 API 的实现,请参阅xml.apache.org/xalan-j/apidocs/org/apache/xpath/domapi/…
    【解决方案3】:

    只是为了跟进...完全有道理,如果您将 Xpath 与 Javascript 一起使用,这一切都归结为 XpathResult...所以我发现这个网站回答了我之前的后续问题,以防万一其他人有它... 再次感谢 http://help.dottoro.com/ljagksjc.php

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-08
      • 1970-01-01
      • 2011-05-27
      • 2017-09-19
      相关资源
      最近更新 更多