Menu

xslt code choose between 2 values

Help
2015-06-17
2015-06-17
  • Raul Iturbe

    Raul Iturbe - 2015-06-17

    Hello,

    I have this xml file.

    <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 other xml file, where I need to add a new row with the value of max between Val1 and Val2:

    <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="MaxVal1ORVal2">23.334</ValeurIndex> <ValeurIndex Libelle="Val2">5.186</ValeurIndex> <ValeurIndex Libelle="Val3">2.79</ValeurIndex> </arrêté> </index> </racine>

    how can I do it?

    Thanks

     
    • Michael Kay

      Michael Kay - 2015-06-17

      I would suggest using StackOverflow for general XSLT coding questions - there’s a wider group of people answering questions on that forum, and this is nothing specifically to do with Saxon.

      On 17 Jun 2015, at 16:53, Raul Iturbe luarit@users.sf.net wrote:

      Hello,

      I have this xml file.

      <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 other xml file, where I need to add a new row with the value of max between Val1 and Val2:

      <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="MaxVal1ORVal2">23.334</ValeurIndex> <ValeurIndex Libelle="Val2">5.186</ValeurIndex> <ValeurIndex Libelle="Val3">2.79</ValeurIndex> </arrêté> </index> </racine>

      how can I do it?

      It’s hard to say without knowing what other input documents your code might have to deal with, but perhaps something like this:

      <xsl;template match="“arrêté”">
      <xsl:copy-of select="“ValeurIndex&lt;span">[@Libelle=‘Val1’]”/>
      <ValeurIndex Libelle="“MaxVal1ORVal2”">
      <xsl:value-of select="“max(ValeurIndex&lt;span">[@Libelle=(‘Val1’, ‘Val2’)])”/>
      </ValeurIndex>
      <xsl:copy-of select="“ValeurIndex&lt;span">[@Libelle=‘Val2’]”/>
      etc.
      </xsl:template>

      Michael Kay
      Saxonica