IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

XSL/XSLT/XPATH XML Discussion :

Passer du XML au CSV avec XSL [XSLT 1.0]


Sujet :

XSL/XSLT/XPATH XML

  1. #1
    Membre à l'essai
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juillet 2014
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Juillet 2014
    Messages : 7
    Points : 10
    Points
    10
    Par défaut Passer du XML au CSV avec XSL
    Bonsoir à tous,

    J'ai un fichier XML contenant une très grosse quantité de données que je veux convertir en CSV, (Format nécéssaire pour le logiciel qui reçoit ces données).

    J'ai donc fait un XSL qui transforme le fichier en CSV mais j'ai un petit souci d'arborescence, je m'explique :

    Voici un extrait de mon fichier XML :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    <Tags>
    <Tag name="SYS_ACQ_DEPOUB" path="" type="OPC">
        <Property name="Datatype">6</Property>
        <Property name="Value">1</Property>
        <Property name="OPCServer">S7 TCP-IP</Property>
        <Property name="OPCItemPath">[DEPOUSSIERAGE]DB50,X21.2</Property>
        <Property name="Scalemode">0</Property>
        <Property name="RawLow">0.000000</Property>
        <Property name="RawHigh">1.000000</Property>
        <Property name="ScaledLow">0.000000</Property>
        <Property name="ScaledHigh">1.000000</Property>
        <Property name="FormatString"></Property>
        <Property name="EngUnit"></Property>
        <Property name="HistoryEnabled">0</Property>
        <Property name="PrimaryHistoryProvider">CIFC</Property>
      </Tag>
      <Tag name="SYS_AL_S_01_01" path="" type="OPC">
        <Property name="Datatype">6</Property>
        <Property name="Value">1</Property>
        <Property name="OPCServer">S7 TCP-IP</Property>
        <Property name="OPCItemPath">[414]DB10,X80.1</Property>
        <Property name="Scalemode">0</Property>
        <Property name="RawLow">0.000000</Property>
        <Property name="RawHigh">1.000000</Property>
        <Property name="ScaledLow">0.000000</Property>
        <Property name="ScaledHigh">1.000000</Property>
        <Property name="FormatString"></Property>
        <Property name="EngUnit"></Property>
        <Property name="HistoryEnabled">0</Property>
        <Property name="PrimaryHistoryProvider">CIFC</Property>
        <Alarms>
          <Alarm name="S7-400 d_faut Rack 1 Carte 1">
            <Property name="priority">1</Property>
            <Property name="setpointA">1.000000</Property>
            <Property name="displayPath">HISTO_CIFC</Property>
          </Alarm>
        </Alarms>
      </Tag>
    </Tags>
    Comme vous l'aurez remarqué, il y a 2 types de variables, une ayant une Alarme <Alarm name="S7-400 d_faut Rack 1 Carte 1"> , et l'autre non.
    Si il n'y a pas d'alarme, les variables correspondantes doivent être mise à "0", sinon on renseigne 4 colonnes avec "Alarme Name" et les 3 valeurs des property.

    Mon Fichier Xsl se présente comme suit :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    <?xml version="1.0"?>
    <xsl:stylesheet version = "1.0" xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
     
      <xsl:output method="text"/>
     
      <xsl:template match="/Tags" >
            <xsl:for-each select="Tag">
              <xsl:text>;</xsl:text>
              <xsl:value-of select="parent::Tag/@name"/>
              <xsl:for-each select="Property">
                <xsl:value-of select="Property"/>
                <xsl:text>;</xsl:text>
              </xsl:for-each>   
     
              <xsl:text>;
    </xsl:text>      
            </xsl:for-each>
      </xsl:template>
    </xsl:stylesheet>
    Ne maitrisant pas du tout le Xsl je suis complètement perdu, je souhaite obtenir un .CSV de la sorte :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    TAG name;Value;DataType;OPCServer;OPCItemPath;ScaleMode;RawLow;RawHigh;ScaledLow;ScaledHigh;FormatString;EngUnit;HistoryEnabled;Alarm name;priority;setpointA;displayPath;PrimaryHistoryProvider
     
    SYS_ACQ_DEPOUB;1;6;S7 TCP-IP;[DEPOUSSIERAGE]DB50,X21.2;0;0.000000;1.000000;0.000000;1.000000;;;0;;-1;0.000000;HISTO_CIFC;CIFC
    SYS_AL_S_01_01;1;6;S7 TCP-IP;[414]DB10,X80.1;0;0.000000;1.000000;0.000000;1.000000;;;0;S7-400 d_faut Rack 1 Carte 1;1;1.000000;HISTO_CIFC;CIFC
    En image : Nom : Capture d’écran 2015-01-20 à 19.43.09.png
