通过比较父节点中子节点的计数,XSLT 中的 IF 条件 [英] IF Condition in XSLT by comparing the count of sub node in parent node

查看:20
本文介绍了通过比较父节点中子节点的计数,XSLT 中的 IF 条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据节点数执行循环 A 或循环 B

I am trying to perform loop A or Loop B based on the count of the nodes

 empjobcount -> "count(employmentNav/EmpEmployment/compInfoNav/EmpCompensation/startDate)"  
 compcount -> "count(employmentNav/EmpEmployment/jobInfoNav/EmpJob/startDate)"

如何将 count 传递给参数以执行 if 条件.以下是导致语法错误.

how to pass count to the parameter in order to perform if condition. Below is causing syntax error.

<xsl:if test= {"count(employmentNav/EmpEmployment/compInfoNav/EmpCompensation/startDate)" ge "count(employmentNav/EmpEmployment/jobInfoNav/EmpJob/startDate)"} >
</xsl:if>

伪代码:如果 empjobcount > compcount.循环A.别的.循环 B.万一.

Pseudocode: if empjobcount > compcount. loopA. else. loopB. endif.

推荐答案

去掉花括号,即使用 test="count(employmentNav/EmpEmployment/compInfoNav/EmpCompensation/startDate) >= count(employmentNav/EmpEmployment/jobInfoNav/EmpJob/startDate)".我还认为,如果您确实需要执行两个不同的代码分支,您需要 xsl:choose/xsl:when test/xsl:otherwise 而不是单个 xsl:if.

Drop the curly braces, i.e. use test="count(employmentNav/EmpEmployment/compInfoNav/EmpCompensation/startDate) >= count(employmentNav/EmpEmployment/jobInfoNav/EmpJob/startDate)". I also think you need xsl:choose/xsl:when test/xsl:otherwise instead of a single xsl:if if you really need to execute two different branches of code.

由于您已经为 XSLT 2.0 和 XSLT 3.0 标记了您的问题,因此可以使用 if(条件表达式) then expression1 else expression2 表达式 eg

As you have tagged your question for XSLT 2.0 and XSLT 3.0 it might be possible to solve things at the XPath expression level with the if (conditional expression) then expression1 else expression2 expression e.g.

if (count(employmentNav/EmpEmployment/compInfoNav/EmpCompensation/startDate) >= count(employmentNav/EmpEmployment/jobInfoNav/EmpJob/startDate)) then mf:foo() else mf:bar()

其中 mf:foomf:bar 是使用 xsl:function 设置的用户定义函数.

where mf:foo and mf:bar would be user-defined functions set up with xsl:function.

关于循环的整个描述听起来好像您尝试将过程编程与 XSLT 一起使用,您可能希望向我们展示 XML 输入、输出的小而有代表性的示例以及您想要实现的规则,以便我们也许可以建议更像 XSLT 的方法.

The whole description with talk about loops sounds as if you try to use procedural programming with XSLT, you might want to show us small but representative samples of XML input, output you want together with the rules you want to implement so that we might be able to suggest a more XSLT like approach.

这篇关于通过比较父节点中子节点的计数,XSLT 中的 IF 条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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