1

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?

Jongware
  • 22,200
  • 8
  • 54
  • 100
David Comino
  • 97
  • 1
  • 1
  • 5
  • 1
    Consider to post minimal but complete samples of XSLT, input XML, result you want and the one you get. I am afraid an incomplete snippet of XSLT that calls a function not shown makes it very hard for us to tell what you want to achieve and where the problem is. – Martin Honnen Mar 23 '15 at 12:25
  • I have tried to improve the info in the problem. sorry for the invonvenience. I hope now could be easyer. – David Comino Mar 23 '15 at 13:08
  • No, it's still not clear. Your XML has only one parent (`asset`). Getting the maximum value of its children `schedule_step` dates should be trivial using the max() function from the context of the parent (assuming you are using XSLT 2.0 as tagged). – michael.hor257k Mar 23 '15 at 13:19
  • 1
    I have change all the explanation, I think now is much better. – David Comino Mar 23 '15 at 13:44

1 Answers1

0

Try max($asset/child_asset_rel/cs:get-asset(@child_asset)/asset/schedule_step/xs:dateTime(@target_date_calc_end)) or max($asset/child_asset_rel/cs:get-asset(@child_asset)/schedule_step/xs:dateTime(@target_date_calc_end)), depending on what your function cs:get-asset returns.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110