Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
456 views
in Technique[技术] by (71.8m points)

xml - Formatting Date Value Using xslt

How can I format the below XML date element in XSLT version1.0 ? The GenerationTime has to be formatted in pattern yyyy-MM-dd'T'HH:mm:ss in the output XML.

<Values>
  <value name="GenerationTime">Tue Dec 29 14:32:53 UTC 2020</value>
<Values>

Here is what I found from my product's (Intergation Server) documentation

When executed, the XSLT service calls an external XSLT engine to convert the XML data. The external XSLT engine must be Java API for XML Processing (JAXP)-compatible. By default, Integration Server includes the Xerces parser and the Xalan XSLT style sheet processor from the Apache Software Foundation.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Xalan supports the EXSLT str:tokenize() extension function, so you could do something like:

<xsl:template name="reformat-date">
    <xsl:param name="input"/>
    <!-- extract dateTime components -->
    <xsl:variable name="tokens" select="str:tokenize($input, ' ')"/>
    <xsl:variable name="mmm" select="$tokens[2]"/>
    <xsl:variable name="day" select="$tokens[3]"/>
    <xsl:variable name="time" select="$tokens[4]"/>
    <xsl:variable name="year" select="$tokens[6]"/>
    <!-- convert MMM to month number -->
    <xsl:variable name="month" select="string-length(substring-before('JanFebMarAprMayJunJulAugSepOctNovDec', $mmm)) div 3 + 1" />
    <!-- output -->
    <xsl:value-of select="$year"/>
    <xsl:value-of select="format-number($month, '-00')"/>
    <xsl:value-of select="format-number($day, '-00')"/>
    <xsl:text>T</xsl:text>
    <xsl:value-of select="$time"/>
</xsl:template>

Demo: http://xsltransform.net/6qCcdd5


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...