XSLT:在两个 xsl:for-each 循环中提取已知前辈的最新 DateTime
XSLT: Extract the latest DateTime of known predecessors within two xsl:for-each loops
给定如下XML:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<description>
<program version="2.2.0" type="plain">
<job id="MyJobID">
<step id="start_of_job">
<param name="begin_time">2014-09-29T16:54:44.920+0200</param>
<param name="end_time">2014-09-29T16:54:45.100+0200</param>
<param name="state">10</param>
</step>
<step id="step_1">
<param name="predlist">start_of_job</param>
<param name="begin_time">2014-09-29T16:54:49.454+0200</param>
<param name="end_time">2014-09-29T16:54:49.473+0200</param>
<param name="state">10</param>
</step>
<step id="step_2">
<param name="predlist">start_of_job</param>
<param name="begin_time">2014-09-29T16:54:48.643+0200</param>
<param name="end_time">2014-09-29T16:54:48.675+0200</param>
<param name="state">10</param>
</step>
<step id="step_3">
<param name="predlist">step_1</param>
<param name="rc">0</param>
<param name="begin_time">2014-09-29T16:54:49.442+0200</param>
<param name="end_time">2014-09-29T16:54:49.554+0200</param>
<param name="state">10</param>
</step>
<step id="step_4">
<param name="predlist">step_1 step_3</param>
<param name="begin_time">2014-09-29T16:54:54.258+0200</param>
<param name="end_time">2014-09-29T16:55:49.958+0200</param>
<param name="state">10</param>
</step>
<step id="step_5">
<param name="predlist">step_1 step_2 step_4</param>
<param name="begin_time">2014-09-29T16:55:54.550+0200</param>
<param name="end_time">2014-09-29T16:55:58.105+0200</param>
<param name="state">10</param>
</step>
<step id="end_of_job">
<param name="predlist">start_of_job step_1 step_2 step_3 step_4 step_5</param>
<param name="state">3</param>
</step>
</job>
</program>
</description>
<content />
</root>
中的objective是获取predlist中已知前驱步骤的end_time,进行计算实际步骤 start_time 和前一步骤 end_time 之间的时间,这是给定前一步骤的最新步骤。例如。对于 Step_5,在这种情况下,这将是 Step_4 的 end_time。但这不会每次都是这样,而且步数是可变的。这些步骤是并行处理的,这就是为什么在这种情况下不会按 end_time 对节点进行排序。但也许我错了。
我对 XSLT 不是很有经验。这是我到目前为止的发现:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:java="http://xml.apache.org/xslt/java"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:exsl="http://exslt.org/common"
version="2.0">
<!-- Add new element -->
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:for-each select="root/description/program/job/step">
<xsl:if test="param[@name='state']=10">
<!-- Retrieve predecessor list -->
<xsl:variable name="predList" select="tokenize(param[@name='predlist'], ' ')"/>
<!-- Check which predecessor finished last -->
<xsl:for-each select="$predList">
<xsl:variable name="predecessor" select="."/>
<!-- Obtain end_time of predecessor and store it in a xsl:variable e.g. $received__predecessor_end_time somehow -->
</xsl:for-each>
<!-- Process ouput XML -->
<program>
<xsl:attribute name="stepID">
<xsl:value-of select="@id"/>
</xsl:attribute>
<xsl:attribute name="waitTime">
<!-- Calculate wait time in seconds -->
<xsl:call-template name="duration_in_s">
<xsl:with-param name="dateTimeBegin" select="substring(param[@name='begin_time'],1,23)"/>
<xsl:with-param name="dateTimeEnd" select="substring($received__predecessor_end_time,1,23)"/>
</xsl:call-template>
</xsl:attribute>
</program>
</xsl:if>
</xsl:for-each>
</xsl:template>
<!-- Template: Calculate wait time in seconds -->
<xsl:template name="duration_in_s">
<xsl:param name="dateTimeBegin"/>
<xsl:param name="dateTimeEnd"/>
<xsl:variable name="DURATION"><xsl:value-of select="xs:dateTime($dateTimeBegin)-xs:dateTime($dateTimeEnd)" /></xsl:variable>
<xsl:value-of select="((hours-from-duration($DURATION)*3600)+(minutes-from-duration($DURATION)*60)+seconds-from-duration($DURATION))" />
</xsl:template>
</xsl:stylesheet>
我的问题是 xsl:for-each $predList 循环中的范围。在此循环中,我无法访问任何节点以获取其 end_time。我想我的做法在这里是错误的。
我怎样才能获得 $predList 前辈的 end_time 值以及除此之外找出其中哪一个是最新的?
请注意,这可能是多个 DateTime 值,需要相互比较。此处可以使用 XSLT 2.0。
How can I get the end_time value of the $predList predecessors & in addition to that figure out which of them is the latest one?
这应该很简单。通过标记当前 predlist
并将其与 id
属性进行比较,即可获得相应的 step
。然后使用max()
得到最新的end_time
.
请参阅下面示例中的变量 "predecessorEndTime"...
XML 输入
<root>
<description>
<program version="2.2.0" type="plain">
<job id="MyJobID">
<step id="start_of_job">
<param name="begin_time">2014-09-29T16:54:44.920+02:00</param>
<param name="end_time">2014-09-29T16:54:45.100+02:00</param>
<param name="state">10</param>
</step>
<step id="step_1">
<param name="predlist">start_of_job</param>
<param name="begin_time">2014-09-29T16:54:49.454+02:00</param>
<param name="end_time">2014-09-29T16:54:49.473+02:00</param>
<param name="state">10</param>
</step>
<step id="step_2">
<param name="predlist">start_of_job</param>
<param name="begin_time">2014-09-29T16:54:48.643+02:00</param>
<param name="end_time">2014-09-29T16:54:48.675+02:00</param>
<param name="state">10</param>
</step>
<step id="step_3">
<param name="predlist">step_1</param>
<param name="rc">0</param>
<param name="begin_time">2014-09-29T16:54:49.442+02:00</param>
<param name="end_time">2014-09-29T16:54:49.554+02:00</param>
<param name="state">10</param>
</step>
<step id="step_4">
<param name="predlist">step_1 step_3</param>
<param name="begin_time">2014-09-29T16:54:54.258+02:00</param>
<param name="end_time">2014-09-29T16:55:49.958+02:00</param>
<param name="state">10</param>
</step>
<step id="step_5">
<param name="predlist">step_1 step_2 step_4</param>
<param name="begin_time">2014-09-29T16:55:54.550+02:00</param>
<param name="end_time">2014-09-29T16:55:58.105+02:00</param>
<param name="state">10</param>
</step>
<step id="end_of_job">
<param name="predlist">start_of_job step_1 step_2 step_3 step_4 step_5</param>
<param name="state">3</param>
</step>
</job>
</program>
</description>
<content />
</root>
XSLT 2.0(很多 xsl:variable
试图让人们更容易看到正在发生的事情。)
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/*">
<results>
<xsl:apply-templates/>
</results>
</xsl:template>
<xsl:template match="text()"/>
<xsl:template match="step">
<xsl:variable name="predecessors"
select="tokenize(param[@name='predlist'],'\s')"
as="item()*"/>
<xsl:variable name="predecessorEndTime"
select="max(../step[@id=$predecessors]/xs:dateTime(param[@name='end_time']))"
as="xs:dateTime?"/>
<xsl:variable name="duration"
select="xs:dateTime(param[@name='begin_time']) - $predecessorEndTime"
as="xs:duration?"/>
<xsl:variable name="waitTime"
select="((hours-from-duration($duration)*3600)+
(minutes-from-duration($duration)*60)+
seconds-from-duration($duration))"
as="xs:double?"/>
<program stepID="{@id}"
waitTime="{if ($waitTime) then $waitTime else 0}"/>
</xsl:template>
</xsl:stylesheet>
XML输出
<results>
<program stepID="start_of_job" waitTime="0"/>
<program stepID="step_1" waitTime="4.354"/>
<program stepID="step_2" waitTime="3.543"/>
<program stepID="step_3" waitTime="-0.031"/>
<program stepID="step_4" waitTime="4.704"/>
<program stepID="step_5" waitTime="4.592"/>
<program stepID="end_of_job" waitTime="0"/>
</results>
编辑回复评论:
如果 job
的其他子项不应输出,您可以通过添加此模板将范围缩小到仅 step
:
<xsl:template match="job">
<xsl:apply-templates select="step"/>
</xsl:template>
或者您可以添加此模板以抑制所有未被任何其他处理处理的文本:
<xsl:template match="text()"/>
我用后者更新了我的 XSLT 示例。
给定如下XML:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<description>
<program version="2.2.0" type="plain">
<job id="MyJobID">
<step id="start_of_job">
<param name="begin_time">2014-09-29T16:54:44.920+0200</param>
<param name="end_time">2014-09-29T16:54:45.100+0200</param>
<param name="state">10</param>
</step>
<step id="step_1">
<param name="predlist">start_of_job</param>
<param name="begin_time">2014-09-29T16:54:49.454+0200</param>
<param name="end_time">2014-09-29T16:54:49.473+0200</param>
<param name="state">10</param>
</step>
<step id="step_2">
<param name="predlist">start_of_job</param>
<param name="begin_time">2014-09-29T16:54:48.643+0200</param>
<param name="end_time">2014-09-29T16:54:48.675+0200</param>
<param name="state">10</param>
</step>
<step id="step_3">
<param name="predlist">step_1</param>
<param name="rc">0</param>
<param name="begin_time">2014-09-29T16:54:49.442+0200</param>
<param name="end_time">2014-09-29T16:54:49.554+0200</param>
<param name="state">10</param>
</step>
<step id="step_4">
<param name="predlist">step_1 step_3</param>
<param name="begin_time">2014-09-29T16:54:54.258+0200</param>
<param name="end_time">2014-09-29T16:55:49.958+0200</param>
<param name="state">10</param>
</step>
<step id="step_5">
<param name="predlist">step_1 step_2 step_4</param>
<param name="begin_time">2014-09-29T16:55:54.550+0200</param>
<param name="end_time">2014-09-29T16:55:58.105+0200</param>
<param name="state">10</param>
</step>
<step id="end_of_job">
<param name="predlist">start_of_job step_1 step_2 step_3 step_4 step_5</param>
<param name="state">3</param>
</step>
</job>
</program>
</description>
<content />
</root>
中的objective是获取predlist中已知前驱步骤的end_time,进行计算实际步骤 start_time 和前一步骤 end_time 之间的时间,这是给定前一步骤的最新步骤。例如。对于 Step_5,在这种情况下,这将是 Step_4 的 end_time。但这不会每次都是这样,而且步数是可变的。这些步骤是并行处理的,这就是为什么在这种情况下不会按 end_time 对节点进行排序。但也许我错了。
我对 XSLT 不是很有经验。这是我到目前为止的发现:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:java="http://xml.apache.org/xslt/java"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:exsl="http://exslt.org/common"
version="2.0">
<!-- Add new element -->
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:for-each select="root/description/program/job/step">
<xsl:if test="param[@name='state']=10">
<!-- Retrieve predecessor list -->
<xsl:variable name="predList" select="tokenize(param[@name='predlist'], ' ')"/>
<!-- Check which predecessor finished last -->
<xsl:for-each select="$predList">
<xsl:variable name="predecessor" select="."/>
<!-- Obtain end_time of predecessor and store it in a xsl:variable e.g. $received__predecessor_end_time somehow -->
</xsl:for-each>
<!-- Process ouput XML -->
<program>
<xsl:attribute name="stepID">
<xsl:value-of select="@id"/>
</xsl:attribute>
<xsl:attribute name="waitTime">
<!-- Calculate wait time in seconds -->
<xsl:call-template name="duration_in_s">
<xsl:with-param name="dateTimeBegin" select="substring(param[@name='begin_time'],1,23)"/>
<xsl:with-param name="dateTimeEnd" select="substring($received__predecessor_end_time,1,23)"/>
</xsl:call-template>
</xsl:attribute>
</program>
</xsl:if>
</xsl:for-each>
</xsl:template>
<!-- Template: Calculate wait time in seconds -->
<xsl:template name="duration_in_s">
<xsl:param name="dateTimeBegin"/>
<xsl:param name="dateTimeEnd"/>
<xsl:variable name="DURATION"><xsl:value-of select="xs:dateTime($dateTimeBegin)-xs:dateTime($dateTimeEnd)" /></xsl:variable>
<xsl:value-of select="((hours-from-duration($DURATION)*3600)+(minutes-from-duration($DURATION)*60)+seconds-from-duration($DURATION))" />
</xsl:template>
</xsl:stylesheet>
我的问题是 xsl:for-each $predList 循环中的范围。在此循环中,我无法访问任何节点以获取其 end_time。我想我的做法在这里是错误的。
我怎样才能获得 $predList 前辈的 end_time 值以及除此之外找出其中哪一个是最新的?
请注意,这可能是多个 DateTime 值,需要相互比较。此处可以使用 XSLT 2.0。
How can I get the end_time value of the $predList predecessors & in addition to that figure out which of them is the latest one?
这应该很简单。通过标记当前 predlist
并将其与 id
属性进行比较,即可获得相应的 step
。然后使用max()
得到最新的end_time
.
请参阅下面示例中的变量 "predecessorEndTime"...
XML 输入
<root>
<description>
<program version="2.2.0" type="plain">
<job id="MyJobID">
<step id="start_of_job">
<param name="begin_time">2014-09-29T16:54:44.920+02:00</param>
<param name="end_time">2014-09-29T16:54:45.100+02:00</param>
<param name="state">10</param>
</step>
<step id="step_1">
<param name="predlist">start_of_job</param>
<param name="begin_time">2014-09-29T16:54:49.454+02:00</param>
<param name="end_time">2014-09-29T16:54:49.473+02:00</param>
<param name="state">10</param>
</step>
<step id="step_2">
<param name="predlist">start_of_job</param>
<param name="begin_time">2014-09-29T16:54:48.643+02:00</param>
<param name="end_time">2014-09-29T16:54:48.675+02:00</param>
<param name="state">10</param>
</step>
<step id="step_3">
<param name="predlist">step_1</param>
<param name="rc">0</param>
<param name="begin_time">2014-09-29T16:54:49.442+02:00</param>
<param name="end_time">2014-09-29T16:54:49.554+02:00</param>
<param name="state">10</param>
</step>
<step id="step_4">
<param name="predlist">step_1 step_3</param>
<param name="begin_time">2014-09-29T16:54:54.258+02:00</param>
<param name="end_time">2014-09-29T16:55:49.958+02:00</param>
<param name="state">10</param>
</step>
<step id="step_5">
<param name="predlist">step_1 step_2 step_4</param>
<param name="begin_time">2014-09-29T16:55:54.550+02:00</param>
<param name="end_time">2014-09-29T16:55:58.105+02:00</param>
<param name="state">10</param>
</step>
<step id="end_of_job">
<param name="predlist">start_of_job step_1 step_2 step_3 step_4 step_5</param>
<param name="state">3</param>
</step>
</job>
</program>
</description>
<content />
</root>
XSLT 2.0(很多 xsl:variable
试图让人们更容易看到正在发生的事情。)
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/*">
<results>
<xsl:apply-templates/>
</results>
</xsl:template>
<xsl:template match="text()"/>
<xsl:template match="step">
<xsl:variable name="predecessors"
select="tokenize(param[@name='predlist'],'\s')"
as="item()*"/>
<xsl:variable name="predecessorEndTime"
select="max(../step[@id=$predecessors]/xs:dateTime(param[@name='end_time']))"
as="xs:dateTime?"/>
<xsl:variable name="duration"
select="xs:dateTime(param[@name='begin_time']) - $predecessorEndTime"
as="xs:duration?"/>
<xsl:variable name="waitTime"
select="((hours-from-duration($duration)*3600)+
(minutes-from-duration($duration)*60)+
seconds-from-duration($duration))"
as="xs:double?"/>
<program stepID="{@id}"
waitTime="{if ($waitTime) then $waitTime else 0}"/>
</xsl:template>
</xsl:stylesheet>
XML输出
<results>
<program stepID="start_of_job" waitTime="0"/>
<program stepID="step_1" waitTime="4.354"/>
<program stepID="step_2" waitTime="3.543"/>
<program stepID="step_3" waitTime="-0.031"/>
<program stepID="step_4" waitTime="4.704"/>
<program stepID="step_5" waitTime="4.592"/>
<program stepID="end_of_job" waitTime="0"/>
</results>
编辑回复评论:
如果 job
的其他子项不应输出,您可以通过添加此模板将范围缩小到仅 step
:
<xsl:template match="job">
<xsl:apply-templates select="step"/>
</xsl:template>
或者您可以添加此模板以抑制所有未被任何其他处理处理的文本:
<xsl:template match="text()"/>
我用后者更新了我的 XSLT 示例。