Affichages : 481
Taille : 54,0 Ko

    J'essaye de tester au fur et à mesure mais le fait qu'il y ait une condition me bloque completement..

    J'imagine que ça doit pourtant être très simple? SI quelqu'un aurait une idée?

    Merci d'avance!

    Bonne soirée.

  2. #2
    Membre à l'essai
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juillet 2014
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Juillet 2014
    Messages : 7
    Points : 10
    Points
    10
    Par défaut
    Je commence à bien avancer, j'ai à présent un xsl de la forme :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    <?xml version="1.0"?>
    <xsl:stylesheet version = "1.0" xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
     
      <xsl:output method="text"/>
     
      <xsl:template match="/" >
        <xsl:for-each select="Tags/Tag">
          <xsl:sort select="@name" order="descending"/>
          <xsl:apply-templates select="@name"/>
          <xsl:apply-templates select="Property"/>
          <xsl:apply-templates select="Alarms"/>
          <xsl:text>;
    </xsl:text>
        </xsl:for-each>
      </xsl:template>
     
      <xsl:template match="@name">
        <xsl:value-of select="."/>
      </xsl:template>
     
      <xsl:template match="Property">
        <xsl:value-of select="."/>
        <xsl:text>;</xsl:text>
      </xsl:template>
     
      <xsl:template match="Alarms">
        <xsl:for-each select="Alarms/alarm">
          <xsl:sort select="@name" order="descending"/>
          <xsl:text>;</xsl:text>
          <xsl:value-of select="Property"/>
          <xsl:text>;</xsl:text>
        </xsl:for-each>
      </xsl:template>
     
     
    </xsl:stylesheet>

    Avec pour rappel un xml de la forme :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    <?xml version="1.0" encoding="iso-8859-1"?>
     
    <Tags>
      <Tag name="SYS_AL_S_01_12" path="" type="OPC">
        <Property name="Datatype">6</Property>
        <Property name="Value">1</Property>
        <Property name="OPCServer">S7 TCP-IP</Property>
        <Property name="OPCItemPath">[414]DB10,X81.4</Property>
        <Property name="Scalemode">0</Property>
        <Property name="RawLow">0.000000</Property>
        <Property name="RawHigh">1.000000</Property>
        <Property name="ScaledLow">0.000000</Property>
        <Property name="ScaledHigh">1.000000</Property>
        <Property name="FormatString"></Property>
        <Property name="EngUnit"></Property>
        <Property name="HistoryEnabled">0</Property>
        <Property name="PrimaryHistoryProvider">CIFC</Property>
        <Alarms>
          <Alarm name="S7-400 d_faut Rack 1 Carte 12">
            <Property name="priority">1</Property>
            <Property name="setpointA">1.000000</Property>
            <Property name="displayPath">HISTO_CIFC</Property>
          </Alarm>
        </Alarms>
      </Tag>
      <Tag name="SYS_ACQ_DEPOUB" path="" type="OPC">
        <Property name="Datatype">6</Property>
        <Property name="Value">1</Property>
        <Property name="OPCServer">S7 TCP-IP</Property>
        <Property name="OPCItemPath">[DEPOUSSIERAGE]DB50,X21.2</Property>
        <Property name="Scalemode">0</Property>
        <Property name="RawLow">0.000000</Property>
        <Property name="RawHigh">1.000000</Property>
        <Property name="ScaledLow">0.000000</Property>
        <Property name="ScaledHigh">1.000000</Property>
        <Property name="FormatString"></Property>
        <Property name="EngUnit"></Property>
        <Property name="HistoryEnabled">0</Property>
        <Property name="PrimaryHistoryProvider">CIFC</Property>
      </Tag>
      <Tag name="SYS_ACQ_DEPOUA" path="" type="OPC">
        <Property name="Datatype">6</Property>
        <Property name="Value">1</Property>
        <Property name="OPCServer">S7 TCP-IP</Property>
        <Property name="OPCItemPath">[DEPOUSSIERAGE]DB40,X21.2</Property>
        <Property name="Scalemode">0</Property>
        <Property name="RawLow">0.000000</Property>
        <Property name="RawHigh">1.000000</Property>
        <Property name="ScaledLow">0.000000</Property>
        <Property name="ScaledHigh">1.000000</Property>
        <Property name="FormatString"></Property>
        <Property name="EngUnit"></Property>
        <Property name="HistoryEnabled">0</Property>
        <Property name="PrimaryHistoryProvider">CIFC</Property>
      </Tag>
    </Tags>
    Et j'obtiens maintenant un .CSV comme suit :

    Nom : Capture d’écran 2015-01-21 à 10.39.07.png
