How do I add conditions to XSLT if statement

Here should go questions about transforming XML with XSLT and FOP.
winkimjr2
Posts: 62
Joined: Thu Jun 26, 2014 9:00 pm

How do I add conditions to XSLT if statement

Post by winkimjr2 » Wed Aug 07, 2019 4:45 pm

How do I check TimeStampChange node is the only node with Op code
AND in CaseEvent there are no Op codes on any other child nodes? i.e. CaseEvent\TimeStampChange Op="E" and there are no Op codes on any other child elements.

If statement will be True when TimeStampChange is the only node in CaseEvent that have Op code.
If any other node have an Op code, then the If statement will be False

Here is the xml that I am reading

Code: Select all

<Case>
   <CaseEvent Op="E">
      <RevDate Op="E">08/01/2019</RevDate>
      <CompDate Op="E">08/01/2019</CompDate>
      <TimestampChange Op="E">08/01/2019 14:07:15:690</TimestampChange>
   </CaseEvent>
</Case>
Here is my xslt code. I need help adding and condition to also check if any other child nodes in CaseEvent have Op code

Code: Select all

<xsl:if test="((TimestampChange[@Op='E']) and ())">True</xsl:if>

adrian
Posts: 2580
Joined: Tue May 17, 2005 4:01 pm

Re: How do I add conditions to XSLT if statement

Post by adrian » Thu Aug 08, 2019 10:35 am

Hi,
If statement will be True when TimeStampChange is the only node in CaseEvent that have Op code.

Code: Select all

not(*[name() != 'TimestampChange']/@Op) and TimestampChange/@Op
Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com

winkimjr2
Posts: 62
Joined: Thu Jun 26, 2014 9:00 pm

Re: How do I add conditions to XSLT if statement

Post by winkimjr2 » Fri Aug 09, 2019 7:04 am

Hi Adrian, I was also asked to return true when CompDate has @Op="E" or "A". So the above XML should return false because TimeStampChange has an @Op="E" and CompDate has @Op="E".
I tried the following but it is returning true instead of false. Please help.

Code: Select all

<xsl:if test="(@Op='E') and (((CompDate[@Op='A']) or (CompDate[@Op='E'])) or (TimestampChange[@Op='E'] and count(*[@Op])=1))">True</xsl:if>

adrian
Posts: 2580
Joined: Tue May 17, 2005 4:01 pm

Re: How do I add conditions to XSLT if statement

Post by adrian » Fri Aug 09, 2019 1:03 pm

Try:

Code: Select all

(@Op='E') and (not(TimestampChange[@Op='E']) and ((CompDate[@Op='A']) or (CompDate[@Op='E'])) or (TimestampChange[@Op='E'] and count(*[@Op])=1))
Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com

winkimjr2
Posts: 62
Joined: Thu Jun 26, 2014 9:00 pm

Re: How do I add conditions to XSLT if statement

Post by winkimjr2 » Fri Aug 09, 2019 3:15 pm

Thank you so much Adrian. I spent 3 days on this and your code is now working as expected. you have no idea how happy and satisfied I am right now! You are a miracle worker.

Post Reply