XPath to select any child without a specific value for a specific attribute

I am trying to create an XPath query that basically selects everything but excludes certain nodes.

This is the XML I'm looking at:

<?xml version="1.0" encoding="UTF-8"?>

<task>
  <title id="30014">Instructions</title>
  <taskbody>
    <context>
      <p>Your box has a document.</p>
      <p audience="print">To get the document:</p>
      <p audience="web">
        <xref href="/node/6308" scope="external">Click here</xref> to get the document.
      </p>
    </context>
    <steps audience="print">
      <step>
        <cmd>Go to 
          <u>www.google.com</u>.
        </cmd>
      </step>
      <step>
        <cmd>Click on the "Resource" button.</cmd>
        <info>
          <fig frame="all">
            <image href="resource.ai" height="1.650in" width="4.500in"/>
          </fig>
        </info>
      </step>
      <step>
        <cmd>Click on "Manuals".</cmd>
      </step>
      <step>
        <cmd>Click on "Shipping".</cmd>
      </step>
      <step>
        <cmd>You can save or print it from your browser window.</cmd>
      </step>
    </steps>
  </taskbody>
</task>

I need to select everything inside the one where the audience is not equal to "print".

I tried all sorts of ways that I read about, but no one seems to work exactly the way I did it.

This is the last thing, almost everything, but not quite:

task/taskbody//*[not(@audience = "print")]

The problem is that it does a great job of removing the level 1 nodes down, which have the meaning of "print." However, the first <p>, which has the meaning of "print," is inside <context>. This node never seems selected.

Here is the result of the query:

<?xml version="1.0" encoding="UTF-8"?>
<result>
<context>
      <p>Your box has a document.</p>
      <p audience="print">To get the document:</p>
      <p audience="web">
        <xref href="/node/6308" scope="external">Click here</xref> to get the document.
      </p>
    </context>

<p>Your box has a document.</p>

<p audience="web">
        <xref href="/node/6308" scope="external">Click here</xref> to get the document.
      </p>

<xref href="/node/6308" scope="external">Click here</xref>

<step>
        <cmd>Go to 
          <u>www.google.com</u>.
        </cmd>
      </step>

<cmd>Go to 
          <u>www.google.com</u>.
        </cmd>

<u>www.google.com</u>

<step>
        <cmd>Click on the "Resource" button.</cmd>
        <info>
          <fig frame="all">
            <image height="1.650in" href="resource.ai" width="4.500in"/>
          </fig>
        </info>
      </step>

<cmd>Click on the "Resource" button.</cmd>

<info>
          <fig frame="all">
            <image height="1.650in" href="resource.ai" width="4.500in"/>
          </fig>
        </info>

<fig frame="all">
            <image height="1.650in" href="resource.ai" width="4.500in"/>
          </fig>

<image height="1.650in" href="resource.ai" width="4.500in"/>

<step>
        <cmd>Click on "Manuals".</cmd>
      </step>

<cmd>Click on "Manuals".</cmd>

<step>
        <cmd>Click on "Shipping".</cmd>
      </step>

<cmd>Click on "Shipping".</cmd>

<step>
        <cmd>You can save or print it from your browser window.</cmd>
      </step>

<cmd>You can save or print it from your browser window.</cmd>

</result>

, "" "", .

?

+4
1

, @audience, , , print:

//*[not(descendant::*[@audience='print']) and not(ancestor-or-self::*[@audience='print'])]

, <title>, <p> <context>. <steps> <p>, audience, print.

( taskbody), :

//task/taskbody//*[not(descendant::*[@audience='print']) and not(ancestor-or-self::*[@audience='print'])] 
+1

Source: https://habr.com/ru/post/1545920/


All Articles