Creation of Dynamic elements through XSLT - xslt

<mL>
<s>1</s>
<dt>2022-01-01-01.00.00</dt>
<q>10</q>
</mL>
<mL>
<s>2</s>
<dt>2022-01-01-02.00.00</dt>
<q>11</q>
</mL>
<mL>
<s>3</s>
<dt>2022-01-02-01.00.00</dt>
<q>12</q>
</mL>
<mL>
<s>4</s>
<dt>2022-01-02-02.00.00</dt>
<q>13</q>
</mL>
and I have to transform this structure into something like this based on the changing dates and the time intervals for e.g. in first 2 set of ml tags we can observe <s>1</s> and <s>2</s> have <dt>2022-01-02-01.00.00</dt> and <dt>2022-01-02-02.00.00</dt> here "2022-01-02" is date and "01.00.00" & "02.00.00" are time intervals, and we have to create the no of elements based on these intervals, only check the sample down here.
<Interval>
<IntervalEnding_01.00.00>10</IntervalEnding_01.00.00>
<IntervalEnding_02.00.00>11</IntervalEnding_02.00.00>
</Interval>
<Interval>
<IntervalEnding_01.00.00>12</IntervalEnding_01.00.00>
<IntervalEnding_02.00.00>13</IntervalEnding_02.00.00>
</Interval>
I tried using a template
<xsl:template name="SplitInterval">
<!--xsl:element name="Report_Period_date"-->
<xsl:for-each select="ns1:idGrp/ns1:mL">
<xsl:element name="{concat('IntervalEnding','_', substring (ns1:dt, 1, 4 ),substring (ns1:dt, 6, 2 ),substring (ns1:dt, 9, 2 ),'_',substring (ns1:dt, 12, 2 ),substring (ns1:dt, 15, 2 ))}">
<xsl:value-of select="ns1:q"/>
</xsl:element>
</xsl:for-each>
<!--/xsl:element-->
</xsl:template>
but I am only able to print like without the root Interval tag
<IntervalEnding_01.00.00>12</IntervalEnding_01.00.00>
<IntervalEnding_02.00.00>13</IntervalEnding_02.00.00>
But I need to print like below
<Interval>
<IntervalEnding_01.00.00>12</IntervalEnding_01.00.00>
<IntervalEnding_02.00.00>13</IntervalEnding_02.00.00>
</Interval>

I am guessing (!) you are trying to do something like:
XML (well-formed!!)
<root>
<mL>
<s>1</s>
<dt>2022-01-01-01.00.00</dt>
<q>10</q>
</mL>
<mL>
<s>2</s>
<dt>2022-01-01-02.00.00</dt>
<q>11</q>
</mL>
<mL>
<s>3</s>
<dt>2022-01-02-01.00.00</dt>
<q>12</q>
</mL>
<mL>
<s>4</s>
<dt>2022-01-02-02.00.00</dt>
<q>13</q>
</mL>
</root>
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/root">
<output>
<xsl:for-each select="mL[position() mod 2 = 1]">
<Interval>
<xsl:for-each select=". | following-sibling::mL[1]">
<xsl:element name="IntervalEnding_{substring(dt, 12)}">
<xsl:value-of select="q"/>
</xsl:element>
</xsl:for-each>
</Interval>
</xsl:for-each>
</output>
</xsl:template>
</xsl:stylesheet>
Result
<?xml version="1.0" encoding="UTF-8"?>
<output>
<Interval>
<IntervalEnding_01.00.00>10</IntervalEnding_01.00.00>
<IntervalEnding_02.00.00>11</IntervalEnding_02.00.00>
</Interval>
<Interval>
<IntervalEnding_01.00.00>12</IntervalEnding_01.00.00>
<IntervalEnding_02.00.00>13</IntervalEnding_02.00.00>
</Interval>
</output>

Related

XML to Fixed Length File Using XSLT (Complex Position Logic)

