2

I want to turn a date like this in XSLT 2.0:

<unitdate>September 9, 2018</unitdate>  

to

<unitdate>2018 September 9</unitdate>

or this:

<unitdate>June 1976</unitdate> 

to

<unitdate>1976 June</unitdate>

I don't have any standardized XS dates to start with.

I have an identity transform:

<xsl:template match="node() | @*">
  <xsl:copy>
    <xsl:apply-templates select="node() | @*"/>
  </xsl:copy>
</xsl:template>

And then this is what I have started:

<xsl:template match="//unitdate">
</xsl:template>
zx485
  • 28,498
  • 28
  • 50
  • 59
Karen
  • 35
  • 3
  • XSLT 2.0 has a function for formatting dates, see http://www.sixtree.com.au/articles/2013/formatting-dates-and-times-using-xslt-2.0-and-xpath/ However your input is a string representing a date so you need to parse it as date first. https://stackoverflow.com/questions/16851726/how-to-parse-string-to-date-in-xslt-2-0 – derloopkat Jul 11 '18 at 00:01
  • Start by writing a specification of the date formats that you want your program to accept. Two examples don't make a spec. – Michael Kay Jul 11 '18 at 06:14

1 Answers1

2

Other thing you can do by using xsl:analyze-string how much pattern you have you need to specify but it will be more code but easy:

<xsl:template match="unitdate">
    <unitdate>
        <xsl:analyze-string select="." regex="([a-zA-Z]+) ([0-9][0-9]?),? ([0-9][0-9][0-9][0-9])">
            <xsl:matching-substring>
                <xsl:value-of select="regex-group(3),regex-group(1),regex-group(2)"/>
            </xsl:matching-substring>
            <xsl:non-matching-substring>
                <xsl:analyze-string select="." regex="([a-zA-Z]+) ([0-9][0-9][0-9][0-9])">
                    <xsl:matching-substring>
                        <xsl:value-of select="regex-group(2),regex-group(1)"/>
                    </xsl:matching-substring>
                    <xsl:non-matching-substring>
                        <xsl:value-of select="."/>
                    </xsl:non-matching-substring>
                </xsl:analyze-string>
            </xsl:non-matching-substring>
        </xsl:analyze-string>
    </unitdate>
</xsl:template>
Amrendra Kumar
  • 1,806
  • 1
  • 7
  • 17
  • Yes, that works! Thanks! I was really hung up by the fact that I didn't have an xs:date to start with and so many of the examples started with numbers only. – Karen Aug 02 '18 at 18:05