locked
BizTalk -XSLT change date format RRS feed

  • Question

  • I have my date coming as 06/12/2015 and I am using XSLT in my transformation.

    How can I convert this to '20150612' in XSLT ? (please not by call template)


    MBH


    • Edited by JaguarsJag Thursday, June 11, 2015 8:08 PM info
    Thursday, June 11, 2015 8:07 PM

Answers

  • You could also use the out-of-the-box XSLT functionalities and do the following:

           <xsl:value-of select="concat(substring(Date,7,4),substring(Date,1,2),substring(Date,4,2))" />
     

    An example input:

    <Input>
      <Date>06/12/2015</Date>
    </Input>

    Using this XSLT:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:template match="Input">
        <Output>
          <NewDate>
            <xsl:value-of select="concat(substring(Date,7,4),substring(Date,1,2),substring(Date,4,2))" />
          </NewDate>
        </Output>
      </xsl:template>
    </xsl:stylesheet>

    Would give you this output:

    <Output>
      <NewDate>20150612</NewDate>
    </Output>

    Morten la Cour

    Friday, June 12, 2015 4:46 AM
  • Hi Jag,

    Find the attached code , I have made the modification, try to us the same and let us know.

    <?xml version="1.0" encoding="utf-16" ?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:var="http://schemas.microsoft.com/BizTalk/2003/var" exclude-result-prefixes="msxsl var s0" version="1.0" xmlns:s0="http://Brightstar.Integration.Serengeti.Schemas.Invoices" xmlns:ns0="http://Munchein.Grouping" xmlns:userCSharp="http://schemas.microsoft.com/BizTalk/2003/userCSharp">
      <xsl:output omit-xml-declaration="yes" method="xml" version="1.0" />
      <xsl:template match="/">
        <xsl:apply-templates select="/s0:invoices" />
      </xsl:template>
    
    
      <xsl:key name="groups" match="invoice" use="invoice"/>
    
      <xsl:template match="/s0:invoices" >
    
    
        <Root>
          <xsl:for-each select="invoice[generate-id(.)=generate-id(key('groups',invoice))]">
    
            <xsl:sort select="id" order="ascending"/>
    
            <req>
    
              <CpnyId>
                <xsl:value-of select="1011"/>
              </CpnyId>
    
              <!--VendorID-->
              <VendId>
                <xsl:value-of select="id/text()"/>
              </VendId>
    
              <!--InvoiceID-->
              <InvcNbr>
                <xsl:value-of select="invoice/text()"/>
              </InvcNbr>
    
              <!--InvoiceDate-->
              <xsl:variable name="var:v1" select="userCSharp:FormatDateTime(string(@Name))" />
              <InvcDate>
                <xsl:value-of select="$var:v1" />
              </InvcDate>
    
              <xsl:for-each select="invoice">
                <ReqAmt>
                  <xsl:value-of select="sum(amount)"/>
                </ReqAmt>
              </xsl:for-each>
    
    
    
              <!--InvoiceDueDate-->
              <InvoiceDueDate>
                <xsl:value-of select="due/text()"/>
              </InvoiceDueDate>
    
    
    
    
    
    
              <!--<Detail>-->
    
              <xsl:for-each select="key('groups',invoice)">
                <rit>
                  <CpnyId>
                    <xsl:value-of select="company"/>
                  </CpnyId>
    
                  <Acct>
                    <xsl:value-of select="account"/>
                  </Acct>
    
                  <Sub>
                    <xsl:value-of select="subaccount"/>
                  </Sub>
    
                  <TranAmt>
                    <xsl:value-of select="amount"/>
                  </TranAmt>
    
    
                  <Descr>
                    <xsl:value-of select="description"/>
                  </Descr>
    
    
    
                </rit>
    
              </xsl:for-each>
              <!-- </Detail>-->
            </req>
    
          </xsl:for-each>
        </Root>
    
      </xsl:template>
    
      <msxsl:script language="C#" implements-prefix="userCSharp">
        <![CDATA[
    public string FormatDateTime(string pDate)
            {
                DateTime dReturn = new DateTime();
                dReturn = DateTime.Parse(pDate);
                return dReturn.ToString("ddMMyyyy");
            }
    
    
    ]]>
      </msxsl:script>
    </xsl:stylesheet>
    


    Regards, Aboorva Raja R Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, June 12, 2015 4:18 AM