We have a requirement in which need to convert XML into Fixed Length File.
First record is as header and after that we have actual records..From 2 record onwards we need to apply the logic which is mentioned below:
1.After length 45, consider 10 numbers 0000001000, what ever be the
last digit we need to check and replace by following the below
table:
"For Positive Amount: (0000001000) - (000000100{)
{= 0
A = 1
B = 2
c = 3
D = 4
E = 5
F = 6
G = 7
H = 8
I = 9
I have not that much idea so created the small XSLT , request anyone please help on the same.
Input:
<?xml version='1.0' encoding='utf-8'?>
<ZR>
<INPUT>
<I_FIL>ERES</I_FIL>
</INPUT>
<TABLES>
<T_ER>
<item>
<DATA> HEADER1111111122222222333333344456</DATA>
</item>
<item>
<DATA>778944 D4E2 EA 1234567891 2018-11-060000001000EA
0000000000000100001020D04YA30TRE0000000XXXYYY 300{ P 2018-11-05</DATA>
</item>
<item>
<DATA>987654 D4E2 EA 1987654321 2018-11-060000002001EA
0000000000000100001020D04YA30UUU0000000XXXLRB 100{ P 2018-11-05</DATA>
</item>
.
.
.
.
.
.
.
.
</T_ER>
</TABLES>
</ZR>
XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fn="http://www.w3.org/2005/xpath-functions" >
<xsl:output omit-xml-declaration="yes"/>
<xsl:param name="break" select="'
'" />
<xsl:template match="/">
<xsl:value-of select="ZR/TABLES/T_ER/item[1]/DATA"/>
<xsl:value-of select="$break" />
</xsl:template>
</xsl:stylesheet>
Expected Output:
HEADER1111111122222222333333344456
778944 D4E2 EA 1234567891 2018-11-06000000100{EA
0000000000000100001020D04YA30TRE0000000XXXYYY 300{ P 2018-11-05
987654 D4E2 EA 1987654321 2018-11-06000000200AEA
0000000000000100001020D04YA30UUU0000000XXXLRB 100{ P 2018-11-05
.
.
.
.
It looks like you just want to replace the 55th character based on your map, so you could do this...
<xsl:template match="/">
<xsl:value-of select="ZR/TABLES/T_ER/item[1]/DATA"/>
<xsl:value-of select="$break" />
<xsl:for-each select="ZR/TABLES/T_ER/item[position() > 1]/DATA">
<xsl:variable name="char" select="substring('{ABCDEFGHI', number(substring(., 55, 1)) + 1, 1)" />
<xsl:value-of select="concat(substring(., 1, 54), $char, substring(., 56))" />
<xsl:value-of select="$break" />
</xsl:for-each>
</xsl:template>
This would work in XSLT 1.0.
An XSLT 2.0 solution could be this...
<xsl:template match="/">
<xsl:value-of select="ZR/TABLES/T_ER/item[1]/DATA,
ZR/TABLES/T_ER/item[position() > 1]/DATA/concat(substring(., 1, 54), substring('{ABCDEFGHI', number(substring(., 55, 1)) + 1, 1), substring(., 56))"
separator="
" />
</xsl:template>
In XSLT 3.0, you could potentially make use of a map with has the advantage of easily being extended if you wanted to consider two or more characters instead:
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:map="http://www.w3.org/2005/xpath-functions/map"
xmlns:fn="http://www.w3.org/2005/xpath-functions" >
<xsl:output omit-xml-declaration="yes"/>
<xsl:param name="break" select="'
'" />
<xsl:variable name="chars" as="map(xs:string, xs:string)">
<xsl:map>
<xsl:map-entry key="'0'" select="'{'"/>
<xsl:map-entry key="'1'" select="'A'"/>
<xsl:map-entry key="'2'" select="'B'"/>
<xsl:map-entry key="'3'" select="'C'"/>
<xsl:map-entry key="'4'" select="'D'"/>
<xsl:map-entry key="'5'" select="'E'"/>
<xsl:map-entry key="'6'" select="'F'"/>
<xsl:map-entry key="'7'" select="'G'"/>
<xsl:map-entry key="'8'" select="'H'"/>
<xsl:map-entry key="'9'" select="'I'"/>
</xsl:map>
</xsl:variable>
<xsl:template match="/">
<xsl:value-of select="ZR/TABLES/T_ER/item[1]/DATA,
ZR/TABLES/T_ER/item[position() > 1]/DATA/concat(substring(., 1, 54), $chars(substring(., 55, 1)), substring(., 56))" separator="
" />
</xsl:template>
</xsl:stylesheet>
There's probably a much nicer way in XSLT 3.0, so hopefully Martin Honnen will be along soon to say....
Well, using functions string-length, substring and translate for your specifications it can be achieved as:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:output omit-xml-declaration="yes" />
<xsl:param name="break" select="'
'" />
<xsl:template match="/">
<xsl:value-of select="ZR/TABLES/T_ER/item[1]/DATA" />
<xsl:value-of select="$break" />
<xsl:for-each select="ZR/TABLES/T_ER/item[position() != 1]">
<xsl:variable name="length" select="string-length(substring(DATA,0,46))" />
<xsl:variable name="tenNumbers" select="substring(DATA, ($length + 1), 10)"/>
<xsl:variable name="charToReplace" select="translate(substring($tenNumbers, string-length($tenNumbers), 1),'0123456789','{ABCDEFGHI')" />
<xsl:value-of select="concat(substring(DATA,0,46), substring(DATA, ($length + 1), 9), $charToReplace, substring(DATA,($length+11),(string-length(DATA) + 1)))"/>
<xsl:value-of select="$break" />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Is there a way to replace the for-each with apply-templates in an XSLT Transform?

Environment: XSLT 1.0
The transform will take each element in partOne section and lookup #field attribute in partTwo section using #find attribute and then output #value attribute.
I'm using a for-each loop and was wondering if apply-templates could work?
xml
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="file.xslt"?>
<xml>
<partOne>
<target field="hello"/>
<target field="world"/>
</partOne>
<partTwo>
<number input="2" find="hello" value="valone" />
<number input="2" find="world" value="valtwo" />
<number input="2" find="hello" value="valthree" />
<number input="2" find="world" value="valfour" />
</partTwo>
</xml>
xsl
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="/xml/partOne/target">
,<xsl:value-of select="#field"/>
<xsl:for-each select="/xml/partTwo/number[#find=current()/#field]">
,<xsl:value-of select="#value"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Output:
,hello
,valone
,valthree
,world
,valtwo
,valfour
Well, it seems straight-forward to change
<xsl:for-each select="/xml/partTwo/number[#find=current()/#field]">
,<xsl:value-of select="#value"/>
</xsl:for-each>
to
<xsl:apply-templates select="/xml/partTwo/number[#find=current()/#field]"/>
with a template
<xsl:template match="partTwo/number">
,<xsl:value-of select="#value"/>
</xsl:template>
As your root template so far processes all elements you need to change it to
<xsl:template match="/">
<xsl:apply-templates select="xml/partOne"/>
</xsl:template>
to avoid processing the partTwo element(s) twice.
For the cross-reference you might want to use a key in both versions:
<xsl:key name="ref" match="partTwo/number" use="#find"/>
and then select="key('ref', #field)" instead of select="/xml/partTwo/number[#find=current()/#field]" for the apply-templates or for-each.

Using Saxon, increment or decrement the global variables in XSLT

I want to use a counter to increment and decrement the value in XSLT. I am able to achieve this using Apache Xalan, but now I want to achieve the same using Saxon.
My XSLT script for Xalan looks like this:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:lxslt="http://xml.apache.org/xslt"
xmlns:result="http://www.example.com/results"
extension-element-prefixes="result">
<lxslt:component prefix="result" functions="incr dcr">
<lxslt:script lang="javascript">
var count = 0;
function incr() {
count++;
return count;
}
function dcr() {
count--;
return count;
}
</lxslt:script>
</lxslt:component>
<xsl:template match="/">
a)<xsl:value-of select="result:incr()"/>
b)<xsl:value-of select="result:incr()"/>
c)<xsl:value-of select="result:dcr()"/>
</xsl:template>
</xsl:stylesheet>
Expected output is:
a)1
b)2
c)1
------------------------- my use case using saxon -------------------
This is my sample data.xml file. I want to transform this to html file.
<entity>
<record>10</record>
<record>15</record>
<record>19</record>
<record>7</record>
<record>4</record>
<record>14</record>
<record>24</record>
<entity>
I want to implement a counter for line-number and want to increment the counter and print the line-number
my expected outputs:
case 1: If record values > 14, then my output should have line-number with value as.
line-num value
1 15
2 19
3 24
case 2 : If record values < 14, then my output should have line-number with value as.
line-num value
1 10
2 7
3 4
My other use case :
<entity>
<record>10</record>
<record>15</record>
<record>19</record>
<record>7</record>
<record>20</record>
<record>14</record>
<record>24</record>
<entity>
<record>30</record>
<record>3</record>
</entity>
</entity>
<entity>
<record>5</record>
<record>17</record>
<record>19</record>
<record>6</record>
<record>70</record>
<record>9</record>
<record>35</record>
<entity>
<record>15</record>
<record>2</record>
</entity>
</entity>
This is my other use case, first <entity> record value > 15 and in second <entity> record value < 10, and my <entity> can grow bigger where i have to show only some record based on condition.
line-num value
1 19
2 20
3 24
4 30
5 5
6 6
7 2
There is no way to use Javascript in Saxon as far as I know.
If you want to use an assignable and changeable variable in Saxon 9 (EE or PE) then you can use
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:saxon="http://saxon.sf.net/"
xmlns:mf="http://example.com/mf"
extension-element-prefixes="saxon"
exclude-result-prefixes="xs saxon mf"
version="2.0">
<xsl:variable name="counter" as="xs:integer" select="0" saxon:assignable="yes"/>
<xsl:function name="mf:incr" as="xs:integer">
<saxon:assign name="counter" select="$counter + 1"/>
<xsl:sequence select="$counter"/>
</xsl:function>
<xsl:function name="mf:decr" as="xs:integer">
<saxon:assign name="counter" select="$counter - 1"/>
<xsl:sequence select="$counter"/>
</xsl:function>
<xsl:template match="/" name="main">
a)<xsl:value-of select="mf:incr()"/>
b)<xsl:value-of select="mf:incr()"/>
c)<xsl:value-of select="mf:decr()"/>
</xsl:template>
</xsl:stylesheet>
See the warnings and notes in http://saxonica.com/html/documentation/extensions/instructions/assign.html however on attempts to introduce side effects. You might want to edit your question to explain your XML input and the desired output and why you think you need such a counter variable, hopefully we can then show a pure XSLT 2.0 solution instead of using an assignable variable.
As for your requirement to process and number certain input elements, the stylesheet
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:output indent="yes"/>
<xsl:template match="entity">
<root>
<xsl:apply-templates select="record[. > 14]"/>
<xsl:apply-templates select="record[. < 14]"/>
</root>
</xsl:template>
<xsl:template match="record">
<xsl:copy>
<line-num>
<xsl:value-of select="position()"/>
</line-num>
<value>
<xsl:value-of select="."/>
</value>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
when run against the input
<entity>
<record>10</record>
<record>15</record>
<record>19</record>
<record>7</record>
<record>4</record>
<record>14</record>
<record>24</record>
</entity>
outputs
<root>
<record>
<line-num>1</line-num>
<value>15</value>
</record>
<record>
<line-num>2</line-num>
<value>19</value>
</record>
<record>
<line-num>3</line-num>
<value>24</value>
</record>
<record>
<line-num>1</line-num>
<value>10</value>
</record>
<record>
<line-num>2</line-num>
<value>7</value>
</record>
<record>
<line-num>3</line-num>
<value>4</value>
</record>
</root>
Other options are using e.g. xsl:number count="entity[. < 14]".
As for your latest requirement, I think you can still handle that using position() as long as you select the elements you want to process, e.g. the stylesheet
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:apply-templates select="/root/entity[1]//record[. > 15], /root/entity[2]//record[. < 10]"/>
</xsl:template>
<xsl:template match="record">
<xsl:value-of select="concat(position(), ' ', ., '
')"/>
</xsl:template>
</xsl:stylesheet>
when applied to the input
<root>
<entity>
<record>10</record>
<record>15</record>
<record>19</record>
<record>7</record>
<record>20</record>
<record>14</record>
<record>24</record>
<entity>
<record>30</record>
<record>3</record>
</entity>
</entity>
<entity>
<record>5</record>
<record>17</record>
<record>19</record>
<record>6</record>
<record>70</record>
<record>9</record>
<record>35</record>
<entity>
<record>15</record>
<record>2</record>
</entity>
</entity>
</root>
outputs (with an XSLT 2.0 processor like Saxon 9)
1 19
2 20
3 24
4 30
5 5
6 6
7 9
8 2
(Responding to the expanded description of the problem)
The key thing to appreciate is that this problem requires sequential processing of the record elements, in document order. This means it can't be achieved in a processor-independent way with xsl:for-each or xsl:apply-templates because these are not guaranteed to process the elements one at a time in order (recent versions of Saxon, for example, will process elements in parallel using multi-threading, and the spec allows this; this makes any attempt to update global variables ineffective).
The classic way to process elements sequentially is using head-tail recursion. Write a template or function that takes the sequence of record elements as a parameter, and the current line number as a second parameter. If the list of records is non-empty, then (a) process the first record in the list, and (b) call yourself recursively to process the rest of the list, passing the new value of the line-number.
It can be a bit mind-blowing to write code in this way the first time you try it, but it soon comes naturally.
In XSLT 3.0 there's a new construct xsl:iterate that looks more like a conventional loop: when you enter the xsl:iterate body you pass initial values for its parameters, and each time you continue to the next iteration of the loop you can set new values for the parameters.
Without javascript, using saxon extension instructions.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:saxon="http://saxon.sf.net/"
extension-element-prefixes="saxon"
exclude-result-prefixes="saxon">
<xsl:variable name="i" select="0" saxon:assignable="yes"/>
<xsl:template match="/">
a) <xsl:value-of select="$i"/>
<saxon:assign name="i" select="$i+1"/>
b) <xsl:value-of select="$i"/>
<saxon:assign name="i" select="$i+2"/>
c) <xsl:value-of select="$i"/>
<saxon:assign name="i" select="$i+3"/>
d) <xsl:value-of select="$i"/>
</xsl:template>
</xsl:stylesheet>

