【问题标题】:Can't figure out an XPath count expression result. C# xml Web Services无法计算出 XPath 计数表达式结果。 C# xml 网络服务
【发布时间】:2015-04-17 21:55:08
【问题描述】:
 <?xml version="1.0" encoding="ISO-8859-1" ?> 
- <OrderFeed>
- <Order id="1">
- <BillingInformation>
  <Name>Bruce Ganek</Name> 
  <Address>99 Main Street</Address> 
  <City>Cranston</City> 
  <State>RI</State> 
  <ZipCode>02910</ZipCode> 
  </BillingInformation>
- <ShippingInformation>
  <Name>Governor Chafee</Name> 
  <Address>82 Smith St # 115</Address> 
  <City>Providence</City> 
  <State>RI</State> 
  <ZipCode>02903-1121</ZipCode> 
  </ShippingInformation>
- <Items>
- <Item>
  <PartNo>JETSWEATER</PartNo> 
  <Description>N.Y. Jets Sweatshirt</Description> 
  <UnitPrice>10.50</UnitPrice> 
  <Quantity>2</Quantity> 
  <TotalCost>21.00</TotalCost> 
- <CustomerOptions>
  <Size>M</Size> 
  <Color>Green</Color> 
  </CustomerOptions>
  </Item>
- <Item>
  <PartNo>JETSWEATER</PartNo> 
  <Description>N.Y. Jets Sweatshirt</Description> 
  <UnitPrice>7.50</UnitPrice> 
  <Quantity>3</Quantity> 
  <TotalCost>22.50</TotalCost> 
- <CustomerOptions>
  <Size>S</Size> 
  <Color>White</Color> 
  </CustomerOptions>
  </Item>
- <Item>
  <PartNo>JETSFLASHLIGHT</PartNo> 
  <Description>N.Y. Jets Flashlight</Description> 
  <UnitPrice>5.00</UnitPrice> 
  <Quantity>1</Quantity> 
  <TotalCost>5.00</TotalCost> 
  <CustomerOptions /> 
  </Item>
  </Items>
  </Order>
- <Order id="2">
- <BillingInformation>
  <Name>Walt Disney</Name> 
  <Address>DisneyWorld Hotel</Address> 
  <City>Orlando</City> 
  <State>FL</State> 
  <ZipCode>32801</ZipCode> 
  </BillingInformation>
- <ShippingInformation>
  <Name>Walt Disney</Name> 
  <Address>DisneyWorld Hotel</Address> 
  <City>Orlando</City> 
  <State>FL</State> 
  <ZipCode>32801</ZipCode> 
  </ShippingInformation>
- <Items>
- <Item>
  <PartNo>JETSWEATER</PartNo> 
  <Description>N.Y. Jets Sweatshirt</Description> 
  <UnitPrice>10.50</UnitPrice> 
  <Quantity>2</Quantity> 
  <TotalCost>21.00</TotalCost> 
- <CustomerOptions>
  <Size>M</Size> 
  <Color>Green</Color> 
  </CustomerOptions>
  </Item>
- <Item>
  <PartNo>JETSWEATER</PartNo> 
  <Description>N.Y. Jets Sweatshirt</Description> 
  <UnitPrice>7.50</UnitPrice> 
  <Quantity>3</Quantity> 
  <TotalCost>22.50</TotalCost> 
- <CustomerOptions>
  <Size>S</Size> 
  <Color>White</Color> 
  </CustomerOptions>
  </Item>
- <Item>
  <PartNo>JETSFLAG</PartNo> 
  <Description>N.Y. Jets Flag for display</Description> 
  <UnitPrice>5.00</UnitPrice> 
  <Quantity>1</Quantity> 
  <TotalCost>5.00</TotalCost> 
  <CustomerOptions /> 
  </Item>
  </Items>
  </Order>
- <Order id="3">
- <BillingInformation>
  <Name>Tom Brady</Name> 
  <Address>One Patriot Place</Address> 
  <City>Foxboro</City> 
  <State>MA</State> 
  <ZipCode>02035</ZipCode> 
  </BillingInformation>
- <ShippingInformation>
  <Name>Tom Brady</Name> 
  <Address>2121 George Halas Drive</Address> 
  <City>Canton</City> 
  <State>OH</State> 
  <ZipCode>44708</ZipCode> 
  </ShippingInformation>
