xPath选择性节点集

时间:2014-12-16 20:38:56

标签: xml xslt xpath xslt-1.0 nodesets

我有一个看起来像这样的文件:

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

现在在xsl中通过执行以下操作很容易获得此节点集:

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

但是我遇到的问题是我必须遇到形成节点集的条件,例如

 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']

因此,例如根据上述规则,我们应该从任何特定的xpath获得此结果:

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

有没有办法用if语句编写一个xpath,可以实现这个或一些xsl代码,可以检查节点集内容,如果找到另一个节点就删除它?

1 个答案:

答案 0 :(得分:1)

  

如果testOne [testTwo =&#39; AAA&#39;]存在,请选择除表格之外的所有节点   testOne [testTwo =&#39; BBB&#39;]

这种情况可以重新说明如下:

选择满足以下任何一项的所有testTwo节点:
1.他们的价值不是BBB&#39;
他们没有一个表兄弟&#34; testTwo节点的值为&#39; AAA&#39;

或者,在另一个表述中:

选择既不满足的所有testTwo节点:
1.他们的价值是BBB&#39;
他们有一个表兄弟&#34; testTwo节点的值为&#39; AAA&#39;

在XPath中可以写成:

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

以下列形式添加另一个谓词:

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

生成一个表达式,在您的示例中将选择:

  <testTwo>AAA</testTwo>
  <testTwo>CCC</testTwo>
  <testTwo>EEE</testTwo>
相关问题