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 :

newbie : XML >> CSV avec XSL


Sujet :

XSL/XSLT/XPATH XML

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Mai 2003
    Messages
    80
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2003
    Messages : 80
    Points : 53
    Points
    53
    Par défaut newbie : XML >> CSV avec XSL
    Bonjour à tous,

    Comme indiqué dans le titre je débute complètement en XSL, c'est pas faute d'avoir chercher... mais je ne parviens pas à générer un CSV propre.
    Je souhaite transformer un XML en CSV en passant par XSL.

    J'ai un XML de la structure suivante :

    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
     
    <?xml version="1.0" encoding="UTF-8"?>
    <commandes>
      <commande>
        <commande_numclient>123456</commande_numclient>
        <commande_date>16/07/2009</commande_date>
        <commande_num>100000064</commande_num>
        <commande_emailclient>123456@domaine.fr</commande_emailclient>
        <commande_produit>
          <commande_produit_ref>11020</commande_produit_ref>
          <commande_produit_nom>Tee-Shirt</commande_produit_nom>
          <commande_produit_prix>12.1000</commande_produit_prix>
          <commande_produit_qte>1.0000</commande_produit_qte>
          <commande_produit_totalht>12.1000</commande_produit_totalht>
        </commande_produit>
       <commande_produit>
          <commande_produit_ref>11030</commande_produit_ref>
          <commande_produit_nom>Pull</commande_produit_nom>
          <commande_produit_prix>16.1000</commande_produit_prix>
          <commande_produit_qte>1.0000</commande_produit_qte>
          <commande_produit_totalht>16.1000</commande_produit_totalht>
        </commande_produit>
        <commande_fraislivraison>5.0000</commande_fraislivraison>
        <commande_commentaire>commentaire</commande_commentaire>
        <commande_totalht>38.2000</commande_totalht>
      </commande>
    </commandes>
    Que je voudrai transformer en CSV (en php) pour qu'il est cette forme :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    commande_numclient,commande_date,commande_num,commande_emailclient,commande_produit_ref,commande_produit_nom,commande_produit_prix,commande_produit_qte,commande_produit_totalht,commande_fraislivraison,commande_commentaire,commande_totalht
    "123456","16/07/2009","100000064","123456@domaine.fr","A11020","Tee-Shirt","12,1","1","12,1","5","commentaire","38,2"
    "123456","16/07/2009","100000064","123456@domaine.fr","A11030","Pull","16,1","1","16,1","5","commentaire","38,2"
    J'ai commencé à faire un XSL mais je galère....

    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
     
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     
    <xsl:output method="text" encoding="utf-8"/>
     
      <xsl:template match="commandes">
        <xsl:apply-templates/>
      </xsl:template>
     
    <xsl:template match="commande">
        <xsl:apply-templates select="commande_numclient"/>
        <xsl:text>,</xsl:text>
        <xsl:apply-templates select="commande_num"/>
        <xsl:text>,</xsl:text>
        <xsl:apply-templates select="commande_date"/>
     
            <xsl:template match="commande_produit">
                <xsl:apply-templates select="commande_produit_ref"/>
                <xsl:text>,</xsl:text>
                <xsl:apply-templates select="commande_produit_nom"/>
                <xsl:text>,</xsl:text>
                <xsl:apply-templates select="commande_produit_qte"/>
                <xsl:text>,</xsl:text>
                <xsl:apply-templates select="commande_produit_totalht"/>
            </xsl:template>
     
        <xsl:text>&#xA;</xsl:text>
      </xsl:template>
     
      <xsl:template match="commande_numclient">
        <xsl:value-of select="text()"/>
      </xsl:template>
     
      <xsl:template match="commande_num">
        <xsl:value-of select="text()"/>
      </xsl:template>
     
      <xsl:template match="commande_date">
        <xsl:value-of select="text()"/>
      </xsl:template>
     
      <xsl:template match="commande_produit_ref">
        <xsl:value-of select="text()"/>
      </xsl:template>
     
      <xsl:template match="commande_produit_nom">
        <xsl:value-of select="text()"/>
      </xsl:template>
     
      <xsl:template match="commande_produit_qte">
        <xsl:value-of select="text()"/>
      </xsl:template>
     
      <xsl:template match="commande_produit_totalht">
        <xsl:value-of select="text()"/>
      </xsl:template>
     
    </xsl:stylesheet>
    Est-ce que quelqu'un aurait la gentillesse d'éclairer ma lanterne de newbie

    Merci !

  2. #2
    Membre expérimenté
    Profil pro
    Inscrit en
    Septembre 2006
    Messages
    1 466
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2006
    Messages : 1 466
    Points : 1 610
    Points
    1 610
    Par défaut
    Hello,
    Qqchose comme ça :
    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
    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="text" omit-xml-declaration="yes"/>
     
      <xsl:template match="commandes">
    		<xsl:apply-templates select="commande/commande_produit"/>
    	</xsl:template>
     
    	<xsl:template match="commande/commande_produit"><xsl:apply-templates select="../commande_numclient"/>,<xsl:apply-templates select="../commande_num"/>,<xsl:apply-templates select="../commande_date"/>,<xsl:apply-templates select="commande_produit_ref"/>,<xsl:apply-templates select="commande_produit_nom"/>,<xsl:apply-templates select="commande_produit_qte"/>,<xsl:apply-templates select="commande_produit_totalht"/>,
    </xsl:template>
     
     
     
    	<xsl:template match="commande_numclient"><xsl:value-of select="text()"/></xsl:template>
     
    	<xsl:template match="commande_num"><xsl:value-of select="text()"/></xsl:template>
     
    	<xsl:template match="commande_date"><xsl:value-of select="text()"/></xsl:template>
     
    	<xsl:template match="commande_produit_ref"><xsl:value-of select="text()"/></xsl:template>
     
    	<xsl:template match="commande_produit_nom"><xsl:value-of select="text()"/></xsl:template>
     
    	<xsl:template match="commande_produit_qte"><xsl:value-of select="text()"/></xsl:template>
     
    	<xsl:template match="commande_produit_totalht"><xsl:value-of select="text()"/></xsl:template>
    </xsl:stylesheet>

  3. #3
    Membre du Club
    Profil pro
    Inscrit en
    Mai 2003
    Messages
    80
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2003
    Messages : 80
    Points : 53
    Points
    53
    Par défaut
    Merci beaucoup Morbo pour cette réponse !!

    Au cas où ça puisse aider, voici mon XSL final (avec les entêtes de colonnes en plus) :

    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
     
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="text" omit-xml-declaration="yes"/>
     
      <xsl:template match="commandes">
        <xsl:text>commande_numclient,commande_date,commande_num,commande_emailclient,commande_produit_ref,commande_produit_nom,commande_produit_prix,commande_produit_qte,commande_produit_totalht,commande_fraislivraison,commande_commentaire,commande_totalht</xsl:text>
    		<xsl:text>&#xD;&#xA;</xsl:text>
        <xsl:apply-templates select="commande/commande_produit"/>
    	</xsl:template>
     
    	<xsl:template match="commande/commande_produit"><xsl:apply-templates select="../commande_numclient"/>,<xsl:apply-templates select="../commande_num"/>,<xsl:apply-templates select="../commande_date"/>,<xsl:apply-templates select="commande_produit_ref"/>,<xsl:apply-templates select="commande_produit_nom"/>,<xsl:apply-templates select="commande_produit_qte"/>,<xsl:apply-templates select="commande_produit_totalht"/>,<xsl:apply-templates select="../commande_fraislivraison"/>,<xsl:apply-templates select="../commande_commentaire"/>,<xsl:apply-templates select="../commande_totalht"/><xsl:text>&#xA;</xsl:text>
    </xsl:template>
     
     
     
    	<xsl:template match="commande_numclient"><xsl:value-of select="text()"/></xsl:template>
     
    	<xsl:template match="commande_num"><xsl:value-of select="text()"/></xsl:template>
     
    	<xsl:template match="commande_date"><xsl:value-of select="text()"/></xsl:template>
     
    	<xsl:template match="commande_produit_ref"><xsl:value-of select="text()"/></xsl:template>
     
    	<xsl:template match="commande_produit_nom"><xsl:value-of select="text()"/></xsl:template>
     
    	<xsl:template match="commande_produit_qte"><xsl:value-of select="text()"/></xsl:template>
     
    	<xsl:template match="commande_produit_totalht"><xsl:value-of select="text()"/></xsl:template>
    </xsl:stylesheet>

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

Discussions similaires

  1. [XSLT 1.0] Passer du XML au CSV avec XSL
    Par BertrandB dans le forum XSL/XSLT/XPATH
    Réponses: 4
    Dernier message: 22/01/2015, 23h18
  2. [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
  3. [débutant]XML vers XML avec XSL
    Par tokamak dans le forum XSL/XSLT/XPATH
    Réponses: 10
    Dernier message: 11/07/2005, 10h27
  4. XML vers XML avec XSL
    Par guizz79 dans le forum XSL/XSLT/XPATH
    Réponses: 3
    Dernier message: 27/06/2005, 09h43
  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