xslt maximum between 2 values that already showed - xslt

I have a problem to make the max of 2 different values.
I have the below xml file, that is the export of my system.
<?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>
My xlst transformation is as I show below:
<?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>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
The result that I obtained is:
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
But my question is that I want to add a new row to the above file, where show the maximum between:
<xsl:when test="#Libelle = 'EA.R00'">
and
<xsl:when test="#Libelle = 'EA.R02'">
In the above example will be the EA.R00 (23.334). So I want to add the row:
00000002,00000002,28/02/2015 00:00:00,KWH,,23.334
You have to note that I want the value, after the value I need the char KW, for the maximum value.
Anybody can help me with this issue?
Thanks

If (!) I understand your question correctly, you need to add something like:
<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>
to the part that processes the index nodes.
To avoid repeating the same code twice (and also make your code more readable), I would suggest you restructure it as:
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>
</xsl:text>
</xsl:template>
</xsl:stylesheet>

Related

Convert date format from YYYY-MM-DD (2018-08-21) to MMM-DD-YYY (Aug-21-2018)

Looking to see how I can convert the following date format using xsl 1.0:
YYYY-MM-DD
to
MMM-DD-YYY
For example:
2018-08-21
to
Aug-21-2018
Thank you!!
This would be much easier in XSLT 2.0 or greater using format-date():
format-date(xs:date('2018-08-21'), '[MNn,*-3]-[D]-[Y]')
The XSLT 1.0 solution below uses substring() to identify the date components, a named template with a parameter to translate the month number into an abbreviation, and then concat() to generate the final result.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<xsl:call-template name="MMM-DD-YYY">
<xsl:with-param name="date" select="'2018-08-21'"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="MMM-DD-YYY">
<xsl:param name="date"/>
<xsl:variable name="year" select="substring($date, 1,4)"/>
<xsl:variable name="day" select="substring($date, 9, 2)"/>
<xsl:variable name="month">
<xsl:call-template name="month-abbr">
<xsl:with-param name="month" select="number(substring($date, 6, 2))"/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="concat($month,'-',$day,'-',$year)"/>
</xsl:template>
<xsl:template name="month-abbr">
<xsl:param name="month"/>
<xsl:choose>
<xsl:when test="$month = 1">Jan</xsl:when>
<xsl:when test="$month = 2">Feb</xsl:when>
<xsl:when test="$month = 3">Mar</xsl:when>
<xsl:when test="$month = 4">Apr</xsl:when>
<xsl:when test="$month = 5">May</xsl:when>
<xsl:when test="$month = 6">Jun</xsl:when>
<xsl:when test="$month = 7">Jul</xsl:when>
<xsl:when test="$month = 8">Aug</xsl:when>
<xsl:when test="$month = 9">Sep</xsl:when>
<xsl:when test="$month = 10">Oct</xsl:when>
<xsl:when test="$month = 11">Nov</xsl:when>
<xsl:when test="$month = 12">Dec</xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>

xslt if not value, no row

I have this xml that needs to be transformed:
<?xml version="1.0" encoding="utf-8"?>
<racine>
<index>
<Parent nom="00000002" Name="" Address="" />
<Meter numSerie="00000002" />
<arrêté dateArrêté="28/02/2015 00:00:00">
<ValeurIndex Libelle="PMAXVALUE0">0.104</ValeurIndex>
</arrêté>
</index>
<index>
<Parent nom="00000002B" Name="" Address="" />
<Meter numSerie="" />
<arrêté dateArrêté="28/02/2015 00:00:00">
<ValeurIndex Libelle="R1INDEX0">3.754</ValeurIndex>
<ValeurIndex Libelle="PMAXVALUE0">1.047</ValeurIndex>
</arrêté>
</index>
</racine>
The xslt that make the transformation is:
<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:text>
</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="arrêté/ValeurIndex[#Libelle='EA.R02']"/>
<xsl:text>
</xsl:text>
</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>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
I want to have just the lines that we have some value, so in this case:
00000002,00000002,28/02/2015 00:00:00,KW, PUNTA_SA,0.104
00000002B,,28/02/2015 00:00:00,KW,LLANO,3.754
00000002B,,28/02/2015 00:00:00,KW, PUNTA_SA,1.047
But as we put the maximum of EA.R00 and EA.R02 I will have a row without date and without data. I want that in case that both values dont exist dont write anything. As the xslt is right now, I will obtain:
00000002,00000002,28/02/2015 00:00:00,KW, PUNTA_SA,0.104
00000002,00000002,28/02/2015 00:00:00,KWH,,
00000002B,,28/02/2015 00:00:00,KW,LLANO,3.754
00000002B,,28/02/2015 00:00:00,KW, PUNTA_SA,1.047
00000002B,,28/02/2015 00:00:00,KWH,,
(I dont want to see the 2nd and 5th row)
Thanks
The extra lines your refer to are output by the code after the comment <!-- ADD NEW ROW -->. What you can do is wrap this code in an xsl:if to check if either EA.R00 or EA.R02 exists.
Try this:
<!-- ADD NEW ROW -->
<xsl:if test="arrêté/ValeurIndex[#Libelle='EA.R00' or #Libelle='EA.R02']">
<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:text>
</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="arrêté/ValeurIndex[#Libelle='EA.R02']"/>
<xsl:text>
</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:if>

