简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM