jmac
jmac

Reputation: 255

Converting three elements from source schema to one element of a target schema in an .XSLT

I have three elements in my source schema: BIRTH_DAY, BIRTH_MONTH, and BIRTH_YEAR. I need these converted into a single element (DOB) in my target schema. The DOB will follow the following 10 character format:

YYYY-MM-DD

How do I do this?

Upvotes: 1

Views: 92

Answers (2)

Daniel Haley
Daniel Haley

Reputation: 52858

Here's one option...

XML Input

<doc>
    <BIRTH_DAY>1</BIRTH_DAY>
    <BIRTH_MONTH>1</BIRTH_MONTH>
    <BIRTH_YEAR>2012</BIRTH_YEAR>
</doc>

XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="doc">
        <DOB>
            <xsl:value-of select="concat(BIRTH_YEAR,'-',
                format-number(BIRTH_MONTH,'00'),'-',
                format-number(BIRTH_DAY,'00'))"/>
        </DOB>
    </xsl:template>
</xsl:stylesheet>

XML Output

<DOB>2012-01-01</DOB>

Edit for different month format.

This is a lot easier in XSLT 2.0, but in 1.0 what I would do is a named template that will return the numerical month. In the example below, the numerical month is returned if the entire month or the first 3 letters of the month are used. It's case insensitive as well.

XML Input

<doc>
    <BIRTH_DAY>1</BIRTH_DAY>
    <BIRTH_MONTH>NOV</BIRTH_MONTH>
    <BIRTH_YEAR>2012</BIRTH_YEAR>
</doc>

XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:variable name="vUpper" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
    <xsl:variable name="vLower" select="'abcdefghijklmnopqrstuvwxyz'"/>

    <xsl:template match="doc">
        <xsl:variable name="vMonth">
            <xsl:choose>
                <xsl:when test="number(BIRTH_MONTH)">
                    <xsl:value-of select="format-number(BIRTH_MONTH,'00')"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:call-template name="getMonth">
                        <xsl:with-param name="pMonth" select="BIRTH_MONTH"/>
                    </xsl:call-template>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <DOB>
            <xsl:value-of select="concat(BIRTH_YEAR,'-',
                $vMonth,'-',
                format-number(BIRTH_DAY,'00'))"/>
        </DOB>
    </xsl:template>

    <xsl:template name="getMonth">
        <xsl:param name="pMonth"/>
        <xsl:variable name="vLCmonth" select="translate(substring($pMonth,1,3),$vUpper,$vLower)"/>
        <xsl:choose>
            <xsl:when test="$vLCmonth='jan'">01</xsl:when>
            <xsl:when test="$vLCmonth='feb'">02</xsl:when>
            <xsl:when test="$vLCmonth='mar'">03</xsl:when>
            <xsl:when test="$vLCmonth='apr'">04</xsl:when>
            <xsl:when test="$vLCmonth='may'">05</xsl:when>
            <xsl:when test="$vLCmonth='jun'">06</xsl:when>
            <xsl:when test="$vLCmonth='jul'">07</xsl:when>
            <xsl:when test="$vLCmonth='aug'">08</xsl:when>
            <xsl:when test="$vLCmonth='sep'">09</xsl:when>
            <xsl:when test="$vLCmonth='oct'">10</xsl:when>
            <xsl:when test="$vLCmonth='nov'">11</xsl:when>
            <xsl:when test="$vLCmonth='dec'">12</xsl:when>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>

XML Output

<DOB>2012-11-01</DOB>

Upvotes: 2

PCM
PCM

Reputation: 873

You can use the CONCAT function.

fn:concat(string1,string2,...)

Here is a good example:

Upvotes: 0

Related Questions