XPath 根据以下节点选择节点,但前提是该节点包含某个元素

问题描述 投票:0回答:1

给定这个 XML 文档存根,(我无法修改其生成)...

<Datastream parserApplicationName="mscorlib" parserApplicationVersion="4.0.0.0" parserAssemblyName="Prophet21.Datastream" parserAssemblyVersion="23.1.1.0">
  <JOBXXXXDEF type="1" typeName="JobHeader" key="{a52722bf-c784-4d0a-b80c-d60a55179cb5}" InputFileName="">
   
    <COPIES>1</COPIES>
  
      <HDRXXXXDEF >
        <TITLE>QUOTATION</TITLE>
        <ORDER_ACK_NUMBER>1000998</ORDER_ACK_NUMBER>
       
      </HDRXXXXDEF>

      <LINEXXXDEF lineno ="1" >
        <ORDERED_QTY>1.00</ORDERED_QTY>
        <ORDER_UOM>EA</ORDER_UOM>
      </LINEXXXDEF>

      <LINEXXXDEF lineno ="2">
        <EXDSCXXDEF >
          <EXTENDED_DESCRIPTION>Delivery: 17-20 Weeks</EXTENDED_DESCRIPTION>
        </EXDSCXXDEF>
      </LINEXXXDEF>

      <LINEXXXDEF lineno ="3">
        <ASCMPXXDEF >
          <QTY_PER_ASSEMBLY>1.00</QTY_PER_ASSEMBLY>
        </ASCMPXXDEF>
      </LINEXXXDEF>

      <LINEXXXDEF lineno ="4">
        <EXDSCXXDEF >
          <EXTENDED_DESCRIPTION>Stainless Steel Design</EXTENDED_DESCRIPTION>
        </EXDSCXXDEF>
      </LINEXXXDEF>

      <LINEXXXDEF lineno ="5">
        <ASCMPXXDEF >
          <QTY_PER_ASSEMBLY>1.00</QTY_PER_ASSEMBLY>
        </ASCMPXXDEF>
      </LINEXXXDEF>
    
      <LINEXXXDEF lineno ="6">
        <ORDERED_QTY>1.00</ORDERED_QTY>
        <ORDER_UOM>EA</ORDER_UOM>
      
      </LINEXXXDEF>
      <LINEXXXDEF lineno ="7">
        <EXDSCXXDEF >
          <EXTENDED_DESCRIPTION>Added to the above if required.</EXTENDED_DESCRIPTION>
        </EXDSCXXDEF>
      </LINEXXXDEF>

      <TOTALSXDEF >
        <SUBTOTXDEF >
          <TOTAL_LINES>2</TOTAL_LINES>
          <SUB_TOTAL>1,880.00</SUB_TOTAL>
          <TAXES>0.00</TAXES>
          <TOTAL_ECO_FEE>0.00</TOTAL_ECO_FEE>
          <RETAIL_DELIVERY_FEE>0.00</RETAIL_DELIVERY_FEE>
          <JURISDICTION_DESC />
        </SUBTOTXDEF>
        <GRDTOTXDEF ">
          <GRAND_TOTAL>1,880.00</GRAND_TOTAL>
          <CURRENCY_DESC>U.S. Dollars</CURRENCY_DESC>
        </GRDTOTXDEF>
      </TOTALSXDEF>
    </FORMXXXDEF>
  </JOBXXXXDEF>
</Datastream>

我需要一个或多个 XPath 查询来选择 LINEXXDEF 有一个名为 ASCMPXXDEF 的子节点的整个节点,然后如果该父节点之后的 NEXT LINEXXDEF 有一个名为 EXDSCXXDEF 的子节点,我需要该节点。 所以在上面,我需要这些节点,用于上面的第 3、4 和 5 行

 <LINEXXXDEF lineno ="3">
        <ASCMPXXDEF >
          <QTY_PER_ASSEMBLY>1.00</QTY_PER_ASSEMBLY>
        </ASCMPXXDEF>
      </LINEXXXDEF>

      <LINEXXXDEF lineno ="4">
        <EXDSCXXDEF >
          <EXTENDED_DESCRIPTION>Stainless Steel Design</EXTENDED_DESCRIPTION>
        </EXDSCXXDEF>
      </LINEXXXDEF>

      <LINEXXXDEF lineno ="5">
        <ASCMPXXDEF >
          <QTY_PER_ASSEMBLY>1.00</QTY_PER_ASSEMBLY>
        </ASCMPXXDEF>
      </LINEXXXDEF>

我需要第 3 行,因为它有 ASCMPXXDEF 作为子项,我需要第 4 行,因为它有一个名为 EXDSCXXDEF 的子项,我需要第 5 行,因为它又有一个 ASCMPXXDEF 子项。

我最接近的是这个 XPATH

//ASCMPXXDEF/parent::*/following-sibling::LINEXXXDEF[EXDSCXXDEF][1] | //ASCMPXXDEF/parent::*

但这正在回归

 <LINEXXXDEF lineno ="3">
        <ASCMPXXDEF >
          <QTY_PER_ASSEMBLY>1.00</QTY_PER_ASSEMBLY>
        </ASCMPXXDEF>
      </LINEXXXDEF>

      <LINEXXXDEF lineno ="4">
        <EXDSCXXDEF >
          <EXTENDED_DESCRIPTION>Stainless Steel Design</EXTENDED_DESCRIPTION>
        </EXDSCXXDEF>
      </LINEXXXDEF>

      <LINEXXXDEF lineno ="5">
        <ASCMPXXDEF >
          <QTY_PER_ASSEMBLY>1.00</QTY_PER_ASSEMBLY>
        </ASCMPXXDEF>
      </LINEXXXDEF>

<LINEXXXDEF lineno ="7">
        <EXDSCXXDEF >
          <EXTENDED_DESCRIPTION>Added to the above if required.</EXTENDED_DESCRIPTION>
        </EXDSCXXDEF>
      </LINEXXXDEF>

但它包括第 7 行,这是我不想要的,因为第 7 行不直接跟随包含 ASCMPXXDEF 的 LINEXXXDEF 节点。

有没有办法让 XPATH 仅评估下一个同级? 本质上我得到了第 7 行,因为第 5 行是一个 ASCMPXXDEF 节点,但如果它直接位于第 6 行之后,我只需要它。第 6 行是我的评估过程的重新启动...但 XPATH 会跳过它,但因为第 7 行在第 5 行之后,所以它被包含在内。

我也在使用 C# 工作,如果有更简单的操作方法...

我正在尝试找出如何删除包含 EXDSCXXDEF 但紧随包含 ASCMPXXDEF 的节点之后的任何节点

<LINEXXXDEF lineno ="3">
        <ASCMPXXDEF >
          <QTY_PER_ASSEMBLY>1.00</QTY_PER_ASSEMBLY>
        </ASCMPXXDEF>
      </LINEXXXDEF>

      <LINEXXXDEF lineno ="4">
        <EXDSCXXDEF >
          <EXTENDED_DESCRIPTION>Stainless Steel Design</EXTENDED_DESCRIPTION>
        </EXDSCXXDEF>
      </LINEXXXDEF>

      <LINEXXXDEF lineno ="5">
        <ASCMPXXDEF >
          <QTY_PER_ASSEMBLY>1.00</QTY_PER_ASSEMBLY>
        </ASCMPXXDEF>
      </LINEXXXDEF>

*this node below should not be included
<LINEXXXDEF lineno ="7">
        <EXDSCXXDEF >
          <EXTENDED_DESCRIPTION>Added to the above if required.</EXTENDED_DESCRIPTION>
        </EXDSCXXDEF>
      </LINEXXXDEF>*
c# xml xpath
1个回答
0
投票

下面的XPath将会得到你想要的。

/Datastream/JOBXXXXDEF/LINEXXXDEF[
  ASCMPXXDEF 
  or (EXDSCXXDEF and preceding-sibling::LINEXXXDEF[1][ASCMPXXDEF ])
]

点网小提琴

© www.soinside.com 2019 - 2024. All rights reserved.