I am calculating a max date @target_date_calc_end
inside a for-each, but I have two nested for-each, and this is being a problem.
As overview: I have this XML:
<asset>
<child_asset_rel child_asset="124510"/>
<child_asset_rel child_asset="124509"/>
<child_asset_rel child_asset="124508"/>
<child_asset_rel child_asset="124511"/>
<child_asset_rel child_asset="124506"/>
<child_asset_rel child_asset="124495"/>
</asset>
Each child node refers to another XML file through child_asset=" "
, which has the form:
<asset id="124510">
<schedule_step target_date_calc_end="2015-07-07T22:00:00Z"/>
<schedule_step target_date_calc_end="2015-08-17T22:00:00Z"/>
<schedule_step target_date_calc_end="2015-08-20T21:59:00Z"/>
<schedule_step target_date_calc_end="2015-08-20T21:59:00Z"/>
<schedule_step target_date_calc_end="2015-07-15T22:00:00Z"/>
</asset>
with several dates target_date_calc_end="..."
.
I want to iterate through all the nodes, load the children files and calculate the maximum date across the files.
This is my attempt:
<xsl:template match="/">
<xsl:for-each select="$asset/child_asset_rel/@child_asset">
<xsl:variable name="childAssetXML">
<xsl:copy-of select="cs:get-asset(.)"/>
</xsl:variable>
<xsl:variable name="endDate">
<xsl:for-each select="$childAssetXML/asset/schedule_step/@target_date_calc_end">
<xsl:sort select="." order="descending"/>
<xsl:if test="position() = 1">
<xsl:value-of select="."/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:value-of select="$endDate"/>
</xsl:for-each>
</xsl:template>
The first step, which is done in the first for-each()
, is to look for the
children. I get an ID and use the function get-asset()
to obtain the XML of the child file, which is stored in childAssetXML
.
I correctly get the maximum date of each child in endDate
variable in each iteration of the for-each, but I am not able to get the maximum value of endDate
after all the iterations. The output I am getting is the maximum value of the last iteration of the inner loop.
Any ideas?