给出以下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>
目标是获取预定列表中已知前任步骤的 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值&amp;除了弄清楚哪一个是最新的?
请注意,这可能是多个DateTime值,需要相互比较。可以在这里使用XSLT 2.0。
答案 0 :(得分:2)
如何获得$ predList前辈的end_time值&amp;除了弄清楚哪一个是最新的?
这应该很简单。只需通过标记当前step
并将其与predlist
属性进行比较,即可获得相应的id
。然后使用max()
获取最新的end_time
。
参见变量&#34; predecessorEndTime&#34;在我下面的例子中......
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示例。