Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8795971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:30:43+00:00 2026-06-13T23:30:43+00:00

So I want to use an XSL to format a date before its sent

  • 0

So I want to use an XSL to format a date before its sent through a Web Service but I also want to run a check that if the DOB field is blank it remains blank and if it has a date in it, it will go ahead an format the date.

Now I have got the XSL to format the date – however I can’t seem to get the condition working that if its empty leave it empty

XML before going through XSL:

<BriefDetails>
    <ObjectID>
        <flt:ObjectType>C1</flt:ObjectType>
    </ObjectID>
</BriefDetails>
<Name>
    <Title>Dr</Title>
    <Forename>Paul</Forename>
    <Surname>Smith</Surname>
    <flt:Preferred>true</flt:Preferred>
</Name>
<ContactEmails>
    <EmailAddress>mail@mail.com</EmailAddress>
    <flt:Preferred>true</flt:Preferred>
</ContactEmails>
<DateOfBirth />
<ContactPostals>
    <AddressNumber>34</AddressNumber>
    <AddressLine>Hopeless Road</AddressLine>
    <AddressLine>Maghera</AddressLine>
    <Postcode>BT74 2TY</Postcode>
    <flt:Preferred>true</flt:Preferred>
</ContactPostals>
<ContactPhones>
    <Number>5545454</Number>
    <flt:Preferred>false</flt:Preferred>
</ContactPhones>
<ContactPhones>
    <Number>5454545</Number>
    <flt:Preferred>false</flt:Preferred>
</ContactPhones>

XML after XSL:

<BriefDetails>
    <ObjectID>
        <flt:ObjectType>C1</flt:ObjectType>
    </ObjectID>
</BriefDetails>
<Name>
    <Title>Dr</Title>
    <Forename>Paul</Forename>
    <Surname>Smith</Surname>
    <flt:Preferred>true</flt:Preferred>
</Name>
<ContactEmails>
    <EmailAddress>mail@mail.com</EmailAddress>
    <flt:Preferred>true</flt:Preferred>
</ContactEmails>
***<DateOfBirth>--</DateOfBirth>***
<ContactPostals>
    <AddressNumber>34</AddressNumber>
    <AddressLine>Hopeless Road</AddressLine>
    <AddressLine>Maghera</AddressLine>
    <Postcode>BT74 2TY</Postcode>
    <flt:Preferred>true</flt:Preferred>
</ContactPostals>
<ContactPhones>
    <Number>5545454</Number>
    <flt:Preferred>false</flt:Preferred>
</ContactPhones>
<ContactPhones>
    <Number>5454545</Number>
    <flt:Preferred>false</flt:Preferred>
</ContactPhones>

Above you will see the DOB as —

XSL being used:

<xsl:template match="DateOfBirth">
    <xsl:element name="DateOfBirth">
        <xsl:call-template name="formatDate">
            <xsl:with-param name="dateParam" select="." />
        </xsl:call-template>
    </xsl:element>
</xsl:template>

<xsl:template name="formatDate">
    <xsl:param name="dateParam" />
    <!-- input format mm/dd/yyyy or m/d/yyyy -->
    <!-- output format yyyy-mm-dd -->

    <!-- parse out the day, month and year -->
    <xsl:variable name="day">
        <xsl:value-of select="substring-before($dateParam,'/')" />
    </xsl:variable>
    <xsl:variable name="month">
        <xsl:value-of select="substring-before(substring-after($dateParam,'/'),'/')" />
    </xsl:variable>
    <xsl:variable name="year">
        <xsl:value-of select="substring-after(substring-after($dateParam,'/'),'/')" />
    </xsl:variable>

    <!-- now print them out. Pad with 0 where necessary. -->
    <xsl:value-of select="$year" />
    <xsl:value-of select="'-'" />
    <xsl:if test="string-length($month) = 1">
        <xsl:value-of select="'0'" />
    </xsl:if>
    <xsl:value-of select="$month" />
    <xsl:value-of select="'-'" />
    <xsl:if test="string-length($day) = 1">
        <xsl:value-of select="'0'" />
    </xsl:if>
    <xsl:value-of select="$day" />
</xsl:template>

I have tried adding in a condition:

<xsl:template match="DateOfBirth">
    <xsl:for-each select="DateOfBirth">
        <xsl:if test="string-length(DateOfBirth) != 0">
            <xsl:call-template name="formatDate">
                <xsl:with-param name="dateParam" select="DateOfBirth" />
            </xsl:call-template>
        </xsl:if>
    </xsl:for-each>
</xsl:template>

<xsl:template name="formatDate">
    <xsl:param name="dateParam" />
    <!-- input format mm/dd/yyyy or m/d/yyyy -->
    <!-- output format yyyy-mm-dd -->

    <!-- parse out the day, month and year -->
    <xsl:variable name="day">
        <xsl:value-of select="substring-before($dateParam,'/')" />
    </xsl:variable>
    <xsl:variable name="month">
        <xsl:value-of select="substring-before(substring-after($dateParam,'/'),'/')" />
    </xsl:variable>
    <xsl:variable name="year">
        <xsl:value-of select="substring-after(substring-after($dateParam,'/'),'/')" />
    </xsl:variable>

    <!-- now print them out. Pad with 0 where necessary. -->
    <xsl:value-of select="$year" />
    <xsl:value-of select="'-'" />
    <xsl:if test="string-length($month) = 1">
        <xsl:value-of select="'0'" />
    </xsl:if>
    <xsl:value-of select="$month" />
    <xsl:value-of select="'-'" />
    <xsl:if test="string-length($day) = 1">
        <xsl:value-of select="'0'" />
    </xsl:if>
    <xsl:value-of select="$day" />
</xsl:template>

but this now strips out the DOB in the XML

Any advice on how I could achieve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:30:45+00:00Added an answer on June 13, 2026 at 11:30 pm

    The problem is with your IF condition

    <xsl:if test="string-length(DateOfBirth) != 0">
    

    You are already positioned on the DateOfBirth element at this point, so this is actually looking for a child element called DateOfBirth in the current element, rather than the value of the current element itself.

    <xsl:if test="string-length(.) != 0">
    

    Actually, you also seem to have added an unnecessary xsl:for-each too. Your template should really look like this

    <xsl:template match="DateOfBirth">
        <DateOfBirth>
            <xsl:if test="string-length(.) != 0">
                <xsl:call-template name="formatDate">
                    <xsl:with-param name="dateParam" select="." />
                </xsl:call-template>
            </xsl:if>
        </DateOfBirth>
    </xsl:template>
    

    As a side note, there is no need for using xsl:element for a static element name, just write out the element directly.

    In fact, there is another way to do this without the need for the xsl:if. You can have the condition within the template match

    <xsl:template match="DateOfBirth[string-length(.) != 0]">
        <DateOfBirth>
            <xsl:call-template name="formatDate">
                 <xsl:with-param name="dateParam" select="." />
            </xsl:call-template>
        </DateOfBirth>
    </xsl:template>
    
    <xsl:template match="DateOfBirth">
        <DateOfBirth />
    </xsl:template>
    

    XSLT will also match the more specific template first, so the second template will only be matched when DateOfBirth is empty. Further more, if you were using the identity transform in your XSLT, you wouldn’t actually need the second template at all.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want use some data from a website with web service. i have a
I want to use if in xsl to compare two values, but i want
I want use BYTE_ORDER macro in my Xcode project but i can't because i
I want use JQuery mobile for the front-end of my mobile application, but I
We have a powerbuilder application and we want use a scanner through this application
can i use xsl or another thing similar to change this format <rr n=Address>
I want to use XSL to remove some elements from a tree. Suppose I
I want to use XSL to convert XML to another XML The input xml
I want use localized strings from resources in xsl template as in aspx page,
I have an XSLT like below, and want to use apply-templates inside the xsl:for-each

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.