how to concat the attribute names of the parent nodes

I have a XML Document like this:
<Module Name="DacInPlaceUpgradeLtmTest" Desc="" >
<TestCase Name="ExecuteInPlaceUpgradeTest">
<TestCase Name="BugRepro">
<TestCase Name="295130">
<Variation Id="1" Desc="Sql - EmptyAlterScript">
<Variation Id="2" Desc="Sql - EmptyDatabase">
</TestCase>
</TestCase>
</TestCase>
</Module>
I use xsl to get a value:
ExectionInplaceUPgradeTest BugRepro 295130
by using following template:
<xsl:template match="TestCase//Variation">
<xsl:for-each select="..#Name">
Today, I can only get 295130, I wonder how can I get all Attribtes of the parent nodes which is TestCase.
I wonder how can I get all Attributes of the parent nodes which is TestCase
You might need the XPath ancestor-or-self axis:
ancestor-or-self::TestCase/#Name
how to concat the attribte names of the parent nodes
It really depends on the template context and on the code you are already working on. taking as a reference your fragment, I would write better:
<xsl:template match="TestCase[Variation]">
<xsl:for-each select="ancestor-or-self::TestCase/#Name">
<xsl:value-of select="concat(.,' ')"/>
</xsl:for-each>
</xsl:template>
This will print the wanted string starting from the TestCase node which has a Variation node as a child.
This transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="Variation[#Id = 1]">
<xsl:apply-templates mode="printName"
select="ancestor::TestCase"/>
</xsl:template>
<xsl:template match="TestCase" mode="printName">
<xsl:if test="not(position()=1)">
<xsl:text> </xsl:text>
</xsl:if>
<xsl:value-of select="#Name"/>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
when applied on the provided XML document:
<Module Name="DacInPlaceUpgradeLtmTest" Desc="" >
<TestCase Name="ExecuteInPlaceUpgradeTest">
<TestCase Name="BugRepro">
<TestCase Name="295130">
<Variation Id="1" Desc="Sql - EmptyAlterScript"/>
<Variation Id="2" Desc="Sql - EmptyDatabase"/>
</TestCase>
</TestCase>
</TestCase>
</Module>
produces the wanted, correct result:
ExecuteInPlaceUpgradeTest BugRepro 295130
II. XPath 2.0/XSLT 2.0 solution
//TestCase[Variation]
/ancestor-or-self::TestCase
/#Name/string(.)
The above XPath 2.0 expression when evaluated on the above XML document produces exactly the wanted, correct result:
ExecuteInPlaceUpgradeTest BugRepro 295130
It can be used in the following XSLT 2.0 solution:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:sequence select=
"//TestCase[Variation]
/ancestor-or-self::TestCase
/#Name/string(.)
"/>
</xsl:template>
</xsl:stylesheet>

