omer khalid
omer khalid

Reputation: 895

Datatype Conversion in XSLT 2.0

I am performing XSLT transformation on some input XML which is generated from a FlatFile. I want to change the datatype of a element to "date" but without showing it in output XML.

Input XML:

<PostingDate>20141009</PostingDate>

XSLT Transformation:

<cdm:PostingDate>
    <xsl:attribute name="name"> 
       <xsl:value-of select="PostingDate"/>
    </xsl:attribute>
    <xsl:attribute name="type">xs:decimal</xsl:attribute>
</cdm:PostingDate>

Current Output:

<cdm:PostingDate name="20141009" type="xs:decimal"/>

Required Output:

<cdm:PostingDate>2014-10-09</cdm:PostingDate>

Note: Similarly I want to do some other transformations like to convert some XML elements into decimal and strings. Is it possible in XSLT 2.0?

Upvotes: 1

Views: 704

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117140

I want to change the data type of a element to "date" but without showing it in output xml.

That's a rather meaningless wish, since the output does not carry the data type with it, and you don't need the data type during the processing (at least I don't see that you do). Why don't you do simply:

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

Edit:

what i meant to say was that it is a some sort of pipe.

AFAIK, if you do:

<cdm:PostingDate>
    <xsl:sequence select="xs:date(concat(substring(PostingDate, 1, 4), '-', substring(PostingDate, 5, 2), '-', substring(PostingDate, 7, 2)))"/>
</cdm:PostingDate>

then the data type of <cdm:PostingDate> will remain xs:date until the output is serialized.

Upvotes: 2

Related Questions