xslt code choose between maximum of 2 values

I have this xml file.
<?xml version="1.0" encoding="utf-8"?>
<racine>
<index>
<Parent nom="00000002" Name="" Address="" />
<Meter numSerie="00000002" />
<arrêté dateArrêté="28/02/2015 00:00:00">
<ValeurIndex Libelle="Val1">23.334</ValeurIndex>
<ValeurIndex Libelle="Val2">5.186</ValeurIndex>
<ValeurIndex Libelle="Val3">2.79</ValeurIndex>
</arrêté>
</index>
</racine>
and I would like to convert it in a txt file, where I need to add a new row with the value of max between Val1 and Val2, as the example below.
Val1,23.334
Val2,5.186
Val3,2.79
MaxVal1Vl2,23.334
What I created it was something like:
<xsl:choose>
<xsl:when test="#Libelle = 'Val1'">Val1</xsl:when>
<xsl:when test="#Libelle = 'Val2'">Val2</xsl:when>
<xsl:when test="#Libelle = 'Val3'">Val3</xsl:when>
</xsl:choose>
<xsl:text>,</xsl:text>
<xsl:value-of select="."/>
<xsl:text>
</xsl:text>
how can I add the 4 row being the max of Val1 and Val2?
Thanks
Try:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="utf-8"/>
<xsl:template match="/racine">
<xsl:for-each select="index/arrêté/ValeurIndex">
<xsl:value-of select="#Libelle"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="."/>
<xsl:text>
</xsl:text>
</xsl:for-each>
<xsl:variable name="v1" select="index/arrêté/ValeurIndex[#Libelle='Val1']" />
<xsl:variable name="v2" select="index/arrêté/ValeurIndex[#Libelle='Val2']" />
<xsl:text>MaxVal1Vl2,</xsl:text>
<xsl:choose>
<xsl:when test="$v1 > $v2">
<xsl:value-of select="$v1"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$v2"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>

Convert date from DD-MMM-YYYY to YYYYMMDD format in xslt 1.0

