-2

I have a document that looks something like this:

<test>
 <testOne>
  <testTwo>AAA</testTwo>
 <testOne>
 <testOne>
  <testTwo>BBB</testTwo>
 <testOne>
 <testOne>
  <testTwo>CCC</testTwo>
 <testOne>
 <testOne>
  <testTwo>DDD</testTwo>
 <testOne>
 <testOne>
  <testTwo>EEE</testTwo>
 <testOne>
</test>

Now in xsl it's easy to get this nodeset by doing:

<xsl:variable name="testOnes" select="//test/testOne">

However the issue I have is that there are conditions I have to meet to form the nodeset, such as

 if testOne[testTwo='AAA'] exists select all nodes except those of form testOne[testTwo='BBB']
and if testOne[testTwo='CCC'] exists then select all nodes except those of form testOne[testTwo='DDD']

So for example according to those rules above we should get this result from any specific xpath:

<test>
 <testOne>
  <testTwo>AAA</testTwo>
 <testOne>
 <testOne>
  <testTwo>CCC</testTwo>
 <testOne>
 <testOne>
  <testTwo>EEE</testTwo>
 <testOne>
</test>

Is there any way of writing an xpath with an if statement in that can achieve this or some xsl code that can check the nodesets contents and remove one node if it finds another?

4

1 回答 1

1

if testOne[testTwo='AAA'] exists select all nodes except those of form testOne[testTwo='BBB']

This condition could be re-stated as follows:

Select all testTwo nodes that satisfy either:
1. their value is not 'BBB'
2. they do not have a "cousin" testTwo node with a value of 'AAA'

Or, in another formulation:

Select all testTwo nodes that satisfy neither:
1. their value is 'BBB'
2. they have a "cousin" testTwo node with a value of 'AAA'

which in XPath could be written as:

//testTwo[not(.='BBB' and ../../testOne/testTwo='AAA')]

Adding another predicate in the form of:

//testTwo[not(.='BBB' and ../../testOne/testTwo='AAA')]
         [not(.='DDD' and ../../testOne/testTwo='CCC')]

produces an expression that in your example would select:

  <testTwo>AAA</testTwo>
  <testTwo>CCC</testTwo>
  <testTwo>EEE</testTwo>
于 2014-12-16T23:57:36.650 回答