在xml文档中查找具有最高价的元素

时间:2016-04-08 11:59:14

标签: xml xslt xsl-variable

我有这样的XML结构:

<TR>
    <Zeile status="done">
        <Typ>FUNCTION</Typ>
        <Name>PressKey</Name>
    </Zeile>
    <Zeile status="failed">
        <Typ>FUNCTION</Typ>
        <Name>PressKey</Name>
    </Zeile>
    <Zeile status="warn">
        <Typ>FUNCTION</Typ>
        <Name>PressKey</Name>
    </Zeile>
    <Zeile status="done">
        <Typ>FUNCTION</Typ>
        <Name>PressKey</Name>
    </Zeile>
</TR>

现在我想要使用最差的status来获取Element。

status值从最佳到最差的顺序:

done
ignoreFailed
ignoreExpected
passed
unknown
warn
failed

我该如何处理?最后,我希望在名为status的变量中将最差的worst_status作为整数,并在变量worst_status_string

中作为String

这是我尝试的(但由于禁止覆盖变量而失败):

<xsl:variable name="statusMapping">
    <statusMap>
        <status key="done" value="1"/>
        <status key="ignoreFailed" value="2"/>
        <status key="ignoreExpected" value="3"/>
        <status key="passed" value="4"/>
        <status key="unknown" value="5"/>
        <status key="warn" value="6"/>
        <status key="failed" value="7"/>
    </statusMap>
</xsl:variable>




<xsl:template match="//TR">
    <xsl:variable name="worst_status" select="1"/>

    <xsl:for-each select="Zeile">
        <xsl:variable name="current_status" select="@status"/>
        <xsl:for-each select="exslt:node-set($statusMapping)/statusMap/status">
            <xsl:if test="@key = $current_status">
                <xsl:if test="@value > $worst_status">
                    <xsl:variable name="worst_status" select="@value"/>
                </xsl:if>
            </xsl:if>
        </xsl:for-each>

        WorstStatus: <xsl:value-of select="$worst_status"/>
    </xsl:for-each>
</xsl:template>

1 个答案:

答案 0 :(得分:2)

按降序排序并取第一个:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
  xmlns:exsl="http://exslt.org/common"
  exclude-result-prefixes="exsl">

<xsl:variable name="statusMapping-rtf">
        <status key="done" value="1"/>
        <status key="ignoreFailed" value="2"/>
        <status key="ignoreExpected" value="3"/>
        <status key="passed" value="4"/>
        <status key="unknown" value="5"/>
        <status key="warn" value="6"/>
        <status key="failed" value="7"/>
</xsl:variable>

<xsl:variable name="statusMapping" select="exsl:node-set($statusMapping-rtf)"/>




<xsl:template match="TR">
    <xsl:variable name="worst_status">
      <xsl:for-each select="Zeile">
          <xsl:sort select="$statusMapping/status[@key = current()/@status]/@value" data-type="number" order="descending"/>
          <xsl:if test="position() = 1">
              <xsl:value-of select="$statusMapping/status[@key = current()/@status]/@value"/>
          </xsl:if>
      </xsl:for-each>
    </xsl:variable>

    <xsl:variable name="worst_status_string" select="$statusMapping/status[@value = $worst_status]/@key"/>

    <xsl:value-of select="concat($worst_status_string, ': ', $worst_status)"/>
</xsl:template>

</xsl:transform>

如果您确实需要将一个变量作为数字值,请添加<xsl:variable name="worst_status_number" select="number($worst_status)"/>