XSLT 1.0 how increment a date

UPDATE: Cannot use any EXSLT extensions.
Also I'm using date in two different places and I only want to update one of them and not both.
I need to increment a date in my XSLT transformation. I'm using XSLT 1.0.
In source XML I have a date like this
<XML>
<Date>4/22/2011 3:30:43 PM</Date>
</XML>
Then I need to add 10 years to the output. Like this
<Output>
<Odate>4/22/2011 3:30:43 PM</Odate>
<Cdate>4/22/2021 3:30:43 PM</Cdate>
</Output>
How this can be done in XSLT 1.0. Thanks in advance.
The following is no general date arithmetic implementation but might suffice to increment the year part:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:param name="year-inc" select="10"/>
<xsl:template match="XML">
<Output>
<xsl:apply-templates/>
</Output>
</xsl:template>
<xsl:template match="Date">
<xsl:variable name="d0" select="substring-before(., '/')"/>
<xsl:variable name="d1" select="substring-before(substring-after(., '/'), '/')"/>
<xsl:variable name="d2" select="substring-after(substring-after(., '/'), '/')"/>
<xsl:variable name="new-year" select="substring($d2, 1, 4) + $year-inc"/>
<Cdate>
<xsl:value-of select="concat($d0, '/', $d1, '/', $new-year, substring($d2, 5))"/>
</Cdate>
</xsl:template>
</xsl:stylesheet>
Depends a little how pernickety you want to be, e.g. what's the date 10 years after 29 Feb 2004? There are a number of useful XSLT 1.0 date-handling routines you can download at www.exslt.org, I think they include both a parse-date template which will convert your US-format date into a standard ISO date, date arithmetic templates which will allow you to add a duration to an ISO-format date, and a format-date function that will turn it back into US format.
I have figured it with the help of #Martin. I extend on #Martin's code and only called the template when I need to modify the date.
XSLT:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:param name="year-inc" select="10"/>
<xsl:template match="XML">
<Output>
<Odate>
<xsl:value-of select="Date"/>
</Odate>
<Cdate>
<xsl:call-template name="increment"/>
</Cdate>
</Output>
</xsl:template>
<xsl:template name="increment">
<xsl:variable name="d0" select="substring-before(Date, '/')"/>
<xsl:variable name="d1" select="substring-before(substring-after(Date, '/'), '/')"/>
<xsl:variable name="d2" select="substring-after(substring-after(Date, '/'), '/')"/>
<xsl:variable name="new-year" select="substring($d2, 1, 4) + $year-inc"/>
<xsl:value-of select="concat($d0, '/', $d1, '/', $new-year, substring($d2, 5))"/>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="utf-8"?>
<Output>
<Odate>4/22/2011 3:30:43 PM</Odate>
<Cdate>4/22/2021 3:30:43 PM</Cdate>
</Output>