当前时间的真假总数

时间:2018-04-17 08:29:20

标签: xpath xslt-2.0 xpath-2.0

我有这个XML文档。是否有办法按升序时间和品牌对节点进行排序,然后计算从开始时间(始终是组中的最低时间)的这段时间内有多少辆汽车(样本),真假?

<?xml version="1.0" encoding="UTF-8"?>
<trade>
    <car time="1950" brand="audi" trend="true">
    </car>
    <car time="1200" brand="renault" trend="true">
    </car>
    <car time="1000" brand="audi" trend="true">
    </car>
    <car time="2800" brand="renault" trend="true">
    </car>
    <car time="2000" brand="audi" trend="true">
    </car>
    <car time="1500" brand="renault" trend="true">
    </car>
    <car time="1900" brand="audi" trend="false">
    </car>
    <car time="2300" brand="audi" trend="false">
    </car>
    <car time="2100" brand="renault" trend="false">
    </car>
</trade>

通缉HTML结果

Results after XSLT

1 个答案:

答案 0 :(得分:1)

考虑在问题文本中包含您下次获得的内容。如果您已经知道如何分组以及如何分类,那么您可以轻松处理已排序的序列并将后续序列直到每个项目并检查具有特定趋势的项目的计数,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    expand-text="yes"
    version="3.0">


  <xsl:output method="html" indent="yes" html-version="5"/>

  <xsl:template match="/">
    <html>
      <head>
        <title>.NET XSLT Fiddle Example</title>
      </head>
      <body>
        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="trade">
      <xsl:for-each-group select="car" group-by="@brand">
          <h2>{current-grouping-key()}</h2>
          <table>
              <thead>
                  <tr>
                      <th>Total of samples</th>
                      <th>Time</th>
                      <th>Total of TRUE</th>
                      <th>Total of FALSE</th>
                  </tr>
              </thead>
              <xsl:variable name="sorted-cars" as="element(car)*">
                  <xsl:perform-sort select="current-group()">
                      <xsl:sort select="xs:integer(@time)"/>
                  </xsl:perform-sort>
              </xsl:variable>
              <tbody>
                  <xsl:for-each select="$sorted-cars">
                      <tr>
                          <td>{position()}</td>
                          <td>{@time}</td>
                          <xsl:variable name="car-group" select="subsequence($sorted-cars, 1, position())"/>
                          <td>{count($car-group[@trend = 'true'])}</td>
                          <td>{count($car-group[@trend = 'false'])}</td>
                      </tr>
                  </xsl:for-each>
              </tbody>
          </table>
      </xsl:for-each-group>
  </xsl:template>

</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/nc4NzQ1