在格式化节点内排序

时间:2017-11-26 21:44:40

标签: xslt xslt-1.0 xslt-2.0

我需要显示XML,其中包含当天距离的总和,以便首先显示大多数公里的日期 我只能制作距离之和

输入

<output>
    <cars>
        <car>
          <id>1</id>
          <license>B-01-TST</license>
        </car>
        <car>
          <id>2</id>
          <license>IF-02-TST</license>
        </car>
    </cars>
    <distances>
        <distance>
          <id_car>1</id_car>
          <date>20110901</date>
          <distance>111</distance>
        </distance>     
        <distance>
          <id_car>1</id_car>
          <date>20110903</date>
          <distance>56</distance>
        </distance>
        <distance>
          <id_car>2</id_car>
          <date>20110901</date>
          <distance>92</distance>
        </distance>
        <distance>
          <id_car>2</id_car>
          <date>20110902</date>
          <distance>97</distance>
        </distance>
    </distances>
</output>

预期输出

<output>
<cars>
    <car>
        <id>1</id>
        <license>B-01-TST</license>
        <distance totalKm="Day 01: 111"/>
        <distance totalKm="Day 03: 56"/>
    </car>
    <car>
        <id>2</id>
        <license>IF-02-TST</license>
        <distance totalKm="Day 01: 92"/>
        <distance totalKm="Day 02: 97"/>
    </car>
</cars>

我无法复制所有XSL,因为它的代码太多了, 所以主要信息是这个

到目前为止,XSL看起来像这样:

<xsl:key name="byCarIdAndDate" match="distance" use="concat(id_car, '|', date)"/>
<xsl:key name="byCarId" match="distance" use="id_car"/>
<xsl:template match="car">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
            <xsl:apply-templates select="key('byCarId', id)"/>
        </xsl:copy>
    </xsl:template>
<xsl:template match="distance[generate-id()= generate-id(key('byCarIdAndDate', concat(id_car, '|', date))[1])]">
    <xsl:variable name="thisDate" select="key('byCarIdAndDate', concat(id_car, '|', date))"/>
    <xsl:variable name="sum" select="sum($thisDate/distance)"/>
    <!--make some sort by higher value -->
    <distance totalKm="Day {substring(date, 7, 2)}: {$sum}"/>
</xsl:template>

删除距离块我做这样模板匹配=&#34;距离|距离&#34;

1 个答案:

答案 0 :(得分:1)

我认为最好是诉诸<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> Click Here <i class="fa fa-bars" id="menu"></i> <div class="wrapper"> <div class="col-md-7 counselingTableDiv" id="counselingTableDivId"> <h3 class="text-center">Counseling Time Table</h3> <div class="table-responsive"> <table class="table table-bordered" id="counselingTable"> <thead> <tr> <th>Sat</th> <th>Sun</th> <th>Mon</th> <th>Tue</th> <th>Wed</th> <th>Thu</th> </tr> </thead> <tbody> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> </tbody> </table> <span id="deleteRoutineData" class="btn btn-success">Delete All Routine Data</span> <span class="btn btn-success" id="setNewRoutine">Set New Routine</span> </div> </div> <div class="col-md-5 counselingSchedulesDiv" id="counselingSchedulesDivId"> <div class="well"> <h3>Enter Counseling Schedules</h3> </div> <form action="" method="POST"> <div class="form-group"> <label for="day">Day:</label> <input type="text" name="day" class="form-control" id="day" placeholder="Day"> </div> <div class="form-group"> <label for="courseName">Course Name:</label> <input type="text" name="courseName" class="form-control" id="courseName" placeholder="Course Name"> </div> <div class="form-group"> <label for="couseCode">Course Code:</label> <input type="text" name="courseCode" class="form-control" id="courseCode" placeholder="Course Code"> </div> <div class="form-group"> <label for="time">Starting Time:</label> <input type="time" name="time" class="form-control" id="time"> </div> <div class="form-group"> <label for="time">Ending Time::</label> <input type="time" name="time" class="form-control" id="time"> </div> <button type="submit" id="submit">Submit</button> </form> </div> </div>然后按日期分组,并允许我们轻松地将一个小组相加:

xsl:for-each-group

http://xsltransform.net/3MvmrAX