Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
280 views
in Technique[技术] by (71.8m points)

IF Condition in XSLT by comparing the count of sub node in parent node

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

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>

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...