How we can convert the date format from DD-MMM-YYYY to YYYY-MM-DD in XSLT.
10-JAN-2013 TO 20130110
in XSLT 1.0
This is very straightforward with an xsl:choose element, and doesn't require any extensions.
This stylesheet
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:strip-space elements="*"/>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/root">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="date">
<xsl:copy>
<xsl:call-template name="date">
<xsl:with-param name="dd-mmm-yyyy" select="."/>
</xsl:call-template>
</xsl:copy>
</xsl:template>
<xsl:template name="date">
<xsl:param name="dd-mmm-yyyy"/>
<xsl:variable name="dd" select="substring-before($dd-mmm-yyyy, '-')"/>
<xsl:variable name="mmm-yyyy" select="substring-after($dd-mmm-yyyy, '-')"/>
<xsl:variable name="mmm" select="substring-before($mmm-yyyy, '-')"/>
<xsl:variable name="yyyy" select="substring-after($mmm-yyyy, '-')"/>
<xsl:value-of select="$yyyy"/>
<xsl:choose>
<xsl:when test="$mmm = 'JAN'">01</xsl:when>
<xsl:when test="$mmm = 'FEB'">02</xsl:when>
<xsl:when test="$mmm = 'MAR'">03</xsl:when>
<xsl:when test="$mmm = 'APR'">04</xsl:when>
<xsl:when test="$mmm = 'MAY'">05</xsl:when>
<xsl:when test="$mmm = 'JUN'">06</xsl:when>
<xsl:when test="$mmm = 'JUL'">07</xsl:when>
<xsl:when test="$mmm = 'AUG'">08</xsl:when>
<xsl:when test="$mmm = 'SEP'">09</xsl:when>
<xsl:when test="$mmm = 'OCT'">10</xsl:when>
<xsl:when test="$mmm = 'NOV'">11</xsl:when>
<xsl:when test="$mmm = 'DEC'">12</xsl:when>
</xsl:choose>
<xsl:value-of select="$dd"/>
</xsl:template>
</xsl:stylesheet>
Applied to this XML data
<?xml version="1.0" encoding="UTF-8"?>
<root>
<date>10-JAN-2013</date>
<date>04-JUL-1776</date>
<date>31-DEC-1999</date>
</root>
Produces this output
<?xml version="1.0" encoding="utf-8"?>
<root>
<date>20130110</date>
<date>17760704</date>
<date>19991231</date>
</root>
If you can use node-set as extension to your xslt 1.0 processor you can try this.
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="xml" indent="yes" />
<xsl:variable name="date" select="'10-JAN-2013'" />
<xsl:variable name="month_data_tmp">
<month short="JAN" nr="01" />
<!--- and so on for each month -->
</xsl:variable>
<xsl:variable name="month_data" select="exsl:node-set($month_data_tmp)" />
<xsl:template name="format_date" >
<xsl:param name ="date" />
<xsl:variable name ="day" select="substring-before($date, '-')" />
<xsl:variable name ="month_and_year" select="substring-after($date, '-')" />
<xsl:variable name ="year" select="substring-after($month_and_year, '-')" />
<xsl:variable name ="month" select="substring-before($month_and_year, '-')" />
<xsl:value-of select="$year"/>
<xsl:value-of select="$month_data/month[#short=$month]/#nr"/>
<xsl:value-of select="$day"/>
</xsl:template>
<xsl:template match="/" >
<xsl:call-template name="format_date" >
<xsl:with-param name ="date" select="$date"/>
</xsl:call-template>
</xsl:template>
The output will be:
20130110
Update doe to additional question in command:
You can call the template at any place where you have used <xsl:value-of select="$date"/>before.
<result>
<xsl:call-template name="format_date" >
<xsl:with-param name ="date" select="$date"/>
</xsl:call-template>
</result>
Or you can assign the result to an new variable and use this.
<xsl:variable name="newdate">
<xsl:call-template name="format_date" >
<xsl:with-param name ="date" select="$date"/>
</xsl:call-template>
</xsl:variable>
<result>
<xsl:value-of select="$newdate"/>
</result>
I realise this thread is quite out of date now, but thought I'd share my template in case someone wanted to save some time by copying and pasting it.
Thanks to the answers above as this is based on them. My template simply converts from dd/MM/yyyy to yyyy-MM-dd; and also from dd/MM/yyyy HH:mm:ss to yyyy-MM-ddTHH:mm:ss.
<!--
Template Name: Date
Description: Takes a date in the format dd/MM/yyyy and outputs it in the format yyyy-mm-dd
Additionally will take a datetime in the format dd/MM/yyyy HH:mm:ss and output in the format yyyy-MM-ddTHH:mm:ss
-->
<xsl:template name="date">
<xsl:param name="slashFormattedDate"/>
<xsl:param name="hasTime"/>
<xsl:variable name="dd" select="substring-before($slashFormattedDate, '/')"/>
<xsl:variable name="monthYear" select="substring-after($slashFormattedDate, '/')"/>
<xsl:variable name="mm" select="substring-before($monthYear, '/')"/>
<xsl:variable name="yyyyTemp" select="substring-after($monthYear, '/')"/>
<xsl:variable name="yyyy">
<xsl:choose>
<xsl:when test="$hasTime='Y'">
<xsl:value-of select="substring-before($yyyyTemp, ' ')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$yyyyTemp"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:choose>
<xsl:when test="$hasTime='Y'">
<xsl:value-of select="$yyyy"/>-<xsl:value-of select="$mm"/>-<xsl:value-of select="$dd"/>T<xsl:value-of select="substring-after($yyyyTemp, ' ')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$yyyy"/>-<xsl:value-of select="$mm"/>-<xsl:value-of select="$dd"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

