none
how to write a xsl function in Biztalk custom xslt? Biztalk support xsl version 1.0, so many of the functions are not available. So how to write a function in my custom xslt to add a month in date field? RRS feed

All replies

  • What have you done so far?

    Why exclude C# scripts or static helper classes both are legit solutions and widely used if you are coding for the an msxml parser

    An XSL function is basically a template either called by a name or through a match to an xpath
    https://www.w3schools.com/xml/xsl_templates.asp

    First you need to split the date into it's elements eg year, month, date
    Add 30 days and check the "overflow" compared to current month, increase month and set the day to the overflow

    The code gets huge and ugly 

    Inspiration
    https://stackoverflow.com/questions/10689099/expanding-datetime-ranges-in-xslt-1-0
    https://stackoverflow.com/questions/23566734/date-operations-on-xsl-1-0

    EDIT: Example in the second link: Date is converted to Julian Date format, days are added and then converted back to the Gregorian format
    Abbrevations
    JDN: Julian Date Numeric
    GD: Gregorian Date

    /Peter

    Saturday, June 2, 2018 10:27 AM
  • Eaxample with C# script

    Source

    <Main>
    	<IssueDate>2016-06-02</IssueDate>
    </Main>

    Target

    <html>
      <body>
        <h2>AddDays example C# script</h2>
        <p>2016-07-02</p>
      </body>
    </html>

    Code

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl userCSharp"
    		xmlns:userCSharp="http://schemas.microsoft.com/BizTalk/2003/userCSharp"
    >
        <xsl:output method="xml" indent="yes"/>
    
        <xsl:template match="/">
    			<xsl:variable name="projectedDate">
    				<xsl:value-of select="Main/IssueDate"/>
    			</xsl:variable>
    
    			<xsl:element name="html">
    				<xsl:element name="body">
    					<xsl:element name="h2">
    						<xsl:text>AddDays example C# script</xsl:text>
    					</xsl:element>
    					<xsl:element name="p">
    						<xsl:value-of select="userCSharp:DateAddDays($projectedDate, 30)"/>
    					</xsl:element>
    				</xsl:element>
    			</xsl:element>
    		</xsl:template>
    	
    	<msxsl:script language="C#" implements-prefix="userCSharp">
    		<![CDATA[
    
    //  --------------------------------------------------------------------
    // Add number of dates to given date
    // Returns an empty string if given date parameter is not valid // -------------------------------------------------------------------- public string DateAddDays(string date, double daysToAdd) { DateTime validDate; IFormatProvider provider = System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat; if (System.DateTime.TryParse(date, provider, System.Globalization.DateTimeStyles.AllowWhiteSpaces, out validDate)) { validDate = validDate.AddDays(daystoAdd); return validDate.ToString("yyyy-MM-dd"); } return ""; } ]]> </msxsl:script> </xsl:stylesheet>

    Saturday, June 2, 2018 2:30 PM