从和 xml 中选择最小值而不进行排序 [英] Selecting the lowest value from and xml without sorting

查看:20
本文介绍了从和 xml 中选择最小值而不进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 xslt 将 xml 文档转换为 html 表,现在我想要做的是更改包含最低价格"值的单元格的背景颜色,而不对我的列表进行排序.

I'm transforming an xml document to an html table using xslt, and now what I want to do is to change the background color of the cell containing the lowest "price" value without sorting my list.

我是新来的,所以我正在做的是遵循 W3C 学校的例子.xml 文件如下所示:

I'm new on this, so what I'm doing is following the W3C schools examples. The xml file looks like this:

    <?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<catalog>
<cd>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
</cd>
<cd>
    <title>Black angel</title>
    <artist>Savage Rose</artist>
    <country>EU</country>
    <company>Mega</company>
    <price>11.90</price>
    <year>1995</year>
</cd>
<cd>
    <title>For the good times</title>
    <artist>Kenny Rogers</artist>
    <country>UK</country>
    <company>Mucik Master</company>
    <price>8.70</price>
    <year>1995</year>
</cd>
</catalog>

我想要获得的东西与此类似,但没有按价格对列表中的元素进行排序.所以我想在XML文档中保持原来的顺序.

And what I want to obtain is something similar to this but without ordering the elements in the list by price. So I want to maintain the original order in the XML document.

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!-- Edited by XMLSpy® -->
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
     <html>
      <body>
        <h2>My CD Collection</h2>
        <table border="1">
          <tr bgcolor="#9acd32">
          <th>Title</th>
          <th>Artist</th>
          <th>Price</th>
        </tr>
        <xsl:for-each select="catalog/cd">
        <xsl:sort select="price" order="ascending" data-type="number"/>
          <tr>
            <td><xsl:value-of select="title"/></td>
            <td><xsl:value-of select="artist"/></td>
            <xsl:choose>
              <xsl:when test="(position() = 1)">
                <td bgcolor="#ff00ff">
                <xsl:value-of select="price"/></td>
              </xsl:when>
            <xsl:otherwise>
                <td><xsl:value-of select="price"/></td>
            </xsl:otherwise>
            </xsl:choose>
          </tr>
        </xsl:for-each>
        </table>
      </body>
      </html>
    </xsl:template>
    </xsl:stylesheet>

预先感谢您的帮助.

我想我已经为我的问题找到了解决方案:

I think I have found a solution for my question:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
  <html>
  <body>
    <h2>My CD Collection</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th>Title</th>
        <th>Artist</th>
        <th>Price</th>
      </tr>
      <xsl:for-each select="catalog/cd">
       <tr>
        <td><xsl:value-of select="title"/></td>
        <td><xsl:value-of select="artist"/></td>
        <xsl:choose>
          <xsl:when test="price=/catalog/cd/price[not(. &gt; ../../cd/price)][1]">
            <td bgcolor="#ff00ff">
            <xsl:value-of select="price"/></td>
          </xsl:when>
        <xsl:otherwise>
            <td><xsl:value-of select="price"/></td>
        </xsl:otherwise>
        </xsl:choose>
      </tr>

      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>

推荐答案

您似乎已经回答了您的问题,但我将在这里介绍一些其他内容.

You seem to have answered your question already, but I will throw in a couple of other things in here.

(1) 通过在样式表或模板中添加一个变量进行一次计算,您可以通过 <cd/> 元素避免在每次迭代时重新计算最低价格:

(1) You can avoid re-calculating the minimum price at each iteration through the <cd/> elements by adding a variable to the stylesheet or the template to do the calculation once:

<xsl:variable name="least" select="math:min(/catalog/cd/price)"/>

(2) $least"变量声明使用 EXSLT math:min 函数(命名空间是 xmlns:math="http://exslt.org/math"),它可能对您的 XSLT 1.0 处理器可用.如果您有 2.0 处理器,则可以使用内置的min()".

(2) That "$least" variable declaration uses the EXSLT math:min function (namespace is xmlns:math="http://exslt.org/math"), which may be available to your XSLT 1.0 processor. If you have a 2.0 processor, you can use the built-in "min()".

如果不出意外,min() 函数对于下一个人来说更具可读性.

If nothing else, a min() function is more readable for the next person.

(3) 这纯粹是风格上的,但是您可以通过使用 xsl:attribute 来减少分支.

(3) This is purely stylistic, but you can reduce the branching a bit by using xsl:attribute.

<td>
    <xsl:if test="price=$lowest">
        <xsl:attribute name="bgcolor">#ff00ff</xsl:attribute>
    </xsl:if>
    <xsl:value-of select="price"/>
</td>

这篇关于从和 xml 中选择最小值而不进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