仅通过 XPath 计算特定的孩子 [英] Count specific children only through XPath

查看:22
本文介绍了仅通过 XPath 计算特定的孩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况,只有当它们为真时,我才必须找到少数布尔字段值的计数.

I have a situation where I have to find the count of few Boolean field values only if they are true.

输入 XML:

<PersonInfo>
  <ArrayOfPersonInfo>
   <CertAsAdultFlag>true</CertAsAdultFlag>
    <DeceasedFlag>true</DeceasedFlag>
    <WantedFlag>false</WantedFlag>
    <CPSORFlag>true</CPSORFlag>
    <ConditonalReleaseFlag>false</ConditonalReleaseFlag>
    <ProbationFlag>true</ProbationFlag>
    <MissingFlag>true</MissingFlag>
    <ATLFlag>true</ATLFlag>
    <CCWFlag>false</CCWFlag>
    <VictimIDTheftFlag>true</VictimIDTheftFlag>
  </ArrayOfPersonInfo>    
</PersonInfo>

如果这些标志为 'true',我需要找到这些标志的数量.

I need to find the count of these flags with the condition if they are 'true'.

以下是我尝试但未成功的方法:

<xsl:variable name="AlertCount" select="
  count(
    PersonInfo/ArrayOfPersonInfo[
      CPSORFlag[.='true'] | CertAsAdultFlag[.='true'] | 
      DeceasedFlag[.='true'] | WantedFlag[.='true'] | 
      ConditonalReleaseFlag[.='true'] | MissingFlag[.='true'] | 
      ATLFlag[.='true'] | ProbationFlag[.='true'] | CCWFlag[.='true'] | 
      VictimIDTheftFlag[.='true'] | CHRIFlag[.='true'] | 
      CivilWritFlag[.='true'] | MentalPetitionFlag[.='true'] |
      ProtectionOrderFlag[.='true'] | juvWantedFlag[.='true'] | 
      WeaponsFlag[.='true'] | WorkCardFlag[.='true']
    ]
  )
"/> 

我真的需要有人帮助我解决这个问题,因为我一直在努力解决这个问题.提前致谢.

I really need help with this from someone as I've been trying hard to get through it. Thanks in advance.

推荐答案

<xsl:variable name="AlertCount" select="count(PersonInfo//*[. = 'true'])" /> 

这就是为什么你的不起作用:

Here's why your's does not work:

您方法中的方括号在节点集上创建谓词.

The square brackets in your approach create a predicate over a node set.

该节点集是所有提到的满足其条件的子节点的联合.非空节点集计算结果为 true,非空节点集计算结果为 false.

That node-set was the union of all mentioned child nodes who fulfilled their condition. A non-empty node-set evaluates to true, a non-empty one to false.

因此您的 count() 将始终为 1 如果任何子节点为真,并且始终为 0 如果所有子节点都为真错误的.

In consequence your count() would always be 1 if any of the children were true and always be 0 if all of them were false.

换句话说:您选择了一个 节点.如果它满足一个条件(具有任意数量的 'true' 作为其值的孩子),它就会被计算在内,否则不会.

In other words: You selected one <ArrayOfPersonInfo> node. If it fulfilled a condition (having any number of children with 'true' as their value) it was counted, otherwise not.

在评论中澄清之后(我只需要担心我在上面 XML 中提到的标志"):

After clarification in the comments ("I need to worry only about the flags I mentioned in the above XML"):

<xsl:variable name="AlertCount" select="
  count(
    PersonInfo//*[
      self::CPSORFlag or
      self::CertAsAdultFlag or
      self::DeceasedFlag or
      self::WantedFlag or
      self::ConditonalReleaseFlag or
      self::MissingFlag or
      self::ATLFlag or
      self::ProbationFlag or
      self::CCWFlag or
      self::VictimIDTheftFlag or
      self::CHRIFlag or
      self::CivilWritFlag or
      self::MentalPetitionFlag or
      self::ProtectionOrderFlag or
      self::juvWantedFlag or
      self::WeaponsFlag or
      self::WorkCardFlag
    ][. = 'true']
  )
" />

这篇关于仅通过 XPath 计算特定的孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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