逐步平均和总和XSLT / XPATH

时间:2017-07-11 02:20:51

标签: xml xslt xpath tags

这是我的一个xml文件:

<?xml version="1.0"  ?>             
<output>                
  <cars>                
   <car>                
    <id>1</id>              
    <brand>Ferrari</brand>              
    <type>F12</type>                
    <license>B-1</license>              
   </car>               
   <car>                
    <id>2</id>              
    <brand>Volkwagen</brand>                
    <type>Golf</type>               
    <license>B-3</license>              
   </car>               
  </cars>               
  <distances>               
   <distance>               
    <id_car>1</id_car>              
    <date>20110901</date>               
    <distance>111</distance>                
   </distance>              
   <distance>               
    <id_car>1</id_car>              
    <date>20110902</date>               
    <distance>23</distance>             
   </distance>              
   <distance>               
    <id_car>1</id_car>              
    <date>20110903</date>               
    <distance>0</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>87</distance>             
   </distance>              
   <distance>               
    <id_car>2</id_car>              
    <date>20110903</date>               
    <distance>132</distance>                
  </distance>               
 </distances>               
</output>               

以下是我正在处理的xsl文件: (有没有办法以通用方式放置id_car / id?因为现在我必须手动为我的xml文件中的每个id / id_car设置一个值来参加我的计划。)

   <?xml version="1.0" encoding="UTF-8"?>                   
   <xsl:stylesheet version="1.0"                    
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">


  <xsl:template match="/">

  <html>                    
  <body>                    
  <h1>Cars</h1>             
   ID: <xsl:value-of select="output/cars/car/id"/><br />
   TOTAL KM: <xsl:value-of select="sum(/output/distances/distance[id_car='1']/distance)"/><br />
   KM/DAY: <xsl:value-of select="sum(/output/distances/distance[id_car='1']/distance) div count(/output/distances/distance[id_car='1']/date)"/> 


 </body>                    
 </html>                    
 </xsl:template>


 </xsl:stylesheet>                  

2 个答案:

答案 0 :(得分:1)

XSLT 1.0具有内置的 key 机制,专门用于处理此类情况,需要解析XML的两个部分之间的交叉引用。使用它将使您的代码更优雅,更高效。

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:key name="dist" match="distance" use="id_car" />

<xsl:template match="/output">
    <html>
        <body>
            <h1>Cars</h1>
            <xsl:for-each select="cars/car">
                <xsl:variable name="distances" select="key('dist', id)" />
                <xsl:variable name="total" select="sum($distances/distance)" />
                ID: <xsl:value-of select="id"/><br/>
                TOTAL KM: <xsl:value-of select="$total"/><br/>
                KM/DAY: <xsl:value-of select="$total div count($distances)"/> <hr/>
            </xsl:for-each>
        </body>
    </html>
</xsl:template>

</xsl:stylesheet>

您可以在此处阅读有关XSLT密钥的更多信息:https://www.xml.com/pub/a/2002/02/06/key-lookups.html

答案 1 :(得分:0)

如果您将ID作为变量并在每个谓词上调用它,那么每次代码循环时您都会动态更新该谓词。例如,使用for-each:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <html>
            <body>
                <h1>Cars</h1>
                <xsl:for-each select="output/cars/car">
                    <xsl:variable name="id" select="id"/>
                    ID: <xsl:value-of select="$id"/><br/>
                    TOTAL KM: <xsl:value-of select="sum(/output/distances/distance[id_car=$id]/distance)"/><br/>
                    KM/DAY: <xsl:value-of select="sum(/output/distances/distance[id_car=$id]/distance) div count(/output/distances/distance[id_car=$id]/date)"/> <br/>
                </xsl:for-each>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

Apply-template也是一种有效的方法,但变量的使用是其中的重要部分。