已显示

时间:2015-07-13 12:19:45

标签: xslt max

我有一个问题是最多可以产生2个不同的值。

我有以下xml文件,即导出我的系统。

<?xml version="1.0" encoding="utf-8"?>
<racine Version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <index nom="Facturation" DateMAJ="20150520170532" HeureMAJ="17:05:32">
 <Parent nom="00000002B" DateMAJ="04/03/2015 14:51:00" HeureMAJ="14:51:00" type="PDL">
 <Attributs />
 </Parent>
 <Propriété IdExterne="" NomEquipement="00000002B" Numero="0" />
 <equipement obsolete="INTERDIT" numSerie="" numSerieClient="00000002" numSerieADS="00000002" type="SL7000" cfg="Standard-ENAMC">
 <Attributs />
 <idExt n="GDO" />
 <idExt n="CONTRAT" />
 <idExt n="LIVRAISON" />
 <idExt n="REF_CLI" />
 </equipement>
 <Meters>
 <Meter nom="00000002B" numSerieClient="00000002" numSerieADS="00000002" type="SL7000" fluide="1" cfg="Standard-ENAMC">
 <Item name="attachBegin" value="19850101" />
 <Item name="attachEnd" value="20500101" />
 </Meter>
 </Meters>
 <arrêté dateArrêté="20150301000000" indexCourant="False" etiquette="0">
 <ValeurIndex Libelle="IND_DON_PERIOD">M</ValeurIndex>
 <ValeurIndex Libelle="IND_DON_CONTRAT">F</ValeurIndex>
 <ValeurIndex Libelle="IND_DON_CALCMODE">A</ValeurIndex>
 <ValeurIndex Libelle="EA.R00">23334.000000</ValeurIndex>
 <ValeurIndex Libelle="EA.R01">5186.000000</ValeurIndex>
 <ValeurIndex Libelle="EA.R02">2790.000000</ValeurIndex>
 <ValeurIndex Libelle="EA.R03">8545.000000</ValeurIndex>
 <ValeurIndex Libelle="EA.R05">7778.000000</ValeurIndex>
 <ValeurIndex Libelle="EA.R06">7777.000000</ValeurIndex>
 <ValeurIndex Libelle="EA.R07">7779.000000</ValeurIndex>
 <ValeurIndex Libelle="EAE.R00">33.000000</ValeurIndex>
 <ValeurIndex Libelle="AEINDEX0">0.000000</ValeurIndex>
 <ValeurIndex Libelle="AEINDEX1">0.000000</ValeurIndex>
 <ValeurIndex Libelle="AEINDEX2">0.000000</ValeurIndex>
 <ValeurIndex Libelle="AEINDEX3">0.000000</ValeurIndex>
 <ValeurIndex Libelle="AEINDEX5">0.000000</ValeurIndex>
 <ValeurIndex Libelle="AEINDEX6">0.000000</ValeurIndex>
 <ValeurIndex Libelle="AEINDEX7">0.000000</ValeurIndex>
 <ValeurIndex Libelle="R1INDEX0">3754.000000</ValeurIndex>
 <ValeurIndex Libelle="PMAXVALUE0">1047</ValeurIndex>
 <ValeurIndex Libelle="PMAXDATE0">02/11/2015 17:10:00</ValeurIndex>
 <ValeurIndex Libelle="IND_DON_CFG">SL7000.STANDARD-ENAMC.1.0.2</ValeurIndex>
 </arrêté>
 </index>
</racine>

我的xlst转换如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text" version="1.0" encoding="ISO-8859-1" indent="yes"/>
 <!-- Parcours des noeuds "racine/index" -->
 <xsl:variable name="v_separateur">
 <xsl:text>,</xsl:text>
 </xsl:variable>

<!-- sort by date desc" --> 
<xsl:template match="/racine">
 <xsl:for-each select="index">
 <xsl:sort select="arrêté/@dateArrêté" order="descending" data-type="number"/>
 <xsl:call-template name="Arrete"/>
 </xsl:for-each>
 </xsl:template>


<xsl:template name="Arrete">
 <!-- Only Process OBIS* values ignore other -->
 <xsl:for-each select="arrêté/ValeurIndex[
 @Libelle='EA.R00'
 or @Libelle='EA.R01'
 or @Libelle='EA.R02'
 or @Libelle='EA.R03'
 or @Libelle='EA.R05'
 or @Libelle='EA.R06'
 or @Libelle='EA.R07'
 or @Libelle='EAE.R00'
 or @Libelle='AEINDEX0'
 or @Libelle='AEINDEX1'
 or @Libelle='AEINDEX2'
 or @Libelle='AEINDEX3'
 or @Libelle='AEINDEX5'
 or @Libelle='AEINDEX6'
 or @Libelle='AEINDEX7'
 or @Libelle='R1INDEX0'
 or @Libelle='PMAXVALUE0'
 ]
 ">

 <xsl:call-template name="ValeurIndex" />
 </xsl:for-each>
 </xsl:template>



