3

I have a XML-Structure like this:

<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>

Now I want to get the Element with the worst status.

The order of the status values from best to worst:

done
ignoreFailed
ignoreExpected
passed
unknown
warn
failed

How do I approach this? At the end I want the worst status as Integer in a variable called worst_status and as String in the variable worst_status_string


This is what I tried (but failed because of forbidden overwriting of variables):

<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>
4

1 回答 1

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)"/>.

于 2016-04-08T12:10:52.443 回答