Default Namespace Issue during translation using xsl

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="no" indent="yes" />
<xsl:strip-space elements="*" />
<!-- <xsl:template match="//edx:rule" xmlns:edx="http://www.cisco.com/BRL">
<xsl:apply-templates />
</xsl:template> -->
<xsl:template match="*">
<xsl:element name="{local-name(.)}">
<xsl:apply-templates select="#* | node()" />
</xsl:element>
</xsl:template>
<xsl:template match="lhs" name="lhsTemplate">
<xsl:element name="lhs">
<xsl:choose>
<xsl:when test="incident != ''">
<xsl:for-each select="incident">
<xsl:element name="freeForm">
<xsl:element name="text">
<xsl:apply-templates select="." />
<!-- <xsl:call-template name = "incidentTemplate"/> -->
</xsl:element>
</xsl:element>
</xsl:for-each>
</xsl:when>
<xsl:when test="evaluate != ''">
<xsl:for-each select="evaluate">
<xsl:element name="freeForm">
<xsl:element name="text">
<xsl:apply-templates select="." />
<!-- <xsl:call-template name = "evaluateTemplate"/> -->
</xsl:element>
</xsl:element>
</xsl:for-each>
</xsl:when>
<xsl:when test="ruleReference != ''">
<xsl:value-of select="ruleReference" />
</xsl:when>
<xsl:otherwise>
<xsl:text> no elements encountered in lhs block</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:element>
</xsl:template>
<xsl:template match="rhs" name="rhsTemplate">
<xsl:element name="rhs">
<xsl:choose>
<xsl:when test="modify != ''">
<xsl:for-each select="modify">
<xsl:element name="freeForm">
<xsl:element name="text">
<xsl:apply-templates select="." />
</xsl:element>
</xsl:element>
</xsl:for-each>
</xsl:when>
<xsl:when test="tryCatchBlock/try/callFunction != ''">
<xsl:for-each select="tryCatchBlock/try/callFunction">
<xsl:element name="freeForm">
<xsl:element name="text">
<xsl:apply-templates select="." />
<xsl:text>;</xsl:text>
</xsl:element>
</xsl:element>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:text> no elements encountered in Try block</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:element>
</xsl:template>
<!-- FIELE VALUES TEMPLATE -->
<xsl:template match="fieldValues" name="fieldValuesTemplate">
<xsl:for-each select="fieldValue" >
<!-- <xsl:if test="field!=''">
<xsl:value-of select="field"/>
<xsl:text>=</xsl:text>
</xsl:if> -->
<xsl:choose>
<xsl:when test="value/literal !=''">
<xsl:value-of select="value/literal"/>
</xsl:when>
<xsl:when test="value/formula !=''">
<xsl:apply-templates select="value/formula"/>
</xsl:when>
<xsl:when test="value/callMethodOnBoundVariable != ''">
<xsl:apply-templates select="value/callMethodOnBoundVariable"/>
</xsl:when>
<xsl:when test="value/callApi !=''">
<xsl:apply-templates select="."/>
</xsl:when>
<xsl:when test="value/boundVariable !=''">
<xsl:value-of select="value/boundVariable"/>
</xsl:when>
<xsl:when test="value/enum != ''">
<xsl:value-of select="value/enum"/>
</xsl:when>
<xsl:when test="value/content != ''">
<xsl:value-of select="value/content"/>
</xsl:when>
<xsl:otherwise>
<xsl:text>No values encountered inside field</xsl:text>
</xsl:otherwise>
</xsl:choose>
<xsl:if test="position() != last()">
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:template>
<!-- INSERT LOGICAL TEMPLATE -->
<xsl:template match="insertLogical" name="insertLogicalTemplate">
<xsl:value-of select="factType"/>
<xsl:text> </xsl:text>
<xsl:value-of select="boundName"/>
<xsl:text> = new </xsl:text>
<xsl:value-of select="factType"/>
<xsl:text>(</xsl:text>
<xsl:if test="fieldValues!=''">
<xsl:apply-templates select="fieldValues"/>
</xsl:if>
<xsl:text>);</xsl:text>
<xsl:text>insertLogical</xsl:text>
<xsl:text>(</xsl:text>
<xsl:value-of select="boundName"/>
<xsl:text>);</xsl:text>
</xsl:template>
<!-- END OF THE INSERT LOGICAL TEMPLATE -->
<!-- ASSERT MODIFY TEMPLATE -->
<xsl:template match="modify" name="modifyTemplate">
<xsl:text>modify</xsl:text>
<xsl:text>( </xsl:text>
<xsl:value-of select="variable"/>
<xsl:text> ){ </xsl:text>
<xsl:if test="fieldValues!=''">
<xsl:apply-templates select="fieldValues"/>
</xsl:if>
<xsl:text> };</xsl:text>
</xsl:template>
<!-- END OF THE MODIFY TEMPLATE -->
<!-- RETRACT TEMPLATE -->
<xsl:template match="retract" name="retractTemplate">
<xsl:text>retract(</xsl:text>
<xsl:value-of select="."/>
<xsl:text>);</xsl:text>
</xsl:template>
<!-- END OF THE RETRACT TEMPLATE -->
<!-- start leftOperandTemplate -->
<xsl:template match="leftOperand" name="leftOperandTemplate">
<xsl:choose>
<xsl:when test="field/fieldName != ''">
<xsl:value-of select="field/fieldName" />
</xsl:when>
<xsl:when test="boundVariable/variableName != ''">
<xsl:value-of select="boundVariable/variableName" />
</xsl:when>
<xsl:when test="expression != ''">
<xsl:apply-templates select="expression" /> <!-- TO DO -->
</xsl:when>
<xsl:when test="literal != ''">
<xsl:value-of select="literal" />
</xsl:when>
<xsl:when test="value/content != ''">
<xsl:value-of select="value/content" />
</xsl:when>
<xsl:when test="callFunction != ''">
<xsl:apply-templates select="callFunction" />
</xsl:when>
<xsl:when test="formula != ''">
<xsl:apply-templates select="formula" />
</xsl:when>
<xsl:when test="callApi != ''">
<xsl:apply-templates select="callApi" />
</xsl:when>
<xsl:when test="callMethodOnBoundVariable != ''">
<xsl:apply-templates select="callMethodOnBoundVariable" />
</xsl:when>
<xsl:otherwise>
<xsl:text>No left operands found!</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- end leftOperandTemplate -->
<!-- start Right operand template -->
<xsl:template match="rightOperand" name="rightOperandTemplate">
<xsl:choose>
<xsl:when test="boundVariable/variableName != ''">
<xsl:value-of select="boundVariable/variableName" />
</xsl:when>
<xsl:when test="configurableVariable/variableName != ''">
<xsl:value-of select="configurableVariable/variableName" />
</xsl:when>
<xsl:when test="literal != ''">
<xsl:value-of select="literal" />
</xsl:when>
<xsl:when test="value/content != ''">
<xsl:value-of select="value/content" />
</xsl:when>
<xsl:when test="callFunction != ''">
<xsl:apply-templates select="callFunction" />
</xsl:when>
<xsl:when test="formula != ''">
<xsl:apply-templates select="formula" />
</xsl:when>
<xsl:when test="callApi != ''">
<xsl:apply-templates select="callApi" />
</xsl:when>
<xsl:when test="callMethodOnBoundVariable != ''">
<xsl:apply-templates select="callMethodOnBoundVariable" />
</xsl:when>
<xsl:when test="expression != ''">
<xsl:apply-templates select="expression" /> <!-- TO DO -->
</xsl:when>
<xsl:otherwise>
<xsl:text>No right operands found!</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- end Right operand template -->
<!-- start Comparison operator template -->
<xsl:template match=" operator | comparisonOperator" name="compareOperatorTemplate">
<xsl:choose>
<xsl:when test=".='gt'">
<xsl:value-of select="' > '"
disable-output-escaping="yes" />
</xsl:when>
<xsl:when test=". = 'lt'">
<xsl:value-of select="' < '"
disable-output-escaping="yes" />
</xsl:when>
<xsl:when test=".='eq'">
<xsl:value-of select="' == '" disable-output-escaping="yes" />
</xsl:when>
<xsl:when test=". = 'ne'">
<xsl:value-of select="' != '" disable-output-escaping="yes" />
</xsl:when>
<xsl:when test=". = 'lt or eq'">
<xsl:value-of select="' <= '"
disable-output-escaping="yes" />
</xsl:when>
<xsl:when test=". = 'gt or eq'">
<xsl:value-of select="' >= '"
disable-output-escaping="yes" />
</xsl:when>
<xsl:when test=". = 'matches'">
<xsl:value-of select="' matches '" />
</xsl:when>
<xsl:when test=". = 'not matches'">
<xsl:value-of select="' not matches '" />
</xsl:when>
<xsl:when test=". = 'contains'">
<xsl:value-of select="' contains '" />
</xsl:when>
<xsl:when test=". = 'not contains'">
<xsl:value-of select="' not contains '" />
</xsl:when>
<xsl:when test=". = 'memberOf'">
<xsl:value-of select="' memberOf '" />
</xsl:when>
<xsl:when test=". = 'not memberOf'">
<xsl:value-of select="' not memberOf '" />
</xsl:when>
<xsl:when test=". = 'sounds like'">
<xsl:value-of select="' sounds like '" />
</xsl:when>
<xsl:otherwise>
<xsl:text>No operator found</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- End Comparison operator template -->
<xsl:template match="evaluate" name="evaluateTemplate">
<xsl:text disable-output-escaping="yes">eval(</xsl:text>
<xsl:if test=". !=''">
<xsl:apply-templates select="leftOperand" />
<xsl:if test="comparisonOperator !=''">
<xsl:apply-templates select="comparisonOperator" />
</xsl:if>
<xsl:apply-templates select="rightOperand" />
</xsl:if>
<xsl:text>)</xsl:text>
</xsl:template>
<!-- END EVALUATE TEMPLATE -->
<!-- start CALL FUNCTION TEMPLATE -->
<xsl:template match="callFunction" name="callFunctionTemplate">
<xsl:if test=". !=''">
<xsl:if test="returnType!='' and returnTypeBoundName !='' ">
<xsl:value-of select="returnType" />
<xsl:text> </xsl:text>
<xsl:value-of select="returnTypeBoundName" />
<xsl:text>=</xsl:text>
</xsl:if>
<xsl:if test="functionName!=''">
<xsl:value-of select="functionName" />
<xsl:text>(</xsl:text>
<!-- <xsl:apply-templates select="args"/> -->
<xsl:call-template name="argsTemplate" />
</xsl:if>
<xsl:text>)</xsl:text>
</xsl:if>
</xsl:template>
<!-- END OF THE CALL FUNCTION TEMPLATE -->
<!-- START ARGS TEMPLATE -->
<xsl:template match="args" name="argsTemplate">
<xsl:if test="args/arg != ''">
<xsl:for-each select="args/arg">
<xsl:choose>
<xsl:when test="callMethodOnBoundVariable != ''">
<xsl:apply-templates select="callMethodOnBoundVariable" />
</xsl:when>
<xsl:when test="callApi != ''">
<xsl:apply-templates select="callApi" />
</xsl:when>
<xsl:when test="value != ''">
<xsl:value-of select="value/content" />
</xsl:when>
<xsl:when test="literal != ''">
<xsl:value-of select="." />
</xsl:when>
<xsl:when test="boundVariable != ''">
<xsl:value-of select="boundVariable/variableName" />
</xsl:when>
<xsl:otherwise>
<xsl:text>No Args</xsl:text>
</xsl:otherwise>
</xsl:choose>
<xsl:if test="position() != last()">
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:if>
</xsl:template>
<!-- END OF THE ARGS TEMPLATE -->
<!-- START of the Call Method on Bound Variable function -->
<xsl:template match="callMethodOnBoundVariable" name="callMethodOnBoundVariableTemplate">
<xsl:value-of select="boundVariableName" />
<xsl:text>.</xsl:text>
<xsl:value-of select="methodName" />
<xsl:text>(</xsl:text>
<!-- <xsl:value-of select="params/param/boundVariable" /> -->
<xsl:call-template name="paramsTemplate" />
<xsl:text>)</xsl:text>
</xsl:template>
<!-- End of the Call Method on Bound Variable function -->
<!-- START CALL API TEMPLATE -->
<xsl:template match="callApi" name="callApiTemplate">
<!-- <xsl:if test="returnType != 'void'"> <xsl:value-of select="returnType
" /> <xsl:text> </xsl:text> </xsl:if> -->
<xsl:value-of select="boundVaribleName" />
<xsl:if test="boundVaribleName[.!='']">
<xsl:text>=</xsl:text>
</xsl:if>
<xsl:value-of select="serviceVariableName" />
<xsl:text>.</xsl:text>
<xsl:value-of select="methodName" />
<xsl:text>(</xsl:text>
<xsl:call-template name="paramsTemplate" />
<xsl:text>)</xsl:text>
</xsl:template>
<!-- END call API TEMPLATE -->
<!-- FORMULA TEMPLATE -->
<xsl:template match="formula" name="formulaTemplate">
<xsl:if test="'leftOperand'">
<xsl:call-template name="formulaWithoutPeranthesisTemplate" />
</xsl:if>
<xsl:if test="'peranthesis'">
<xsl:call-template name="formulaWithPeranthesisTemplate" />
</xsl:if>
</xsl:template>
<!-- END OF THE FORMULA TEMPLATE -->
<!-- start Formula without peranthesis template -->
<xsl:template match="formulaWithoutPeranthesis" name="formulaWithoutPeranthesisTemplate">
<xsl:if test="leftOperand">
<xsl:apply-templates select="leftOperand" />
</xsl:if>
<xsl:if test="arithmeticOperator">
<xsl:apply-templates select="arithmeticOperator" />
</xsl:if>
<xsl:if test="rightOperand">
<xsl:apply-templates select="rightOperand" />
</xsl:if>
</xsl:template>
<!-- End Formula without peranthesis template -->
<!-- start Formula withparenthesis template -->
<xsl:template match="formulaWithPeranthesis" name="formulaWithPeranthesisTemplate">
<xsl:if test="peranthesis != ''">
<xsl:text>(</xsl:text>
<xsl:if test="peranthesis/leftOperand !=''">
<xsl:apply-templates select="peranthesis/leftOperand" />
</xsl:if>
<xsl:if test="peranthesis/arithmeticOperator">
<xsl:apply-templates select="peranthesis/arithmeticOperator" />
</xsl:if>
<xsl:if test="peranthesis/rightOperand">
<xsl:apply-templates select="peranthesis/rightOperand" />
</xsl:if>
<xsl:text>)</xsl:text>
</xsl:if>
</xsl:template>
<!-- END Formula with parenthesis template -->
<!-- START ARITHMETIC OPERATOR -->
<xsl:template match="arithmeticOperator" name="arithmeticOperatorTemplate">
<xsl:choose>
<xsl:when test="'+'">
<xsl:value-of select=" . " />
</xsl:when>
<xsl:when test="'-'">
<xsl:value-of select=" . " />
</xsl:when>
<xsl:when test="'*'">
<xsl:value-of select=" . " />
</xsl:when>
<xsl:when test="'/'">
<xsl:value-of select=" . " />
</xsl:when>
<xsl:when test="'%'">
<xsl:value-of select=" . " />
</xsl:when>
<xsl:otherwise>
<xsl:text>Not a valid airthmetic operator</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- END ARITHMETIC OPERATOR -->
<!-- START OF THE PARAMS TEMPLATE -->
<xsl:template match="params" name="paramsTemplate">
<xsl:if test="params/param != ''">
<xsl:for-each select="params/param">
<xsl:choose>
<xsl:when test="callMethodOnBoundVariable != ''">
<xsl:apply-templates select="callMethodOnBoundVariable" />
</xsl:when>
<xsl:when test="formula != ''">
<xsl:apply-templates select="formula" />
</xsl:when>
<xsl:when test="callApi != ''">
<xsl:apply-templates select="callApi" />
</xsl:when>
<xsl:when test="value != ''">
<xsl:value-of select="value/content" />
</xsl:when>
<xsl:when test="literal != ''">
<xsl:text>"</xsl:text>
<xsl:value-of select="." />
<xsl:text>"</xsl:text>
</xsl:when>
<xsl:when test="boundVariable != ''">
<xsl:value-of select="boundVariable" />
</xsl:when>
<xsl:when test="enum != ''">
<xsl:value-of select="enum" />
</xsl:when>
<xsl:otherwise>
<xsl:text>No Param</xsl:text>
</xsl:otherwise>
</xsl:choose>
<xsl:if test="position() != last()">
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:if>
</xsl:template>
<!-- END OF THE PARAMS TEMPLATE -->
<!--START COMPOSITE PATTERN TEMPLATE -->
<xsl:template match='compositePattern' name="compositePatternTemplate">
<xsl:if test="type != ''">
<xsl:value-of select="type" />
<xsl:text> </xsl:text>
</xsl:if>
<xsl:apply-templates select="patterns" />
</xsl:template>
<!-- END COMPOSITE PATTERN TEMPLATE -->
<xsl:template name="tagAndContentRemovalTemplate"
match=" nuggetVersion |nuggetId | ruleVersion | notes|ruleId | nuggetName | application | brlVersion | modelVersion | notes|declaration|preprocess " />
<xsl:template name="tagRemovalTemplate"
match=" ruleSection | blocks | block |actions | fieldRestriction | declaration | preprocess | tryCatchBlock |constraintList ">
<xsl:apply-templates />
<!-- set field and expression TBD -->
</xsl:template>
</xsl:stylesheet>
The Input file looks like below.
<?xml version="1.0" encoding="UTF-8"?>
<rule >
<name>IC-86</name>
<ruleId>1001</ruleId>
<ruleVersion>1.0</ruleVersion>
<nuggetId>1122</nuggetId>
<brlVersion>1.0</brlVersion>
<modelVersion>1.0</modelVersion>
<attributes>
<attribute>
<attributeName>agenda-group</attributeName>
<value>commonATSP</value>
</attribute>
</attributes>
<notes>Some text description about the rule, like what exactly it does...</notes>
<declaration />
<preprocess />
<ruleSection>
<blocks>
<block order="0">
<lhs>
<evaluate>
<leftOperand>
<boundVariable>
<variableName>$count</variableName>
<classType>Integer</classType>
<genericType>Number</genericType>
</boundVariable>
</leftOperand>
<comparisonOperator>gt</comparisonOperator>
<rightOperand>
<literal>10</literal>
</rightOperand>
</evaluate>
</lhs>
<rhs>
<modify>
<fieldValues>
<fieldValue>
<field>name</field>
<value>
<literal>$imageVersion</literal>
</value>
<nature>1</nature>
<type>String</type>
</fieldValue>
<fieldValue>
<field>value</field>
<value>
<callMethodOnBoundVariable>
<params>
<param>
<value>
<content>0</content>
<classType>int</classType>
<genericType>Numeric</genericType>
</value>
</param>
</params>
<boundVariableName>$attributes</boundVariableName>
<methodName>get</methodName>
<returnType>String</returnType>
</callMethodOnBoundVariable>
</value>
<nature>4</nature>
<type>String</type>
</fieldValue>
<fieldValue>
<field>type</field>
<value>
<literal>imageName</literal>
</value>
<nature>1</nature>
<type>String</type>
</fieldValue>
</fieldValues>
<variable>$attribute</variable>
</modify>
</rhs>
</block>
</blocks>
</ruleSection>
</rule>
I am facing an issue with my default namespace. I have a file with the root tag as below:
<rule xmlns="http://www.max.com/BRL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.max.com/BRL ../BRLV4.xsd ">
If I remove the namespaced <rule> as seen in the above input XML, then the translation works. Otherwise, it doesn't work. That is, if I try the translation without the default namespace then I am getting the required output as below. But I need the same output when I get the XML file with the default namespace on the root tag.
<?xml version="1.0" encoding="UTF-8"?>
<rule>
<name>IC-86</name>
<attributes>
<attribute>
<attributeName>agenda-group</attributeName>
<value>commonATSP</value>
</attribute>
</attributes>
<lhs>
<freeForm>
<text>eval($count > 10)</text>
</freeForm>
</lhs>
<rhs>
<freeForm>
<text>modify( $attribute ){ $imageVersion,$attributes.get(0),imageName };</text>
</freeForm>
</rhs>
</rule>
The default namespace in the XML document must be declared in the XSLT. For example, use the prefix 'brl'.
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:brl="http://www.max.com/BRL">
Then use the brl prefix when referring to the tag names. For example,
<xsl:template match="brl:lhs" name="lhsTemplate">
...
<xsl:when test="brl:incident != ''">
<xsl:for-each select="brl:incident">
If the XML document may or may not have a namespace, see my article "Stay on the XPath: Tip 5: Handling XPath select expressions that fail to match documents with a default namespace" on IBM developerWorks.
Basically, you need your stylesheet to work whether all the elements are in no namespace, or in the BRL namespace. This is an odd requirement, and the ideal solution would be to tell your upstream data provider to be consistent about using the namespace.
But if you don't have that option, you have a few others.
1) Declare a prefix for the BRL namespace in your stylesheet, and use it to match elements in that namespace or in no namespace:
<xsl:stylesheet ... xmlns:BRL="http://www.max.com/BRL">
...
<xsl:template match="lhs | BRL:lhs">
...
2) Make all your matches and selects use local-name():
<xsl:template match="*[local-name()='lhs']">...
...
(Yuck!)
3) In XSLT 2.0, you can use the wildcard for namespaces:
<xsl:template match="*:lhs">
...
<xsl:when test="*:field/*:fieldName != ''">
But again, these three are all working around a problem in the source data. They are compensating for someone not using namespaces correctly, and thus enabling them to keep on doing the same. If the schema says the input document's elements should be in the "http://www.max.com/BRL" namespace, then you should assume that they are in that namespace (whether that's the default namespace is immaterial to you), so you should always be using a namespace prefix when matching or selecting those elements with XPath.