<xsl:template name="ValeurIndex"> <!--match="ValeurIndex"-->
 <!-- SP_ID -->
 <xsl:value-of select="../../Parent/@nom"/>
 <xsl:value-of select="$v_separateur"/>
 <!-- METER_NUMBER-->
 <xsl:value-of select="../../Meter/@numSerie"/>
 <xsl:value-of select="$v_separateur"/>
 <!-- DATE & TIME-->
 <xsl:value-of select="../../arrêté/@dateArrêté"/>
 <xsl:value-of select="$v_separateur"/>

 <xsl:choose>
 <xsl:when test="@Libelle = 'EA.R00'">KWH,PUNTA</xsl:when>
 <xsl:when test="@Libelle = 'EA.R01'">KWH,VALLE</xsl:when>
 <xsl:when test="@Libelle = 'EA.R02'">KWH,LLANO</xsl:when>
 <xsl:when test="@Libelle = 'EA.R03'">KW,</xsl:when> 
<xsl:when test="@Libelle = 'EA.R05'">KWH,</xsl:when> 
<xsl:when test="@Libelle = 'EA.R06'">KVH,ER_Q2</xsl:when>
 <xsl:when test="@Libelle = 'EA.R07'">KVH,ER_Q3</xsl:when> 
<xsl:when test="@Libelle = 'EAE.R00'">KVH,ER_Q4</xsl:when>
 <xsl:when test="@Libelle = 'AEINDEX0'">KWH,PUNTA_SA</xsl:when> 
<xsl:when test="@Libelle = 'AEINDEX1'">KWH,VALLE_SA</xsl:when> 
<xsl:when test="@Libelle = 'AEINDEX2'">KWH,LLANO_SA</xsl:when> 
<xsl:when test="@Libelle = 'AEINDEX3'">KWH,</xsl:when> 
<xsl:when test="@Libelle = 'AEINDEX5'">KWH,ENERSAL</xsl:when> 
<xsl:when test="@Libelle = 'AEINDEX6'">KW,PUNTA</xsl:when> 
<xsl:when test="@Libelle = 'AEINDEX7'">KW,VALLE</xsl:when> 
<xsl:when test="@Libelle = 'R1INDEX0'">KW,LLANO</xsl:when> 
<xsl:when test="@Libelle = 'PMAXVALUE0'">KW, PUNTA_SA</xsl:when> 
<xsl:otherwise><xsl:value-of select="Param[@code = 'TYPE_EQP']/@value"/></xsl:otherwise>
 </xsl:choose>

<xsl:text>,</xsl:text>
 <xsl:value-of select="."/>
 <xsl:text>&#xD;&#xA;</xsl:text>
 </xsl:template>
</xsl:stylesheet>

我获得的结果是:

00000002,00000002,28/02/2015 00:00:00,KWH,PUNTA,23.334
00000002,00000002,28/02/2015 00:00:00,KWH,VALLE,5.186
00000002,00000002,28/02/2015 00:00:00,KWH,LLANO,2.79
00000002,00000002,28/02/2015 00:00:00,KW,,8.545
00000002,00000002,28/02/2015 00:00:00,KWH,,7.778
00000002,00000002,28/02/2015 00:00:00,KVH,ER_Q2,7.777
00000002,00000002,28/02/2015 00:00:00,KVH,ER_Q3,7.779
00000002,00000002,28/02/2015 00:00:00,KVH,ER_Q4,0.033
00000002,00000002,28/02/2015 00:00:00,KWH,PUNTA_SA,3234
00000002,00000002,28/02/2015 00:00:00,KWH,VALLE_SA,342
00000002,00000002,28/02/2015 00:00:00,KWH,LLANO_SA,342
00000002,00000002,28/02/2015 00:00:00,KWH,,0
00000002,00000002,28/02/2015 00:00:00,KWH,ENERSAL,0
00000002,00000002,28/02/2015 00:00:00,KW,PUNTA,0
00000002,00000002,28/02/2015 00:00:00,KW,VALLE,0
00000002,00000002,28/02/2015 00:00:00,KW,LLANO,3.754
00000002,00000002,28/02/2015 00:00:00,KW, PUNTA_SA,0.104

但我的问题是我想在上面的文件中添加一个新行,其中显示最大值:

<xsl:when test="@Libelle = 'EA.R00'">

<xsl:when test="@Libelle = 'EA.R02'">

在上面的例子中将是EA.R00(23.334)。所以我想添加一行: 00000002,00000002,28 / 02/2015 00:00:00,KWH ,, 23.334 你需要注意的是,在我需要char KW的值之后,我希望值为最大值。

任何人都可以帮我解决这个问题吗?

谢谢

1 个答案:

答案 0 :(得分:0)

如果(!)我理解你的问题,你需要添加如下内容:

<xsl:choose>
    <xsl:when test="arrêté/ValeurIndex[@Libelle='EA.R00'] > arrêté/ValeurIndex[@Libelle='EA.R02']">
        <xsl:value-of select="arrêté/ValeurIndex[@Libelle='EA.R00']"/>
    </xsl:when>
    <xsl:otherwise>
        <xsl:value-of select="arrêté/ValeurIndex[@Libelle='EA.R02']"/>
    </xsl:otherwise>