Affichages : 438
Taille : 36,2 Ko

    Je suis coincé sur l'affichage du contenu dans la balise Alarms lorsqu'elle est présente, il me faut le nom de l'alarme et les valeurs des 3 property, auriez vous une idée?

    Merci d'avance

  3. #3
    Membre à l'essai
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juillet 2014
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Juillet 2014
    Messages : 7
    Points : 10
    Points
    10
    Par défaut
    Bon bah j'ai trouvé la solution au final, si ça peut aider quelqu'un un jour, voici mon xsl:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    <?xml version="1.0"?>
    <xsl:stylesheet version = "1.0" xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
     
      <xsl:output indent="no" method="text"/>
     
      <xsl:template match="/" >
          <xsl:text>TAG name;Value;DataType;OPCServer;OPCItemPath;ScaleMode;RawLow;RawHigh;ScaledLow;ScaledHigh;FormatString;EngUnit;HistoryEnabled;Alarm name;priority;setpointA;displayPath;PrimaryHistoryProvider</xsl:text>
        <xsl:text>
    </xsl:text>
          <xsl:text>0;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16;17</xsl:text>
        <xsl:text>
    </xsl:text>
          <xsl:text>#ROWS;;;;;;;;;;;;;;;;;</xsl:text>
        <xsl:text>
    </xsl:text>
          <xsl:text>str;I;I;str;str;B;D;D;D;D;str;str;B;str;I;D;str;str</xsl:text>
        <xsl:text>
    </xsl:text>
          <xsl:text>TYPES;;;;;;;;;;;;;;;;;</xsl:text>
        <xsl:text>
    </xsl:text>
          <xsl:for-each select="Tags/Tag">
            <xsl:sort select="@name" order="ascending"/>
            <xsl:apply-templates select="@name"/>
            <xsl:text>;</xsl:text>
            <xsl:apply-templates select="Property"/>
            <xsl:choose>
              <xsl:when test="not(Alarms)">
                <xsl:text>"";-1;"";HISTO_CIFC</xsl:text>
              </xsl:when>
              <xsl:otherwise>
                <xsl:apply-templates select="Alarms/Alarm/@name"/>
                <xsl:apply-templates select="Alarms/Alarm/Property"/>        
              </xsl:otherwise>
            </xsl:choose>
            <xsl:text>
    </xsl:text>
          </xsl:for-each>
        </xsl:template>
     
        <xsl:template match="@name">
          <xsl:value-of select="."/>
        </xsl:template>
     
        <xsl:template match="Property">
          <xsl:value-of select="."/>
          <xsl:text>;</xsl:text>
        </xsl:template>
     
        <xsl:template match="Alarms/Alarm/@name">
          <xsl:value-of select="."/>
          <xsl:text>;</xsl:text>
        </xsl:template>
     
        <xsl:template match="Alarms/Alarm/Property">
          <xsl:choose>
            <xsl:when test="not(@name='displayPath')">
              <xsl:value-of select="."/>
              <xsl:text>;</xsl:text>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="."/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:template>
     
      </xsl:stylesheet>

  4. #4
    Modérateur

    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    12 566
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2004
    Messages : 12 566
    Points : 21 635
    Points
    21 635
    Par défaut
    Félicitations pour avoir trouvé !

    Pour ceux qui se demandent, dans ce genre de situation le plus simple est d'utiliser <xsl:choose> avec un <xsl:when> et un <xsl:otherwise>

  5. #5
    Membre à l'essai
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juillet 2014
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Juillet 2014
    Messages : 7
    Points : 10
    Points
    10
    Par défaut
    Citation Envoyé par thelvin Voir le message
    Félicitations pour avoir trouvé !

    Pour ceux qui se demandent, dans ce genre de situation le plus simple est d'utiliser <xsl:choose> avec un <xsl:when> et un <xsl:otherwise>

    J'ai modifié ma solution, j'avais un problème avec les retours chariot que je faisais avec une manip un peu bordélique, je jouais avec l'indentation du xsl.. Alors qu'il faut mettre au final cette balise :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    <xsl:text>
    </xsl:text>
    Tout fonctionne parfaitement maintenant

    J'ai un autre problème avec mon programme maintenant mais c'est en C# et je compte bien chercher avant de poster une question ^^

    Bonne soirée à tous !

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. xml vers csv via xsl pur
    Par tofpad dans le forum XSL/XSLT/XPATH
    Réponses: 4
    Dernier message: 17/03/2010, 09h35
  2. newbie : XML >> CSV avec XSL
    Par cli16 dans le forum XSL/XSLT/XPATH
    Réponses: 2
    Dernier message: 16/07/2009, 17h20
  3. Réponses: 7
    Dernier message: 04/06/2007, 15h51
  4. [XSLT]Passer du XML au CSV avec XSL
    Par tibotibotibo dans le forum XSL/XSLT/XPATH
    Réponses: 2
    Dernier message: 16/03/2007, 11h12
  5. Questionnaire à réaliser avec XSL et XML
    Par olive.m dans le forum XSL/XSLT/XPATH
    Réponses: 4
    Dernier message: 19/11/2003, 16h37

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo