Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
353 views
in Technique[技术] by (71.8m points)

xpath - Total of true and false in current time

I have got this XML document. Is there way how to sort the nodes by ascending time and by brand and then count how many cars (samples), true and false was in this time from start time (always the lowest time in the group)?

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

Wanted result in HTML

Results after XSLT

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Consider to include what you have got next time in the question text. If you already know how to group and how to sort you can then easily process the sorted sequence and take the subsequence until each item and check the count of the items with a certain trend as follows:

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...