</xsl:choose>

到处理index节点的部分。

为避免重复相同的代码两次(并使代码更具可读性),我建议您将其重组为:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="ISO-8859-1"/>

<!-- Parcours des noeuds "racine/index" -->
<xsl:variable name="v_separateur">
    <xsl:text>,</xsl:text>
</xsl:variable>

<!-- sort by date desc" --> 
<xsl:template match="/racine">
    <xsl:apply-templates select="index">
        <xsl:sort select="arrêté/@dateArrêté" order="descending" data-type="number"/>
        </xsl:apply-templates>
</xsl:template> 

<xsl:template match="index">        
    <!-- get repeating values once -->
    <xsl:variable name="constants">
        <!-- SP_ID -->
        <xsl:value-of select="Parent/@nom"/>
        <xsl:value-of select="$v_separateur"/>
        <!-- METER_NUMBER-->
        <xsl:value-of select="Meter/@numSerie"/>
        <xsl:value-of select="$v_separateur"/>
        <!-- DATE & TIME-->
        <xsl:value-of select="arrêté/@dateArrêté"/>
        <xsl:value-of select="$v_separateur"/>
    </xsl:variable>
    <!-- Only Process OBIS* values ignore other -->
    <xsl:apply-templates select="arrêté/ValeurIndex[
           @Libelle='EA.R00'
        or @Libelle='EA.R01'
        or @Libelle='EA.R02'
        or @Libelle='EA.R03'
        or @Libelle='EA.R05'
        or @Libelle='EA.R06'
        or @Libelle='EA.R07'
        or @Libelle='EAE.R00'
        or @Libelle='AEINDEX0'
        or @Libelle='AEINDEX1'
        or @Libelle='AEINDEX2'
        or @Libelle='AEINDEX3'
        or @Libelle='AEINDEX5'
        or @Libelle='AEINDEX6'
        or @Libelle='AEINDEX7'
        or @Libelle='R1INDEX0'
        or @Libelle='PMAXVALUE0'
        ]">
        <xsl:with-param name="constants" select="$constants"/>
    </xsl:apply-templates>
    <!-- ADD NEW ROW -->
    <xsl:value-of select="$constants"/>
    <xsl:text>KWH</xsl:text>
    <xsl:value-of select="$v_separateur"/>
    <xsl:value-of select="$v_separateur"/>
    <xsl:choose>
        <xsl:when test="arrêté/ValeurIndex[@Libelle='EA.R00'] > arrêté/ValeurIndex[@Libelle='EA.R02']">
            <xsl:value-of select="arrêté/ValeurIndex[@Libelle='EA.R00']"/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="arrêté/ValeurIndex[@Libelle='EA.R02']"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

<xsl:template match="ValeurIndex"> <!--match="ValeurIndex"-->
    <xsl:param name="constants"/>
    <xsl:value-of select="$constants"/>
    <xsl:choose>
        <xsl:when test="@Libelle = 'EA.R00'">KWH,PUNTA</xsl:when>
        <xsl:when test="@Libelle = 'EA.R01'">KWH,VALLE</xsl:when>
        <xsl:when test="@Libelle = 'EA.R02'">KWH,LLANO</xsl:when>
        <xsl:when test="@Libelle = 'EA.R03'">KW,</xsl:when> 
        <xsl:when test="@Libelle = 'EA.R05'">KWH,</xsl:when> 
        <xsl:when test="@Libelle = 'EA.R06'">KVH,ER_Q2</xsl:when>
        <xsl:when test="@Libelle = 'EA.R07'">KVH,ER_Q3</xsl:when> 
        <xsl:when test="@Libelle = 'EAE.R00'">KVH,ER_Q4</xsl:when>
        <xsl:when test="@Libelle = 'AEINDEX0'">KWH,PUNTA_SA</xsl:when> 
        <xsl:when test="@Libelle = 'AEINDEX1'">KWH,VALLE_SA</xsl:when> 
        <xsl:when test="@Libelle = 'AEINDEX2'">KWH,LLANO_SA</xsl:when> 
        <xsl:when test="@Libelle = 'AEINDEX3'">KWH,</xsl:when> 
        <xsl:when test="@Libelle = 'AEINDEX5'">KWH,ENERSAL</xsl:when> 
        <xsl:when test="@Libelle = 'AEINDEX6'">KW,PUNTA</xsl:when> 
        <xsl:when test="@Libelle = 'AEINDEX7'">KW,VALLE</xsl:when> 
        <xsl:when test="@Libelle = 'R1INDEX0'">KW,LLANO</xsl:when> 
        <xsl:when test="@Libelle = 'PMAXVALUE0'">KW, PUNTA_SA</xsl:when> 
        <xsl:otherwise>
            <xsl:value-of select="Param[@code = 'TYPE_EQP']/@value"/>
        </xsl:otherwise>
    </xsl:choose>
    <xsl:value-of select="$v_separateur"/>
    <xsl:value-of select="."/>
    <xsl:text>&#xD;&#xA;</xsl:text>
</xsl:template>

</xsl:stylesheet>