-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?

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
user898465
  • 944
  • 1
  • 12
  • 23
  • What do you mean by "remove"? Variables are immutable in XSLT, so you can't modify the existing variable. You should either narrow the set when you create the variable or narrow it when the variable is used. – Daniel Haley Dec 16 '14 at 23:05
  • I don't understand your conditions esp.the formulation of `if (a) then x AND if (b) then y`. The two are not mutually exclusive. -- Also, why must this be done in XPath, when you could use `xsl:choose`? – michael.hor257k Dec 16 '14 at 23:38

1 Answers1

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>
michael.hor257k
  • 113,275
  • 6
  • 33
  • 51