I am trying to transform one xml by changing 2 date fields.
Here is my source XML:
<?xml version="1.0" encoding="UTF-8"?>
<Publication_MarketDocument xmlns="urn:iec62325.351:tc57wg16:451-3:publicationdocument:7:0">
<period.timeInterval>
<start>2022-09-27T22:00Z</start>
<end>2022-09-29T22:00Z</end>
</period.timeInterval>
<TimeSeries>
<Period>
<timeInterval>
<start>2022-09-27T22:00Z</start>
<end>2022-09-28T22:00Z</end>
</timeInterval>
<resolution>PT60M</resolution>
<Point>
<position>1</position>
<price.amount>323.70</price.amount>
</Point>
<Point>
<position>2</position>
<price.amount>309.91</price.amount>
</Point>
</Period>
</TimeSeries>
</Publication_MarketDocument>
I want all the start node to use the current dateTime and the end one to be one day later. So I wrote the following XSLT:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xpath-default-namespace="urn:iec62325.351:tc57wg16:451-3:publicationdocument:7:0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:variable name="dateNow" select="fn:format-dateTime(fn:current-dateTime(), '[Y]-[M]-[D]T[H]:[m]Z')"/>
<xsl:variable name="dateTomorrow" select="fn:format-dateTime(fn:current-dateTime() + xs:dayTimeDuration('P1D'), '[Y]-[M]-[D]T[H]:[m]Z')"/>
<xsl:template match="start/text()">
<xsl:value-of select="$dateNow"/>
</xsl:template>
<xsl:template match="end/text()">
<xsl:value-of select="$dateTomorrow"/>
</xsl:template>
</xsl:stylesheet>
(after edit) I first got confused with namespaces, but defining the default namespace as the comment suggested allowed this transformation to run.
However, it is a xslt 2.0 and thus not supported by xslt 1.0 processor. For instance, Visual Studio 2022 offer xml debugging but still do NOT support xslt 2.0. Since my target environment is Linux based, I found out the most standard solution is to use Saxon: java net.sf.saxon.Transform -o:/mnt/d/tmp/output.xml -s:mock.xml -xsl:transform.xslt
this runs silently and finally get correct results. but using jre is quite heavy for a deployment on automated test systems. It is possible to do so with only XSLT 1.0? It seems to be quite complicated with time shift as seen here