Home > Software design >  How to create template for display specific information using formulas. xml with xslt
How to create template for display specific information using formulas. xml with xslt

Time:12-10

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>`

CodePudding user response:

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>
  • Related