我在下面的数据中,我试图选择除RejectedRecords及其所有子项之外的所有节点。

<?xml version="1.0" encoding="UTF-8"?>
<inmsg>
  <BPDATA>
    <DATE_TIME>10072014084945</DATE_TIME>
  </BPDATA>
  <Orders>
    <Rejected>
      <RejectedRecords>
        <RecordNumber>1</RecordNumber>
        <RecordError>State Code is invalid</RecordError>
      </RejectedRecords>
      <RejectedRecords>
        <RecordNumber>2</RecordNumber>
        <RecordError>State Code is invalid</RecordError>
      </RejectedRecords>
      <FileName>Foo1.txt</FileName>
      <MessageType>Rejected</MessageType>
      <RecordCount>2</RecordCount>
      <TotalAmount>1050.01</TotalAmount>
    </Rejected>
    <Unrestricted>
      <FileName>Foo2.txt</FileName>
      <MessageType>UnrestrictedState</MessageType>
      <RecordCount>2</RecordCount>
      <TotalAmount>100.10</TotalAmount>
    </Unrestricted>
  </Orders>
  <PrimaryDocument SCIObjectID="6442821469081a3a3node1"/>
</inmsg>

我已经尝试了许多声明,例如
//*/node()[not(parent::RejectedRecords) and not(self::RejectedRecords) and not(self::RecordNumber) and not(self::RecordError)]
//*[not(parent::RejectedRecords) and not(self::RejectedRecords)]
//*[not(descendant-or-self::RejectedRecords)]

不管我用什么,我仍然会得到RejectedRecords节点及其子节点,因为它与Rejected节点一起出现。我究竟做错了什么?

最佳答案

此表达式选择所有节点,但不将所有RejectedRecords或以RejectedRecords作为祖先的节点包括在结果集中:

//*[not(descendant::RejectedRecords) and not(ancestor-or-self::RejectedRecords)]

这是XPath Tester中结果的链接

10-05 19:40