如何选择多个节点的值并在单个条件下对其进行测试? [英] How to pick value of multiple nodes and test it in a single condition?

查看:23
本文介绍了如何选择多个节点的值并在单个条件下对其进行测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

继续这个post,我已经重组了代码以更好地理解我的需求.我还提供了更多细节.

In continuation to this post, I have restructured the code to better understand my requirement. I have also provided more detail to it.

XML 文件

<Person>
  <Name>Kavya</Name>
  <Gift>
    <ItemName>PD1</ItemName>
  </Gift>
  <Gift>
    <ItemName>PS1</ItemName>
  </Gift>
  <Gift>
    <ItemName>PD2</ItemName>
  </Gift>
</Person>

现在,在上述结构中,仅当 Person 具有 GiftItemName 时,我才需要返回文本 Successfull 或只有 PS1 而没有 PD1PD2.因此,在编写 XSLT 文件时,我使用了以下方法:

Now, In the above structure, I need to return a text Successfull only when the Person has a Gift with ItemName that is empty or has only PS1 and not PD1 or PD2. So, when writing an XSLT file, I used the below approach:

XSLT 文件(示例)

<Test>
  <xsl:choose>
    <xsl:when test="//Person/Gift[ItemName='PS1' and ItemName!='PD1']">
      <xsl:text>Successfull</xsl:text>
    </xsl:when>
  </xsl:choose>
</Test>

截至目前,它返回成功,因为其中一个礼物的 ItemName 为PS1".但是,根据我的需要,它不能显示任何内容,因为虽然有一个 ItemName 为 'PS1',但该人仍然拥有 ItemName 为 'PD1' 和 'PD2' 的礼物

As of now, it returns Successfull, since one of the Gifts has ItemName as 'PS1'. But, according to my need, it must not display anything since, although there is an ItemName as 'PS1', the person yet has gifts with ItemName as 'PD1' and 'PD2'

准确地说,我不想检查 Gift 中的单个 ItemName.我试图检查这个人(作为一个整体)是否有 PS1 而不是 PD1 或 PD2

To be precise, I don't want to check individual ItemName in Gift. I am trying to check if the Person ( as a whole ) has PS1 and not PD1 or PD2

希望这很清楚.请协助.

Hoping this is clear. Kindly assist.

推荐答案

在 XSLT 2.0 中,你可以使用 exists():

In XSLT 2.0, you could use exists():

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/Person">
    <xsl:choose>
      <xsl:when test="./Gift[ItemName='PS1'] and not(exists(./Gift[ItemName='PD1']))">
        <xsl:text>Sample</xsl:text>
      </xsl:when>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>

这篇关于如何选择多个节点的值并在单个条件下对其进行测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