Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to create template for display specific information using formulas. xml with xslt

I have come across related dates and am trying to find some algorithm to calculate the dependency between transfer date and exercise date. If our transfer date before exercise date it should convert to new element with value Spot. And if transfer date equal or later then exercise date , convert to new element with value Forward

<main>
    <Exercise>
        <transferDate>2000-08-30</transferDate>
        <exerciseDate>2001-08-28</exerciseDate>
    </Exercise>
    <Exercise>
        <transferDate>2001-08-30</transferDate>
        <exerciseDate>2001-08-28</exerciseDate>
    </Exercise>
    <Exercise>
        <transferDate>2001-08-28</transferDate>
        <exerciseDate>2001-08-28</exerciseDate>
    </Exercise>
</main>
I'm expecting next .xml using xslt
`<?xml version="1.0" encoding="UTF-8"?>
<Type>Spot</Type>
<Type>Forward</Type>
<Type>Forward</Type>`

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You could use something like this :

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="#all"
  version="3.0">

  <xsl:output method="xml" indent="yes"/>

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:template match="Exercise">
    <xsl:copy>
      <xsl:apply-templates/>
      <xsl:variable name="tDate" select="xs:date(transferDate)"/>
      <xsl:variable name="eDate" select="xs:date(exerciseDate)"/>
      <Type>
        <xsl:value-of select="if($tDate lt $eDate)
                                then 'Spot'
                              else if($tDate ge $eDate)
                                then 'Forward'
                              else 'Error'"/>
      </Type>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading