user1800502
user1800502

Reputation: 3

check an element in XML using XSL

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>[email protected]</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>[email protected]</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?

Upvotes: 0

Views: 1125

Answers (1)

Tim C
Tim C

Reputation: 70648

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.

Upvotes: 2

Related Questions