All replies

  • Use one inline xslt  function to do :

    1. Pass this as variable to xslt template to remove "/" as special character. output will be 06122015

    2. pass spc character removed string in to 2 different vaibale using substring function.

      Var1 = substring (0 , 4)

    var2 = Substring ( 5,8)

    3. use concat : Concat ( Var2, Var 1).

    Let me know if you need any more help.

    Friday, June 12, 2015 1:05 AM
  • Hello Swapnil, below is my XSLT (.xsl) file provided in the map. If you can see below in the bold is <InvcDate> which is receiving xml as source. Now how in this xsl file I can convert the date from 06/12/2015 to 20150612.

    If you want I can provide you source (test) xml too ? Please let me know

     <?xml version="1.0" encoding="utf-16" ?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:var="http://schemas.microsoft.com/BizTalk/2003/var" exclude-result-prefixes="msxsl var s0" version="1.0" xmlns:s0="http://Brightstar.Integration.Serengeti.Schemas.Invoices" xmlns:ns0="http://Munchein.Grouping">
      <xsl:output omit-xml-declaration="yes" method="xml" version="1.0" />
      <xsl:template match="/">
        <xsl:apply-templates select="/s0:invoices" />
      </xsl:template>
    
    
      <xsl:key name="groups" match="invoice" use="invoice"/>
    
      <xsl:template match="/s0:invoices" >
      
    
        <Root>
          <xsl:for-each select="invoice[generate-id(.)=generate-id(key('groups',invoice))]">
    
            <xsl:sort select="id" order="ascending"/>
    
            <req>
    
              <CpnyId>
                <xsl:value-of select="1011"/>
              </CpnyId>
              
              <!--VendorID-->
              <VendId>
                <xsl:value-of select="id/text()"/>
              </VendId>
    
               <!--InvoiceID-->
              <InvcNbr>
                <xsl:value-of select="invoice/text()"/>
              </InvcNbr>
    
              <!--InvoiceDate-->
    
              <InvcDate>
                <xsl:value-of select="date"/>
              </InvcDate>
                 
               <xsl:for-each select="invoice">
              <ReqAmt>
                  <xsl:value-of select="sum(amount)"/>
              </ReqAmt> 
                 </xsl:for-each>
                
                  
                     
              <!--InvoiceDueDate-->
              <InvoiceDueDate>
                <xsl:value-of select="due/text()"/>
              </InvoiceDueDate>         
              
    	
              
    
    
    
              <!--<Detail>-->
    
              <xsl:for-each select="key('groups',invoice)">
                <rit>
                  <CpnyId>
                    <xsl:value-of select="company"/>
                  </CpnyId>
    
                  <Acct>
                    <xsl:value-of select="account"/>
                  </Acct>
    
                  <Sub>
                    <xsl:value-of select="subaccount"/>
                  </Sub>
    
                  <TranAmt>
                    <xsl:value-of select="amount"/>
                  </TranAmt>
    
    
                  <Descr>
                    <xsl:value-of select="description"/>
                  </Descr>
               
    
    
                </rit>
    
              </xsl:for-each>
              <!-- </Detail>-->
            </req>
    
          </xsl:for-each>
        </Root>
    
      </xsl:template>
    </xsl:stylesheet>
    
    



    MBH

    Friday, June 12, 2015 1:19 AM
  • Iam stuck here, anybody has any suggestions on how to achieve this scenario will be great help.

    MBH

    Friday, June 12, 2015 3:10 AM
  • Hi Jag,

    Find the attached code , I have made the modification, try to us the same and let us know.

    <?xml version="1.0" encoding="utf-16" ?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:var="http://schemas.microsoft.com/BizTalk/2003/var" exclude-result-prefixes="msxsl var s0" version="1.0" xmlns:s0="http://Brightstar.Integration.Serengeti.Schemas.Invoices" xmlns:ns0="http://Munchein.Grouping" xmlns:userCSharp="http://schemas.microsoft.com/BizTalk/2003/userCSharp">
      <xsl:output omit-xml-declaration="yes" method="xml" version="1.0" />
      <xsl:template match="/">
        <xsl:apply-templates select="/s0:invoices" />
      </xsl:template>
    
    
      <xsl:key name="groups" match="invoice" use="invoice"/>
    
      <xsl:template match="/s0:invoices" >
    
    
        <Root>
          <xsl:for-each select="invoice[generate-id(.)=generate-id(key('groups',invoice))]">
    
            <xsl:sort select="id" order="ascending"/>
    
            <req>
    
              <CpnyId>
                <xsl:value-of select="1011"/>
              </CpnyId>
    
              <!--VendorID-->
              <VendId>
                <xsl:value-of select="id/text()"/>
              </VendId>
    
              <!--InvoiceID-->
              <InvcNbr>
                <xsl:value-of select="invoice/text()"/>
              </InvcNbr>
    
              <!--InvoiceDate-->
              <xsl:variable name="var:v1" select="userCSharp:FormatDateTime(string(@Name))" />
              <InvcDate>
                <xsl:value-of select="$var:v1" />
              </InvcDate>
    
              <xsl:for-each select="invoice">
                <ReqAmt>
                  <xsl:value-of select="sum(amount)"/>
                </ReqAmt>
              </xsl:for-each>
    
    
    
              <!--InvoiceDueDate-->
              <InvoiceDueDate>
                <xsl:value-of select="due/text()"/>
              </InvoiceDueDate>
    
    
    
    
    
    
              <!--<Detail>-->
    
              <xsl:for-each select="key('groups',invoice)">
                <rit>
                  <CpnyId>
                    <xsl:value-of select="company"/>
                  </CpnyId>
    
                  <Acct>
                    <xsl:value-of select="account"/>
                  </Acct>
    
                  <Sub>
                    <xsl:value-of select="subaccount"/>
                  </Sub>
    
                  <TranAmt>
                    <xsl:value-of select="amount"/>
                  </TranAmt>
    
    
                  <Descr>
                    <xsl:value-of select="description"/>
                  </Descr>
    
    
    
                </rit>
    
              </xsl:for-each>
              <!-- </Detail>-->
            </req>
    
          </xsl:for-each>
        </Root>
    
      </xsl:template>
    
      <msxsl:script language="C#" implements-prefix="userCSharp">
        <![CDATA[
    public string FormatDateTime(string pDate)
            {
                DateTime dReturn = new DateTime();
                dReturn = DateTime.Parse(pDate);
                return dReturn.ToString("ddMMyyyy");
            }
    
    
    ]]>
      </msxsl:script>
    </xsl:stylesheet>
    


    Regards, Aboorva Raja R Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, June 12, 2015 4:18 AM
  • You could also use the out-of-the-box XSLT functionalities and do the following:

           <xsl:value-of select="concat(substring(Date,7,4),substring(Date,1,2),substring(Date,4,2))" />
     

    An example input:

    <Input>
      <Date>06/12/2015</Date>
    </Input>

    Using this XSLT:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:template match="Input">
        <Output>
          <NewDate>
            <xsl:value-of select="concat(substring(Date,7,4),substring(Date,1,2),substring(Date,4,2))" />
          </NewDate>
        </Output>
      </xsl:template>
    </xsl:stylesheet>

    Would give you this output:

    <Output>
      <NewDate>20150612</NewDate>
    </Output>

    Morten la Cour

    Friday, June 12, 2015 4:46 AM