gpt4 book ai didi

xml - XSLT:在两个xsl:for-each循环中提取已知前辈的最新DateTime

转载 作者:行者123 更新时间:2023-12-03 17:36:39 27 4
gpt4 key购买 nike

给出以下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>


目的是获得predlist中已知的前任步骤的end_time,以计算实际步骤start_time与前任步骤end_time之间的时间,该时间是给定前任中的最新步骤。例如。对于Step_5,在这种情况下,这将是Step_4的结束时间。但这不会每次都这样,并且步数是可变的。这些步骤是并行处理的,这就是为什么在这种情况下,按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可以在这里使用。

最佳答案

我如何获得$ predList的前任者的end_time值,以及如何弄清楚前者中的哪一个是最新的?


这应该很容易。只需通过标记当前step并将其与predlist属性进行比较即可获得相应的id。然后使用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 - XSLT:在两个xsl:for-each循环中提取已知前辈的最新DateTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28053748/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com