- <Items>
- <Item>
  <PartNo>JETPANTS</PartNo> 
  <Description>N.Y. Jets Sweatpants</Description> 
  <UnitPrice>10.50</UnitPrice> 
  <Quantity>3</Quantity> 
  <TotalCost>31.50</TotalCost> 
- <CustomerOptions>
  <Size>M</Size> 
  <Color>Green</Color> 
  </CustomerOptions>
  </Item>
- <Item>
  <PartNo>JETSWEATER</PartNo> 
  <Description>N.Y. Jets Sweatshirt</Description> 
  <UnitPrice>7.50</UnitPrice> 
  <Quantity>1</Quantity> 
  <TotalCost>7.50</TotalCost> 
- <CustomerOptions>
  <Size>S</Size> 
  <Color>White</Color> 
  </CustomerOptions>
  </Item>
- <Item>
  <PartNo>JETSFLAG</PartNo> 
  <Description>N.Y. Jets Flag for display</Description> 
  <UnitPrice>5.00</UnitPrice> 
  <Quantity>1</Quantity> 
  <TotalCost>5.00</TotalCost> 
  <CustomerOptions /> 
  </Item>
  </Items>
  </Order>
  </OrderFeed>

上面是我正在解析的xml文档。我的目标是用于输入 PartNo,例如“JETSFLAG”是他们传递给应用程序的字符串。我想找到整个 OrderFeed 中的所有订单,并计算 PartNo 特别被购买的次数。我有一个 XPath Navigator 工具,由于某种原因,当我编写应该可以得到我想要的东西的表达式时,它没有返回任何东西。

这是我在评估表达式时得到的结果://OrderFeed/Order/Items//Item//PartNo,它得到了我需要的数据,我需要的只是用户输入的任何特定项目的计数......

  <xml>JETSWEATER JETSWEATER JETSFLASHLIGHT JETSWEATER JETSWEATER JETSFLAG JETPANTS JETSWEATER JETSFLAG</xml> 

现在,我很确定写 count(//OrderFeed/Order/Items//Item//PartNo[JETSFLAG]) 应该会计算 2 个项目。但这就是我得到的回报……

  <xml>0</xml> 

显示时,计数返回 0。有人可以帮忙吗?

【问题讨论】:

    标签: c# asp.net xml xpath


    【解决方案1】:

    尝试路径:

    /OrderFeed/Order/Items/Item[PartNo=JETSFLAG]
    

    (根据您的环境引用)

    【讨论】:

      【解决方案2】:

      现在,我很确定写 count(//OrderFeed/Order/Items//Item//PartNo[JETSFLAG]) 应该会计算 2 个项目。但这就是我得到的回报……

       <xml>0</xml> 
      

      上面的XPath表达式有几个问题

      1. PartNo[JETSFLAG] 表示:“选择任何具有名为 JETSFLAG 的子元素的 PartNo 元素”。但这不是你想要的——你真正想要的:任何PartNo 元素,其字符串值为字符串"JETSFLAG"——所以使用:PartNo[. = 'JETSFLAG']
      2. 不必要地使用 // 缩写。这通常很昂贵,在这种特殊情况下,您只需要在每个位置步骤中选择的元素的子元素 - 因此只需使用 / 运算符。
      3. 您可以省略文档元素的名称,因为根据定义,格式良好的 XML 文档只有一个文档元素。所以,不要写/OrderFeed,而是使用/*

      结合以上所有修复,我们分别得到这两个 XPath 表达式

      /*/Order/Items/Item/PartNo[. = 'JETSFLAG']
      

      count(/*/Order/Items/Item/PartNo[. = 'JETSFLAG'])
      

      前者选择两个想要的PartNo元素。

      后者返回他们的号码: 2

      【讨论】:

        【解决方案3】:

        我尝试了这个 xpath,它似​​乎工作正常。

        count(//OrderFeed/Order/Items/Item[PartNo = 'JETSFLAG'])

        【讨论】:

          【解决方案4】:

          有几个人向您展示了您需要的 XPath 表达式。但要小心注入攻击!您永远不应该通过连接骨架表达式和用户提供的输入来构造 XPath 表达式,而不知道注入风险。理想情况下,如果 XPath API 允许,请使用带有变量的表达式:

          count(//OrderFeed/Order/Items/Item[PartNo = $param])
          

          然后通过 API 提供 $param 的值。

          【讨论】:

            猜你喜欢
            • 2013-05-14
            • 1970-01-01
            • 2017-05-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-07-13
            • 1970-01-01
            相关资源
            最近更新 更多