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 :

XML à fichier text ou csv : mise à plat


Sujet :

XSL/XSLT/XPATH XML

  1. #1
    Candidat au Club
    Profil pro
    Inscrit en
    Mars 2007
    Messages
    8
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Mars 2007
    Messages : 8
    Points : 2
    Points
    2
    Par défaut XML à fichier text ou csv : mise à plat
    Bonjour à tous,

    je chercher à produire un fichier text à partir d'un xml avec xslt. Quelle est la meilleure méthode pour "mettre à plat" un xml ? Je m'explique :

    Les noeuds qui n'existent pas doivent être transcrit en caractère de séparation dans mon fichier texte (tab ou ; par exemple)

    J'ai le fichier xml suivant (allégé) :

    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
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    <eCH0083:record>
        <eCH0083:attributed>
          <eCH0083:submittedPersonalAttributes>
            <eCH0083:localPersonId>
              <eCH0044:personIdCategory>LOC.RCPers</eCH0044:personIdCategory>
              <eCH0044:personId>3913471</eCH0044:personId>
            </eCH0083:localPersonId>
            <eCH0083:otherPersonId>
              <eCH0044:personIdCategory>CT.VD</eCH0044:personIdCategory>
              <eCH0044:personId>3913471</eCH0044:personId>
            </eCH0083:otherPersonId>
            <eCH0083:otherPersonId>
              <eCH0044:personIdCategory>MU.5586</eCH0044:personIdCategory>
              <eCH0044:personId>558612356</eCH0044:personId>
            </eCH0083:otherPersonId>
    	   </eCH0083:submittedPersonalAttributes>
        </eCH0083:attributed>
      </eCH0083:record>
      <eCH0083:record>
        <eCH0083:attributed>
          <eCH0083:submittedPersonalAttributes>
            <eCH0083:localPersonId>
              <eCH0044:personIdCategory>LOC.RCPers</eCH0044:personIdCategory>
              <eCH0044:personId>3913477</eCH0044:personId>
            </eCH0083:localPersonId>
            <eCH0083:otherPersonId>
              <eCH0044:personIdCategory>CT.VD</eCH0044:personIdCategory>
              <eCH0044:personId>3913477</eCH0044:personId>
            </eCH0083:otherPersonId>
            <eCH0083:otherPersonId>
              <eCH0044:personIdCategory>CH.ZEMIS</eCH0044:personIdCategory>
              <eCH0044:personId>5684963180</eCH0044:personId>
            </eCH0083:otherPersonId>
            <eCH0083:otherPersonId>
              <eCH0044:personIdCategory>MU.5586</eCH0044:personIdCategory>
              <eCH0044:personId>558612362</eCH0044:personId>
            </eCH0083:otherPersonId>
          </eCH0083:submittedPersonalAttributes>
        </eCH0083:attributed>
      </eCH0083:record>
      <eCH0083:record>
        <eCH0083:attributed>
          <eCH0083:submittedPersonalAttributes>
            <eCH0083:localPersonId>
              <eCH0044:personIdCategory>LOC.RCPers</eCH0044:personIdCategory>
              <eCH0044:personId>3913495</eCH0044:personId>
            </eCH0083:localPersonId>
            <eCH0083:otherPersonId>
              <eCH0044:personIdCategory>CT.VD</eCH0044:personIdCategory>
              <eCH0044:personId>3913495</eCH0044:personId>
            </eCH0083:otherPersonId>
            <eCH0083:otherPersonId>
              <eCH0044:personIdCategory>MU.5586</eCH0044:personIdCategory>
              <eCH0044:personId>558612380</eCH0044:personId>
            </eCH0083:otherPersonId>
          </eCH0083:submittedPersonalAttributes>
        </eCH0083:attributed>
      </eCH0083:record>
      <eCH0083:record>
        <eCH0083:attributed>
          <eCH0083:submittedPersonalAttributes>
            <eCH0083:localPersonId>
              <eCH0044:personIdCategory>LOC.RCPers</eCH0044:personIdCategory>
              <eCH0044:personId>3913507</eCH0044:personId>
            </eCH0083:localPersonId>
            <eCH0083:otherPersonId>
              <eCH0044:personIdCategory>CT.VD</eCH0044:personIdCategory>
              <eCH0044:personId>3913507</eCH0044:personId>
            </eCH0083:otherPersonId>
            <eCH0083:otherPersonId>
              <eCH0044:personIdCategory>CH.ZEMIS</eCH0044:personIdCategory>
              <eCH0044:personId>5684963192</eCH0044:personId>
            </eCH0083:otherPersonId>
            <eCH0083:otherPersonId>
              <eCH0044:personIdCategory>MU.5586</eCH0044:personIdCategory>
              <eCH0044:personId>558612391</eCH0044:personId>
            </eCH0083:otherPersonId>
          </eCH0083:submittedPersonalAttributes>
        </eCH0083:attributed>
      </eCH0083:record>
      <eCH0083:record>
        <eCH0083:attributed>
          <eCH0083:submittedPersonalAttributes>
            <eCH0083:localPersonId>
              <eCH0044:personIdCategory>LOC.RCPers</eCH0044:personIdCategory>
              <eCH0044:personId>3913519</eCH0044:personId>
            </eCH0083:localPersonId>
            <eCH0083:otherPersonId>
              <eCH0044:personIdCategory>CT.VD</eCH0044:personIdCategory>
              <eCH0044:personId>3913519</eCH0044:personId>
            </eCH0083:otherPersonId>
            <eCH0083:otherPersonId>
              <eCH0044:personIdCategory>CH.ZEMIS</eCH0044:personIdCategory>
              <eCH0044:personId>5684963198</eCH0044:personId>
            </eCH0083:otherPersonId>
            <eCH0083:otherPersonId>
              <eCH0044:personIdCategory>MU.5586</eCH0044:personIdCategory>
              <eCH0044:personId>558612405</eCH0044:personId>
            </eCH0083:otherPersonId>
          </eCH0083:submittedPersonalAttributes>
        </eCH0083:attributed>
      </eCH0083:record>
    Comment faire avec les otherPersonId pour que lorsqu'ils n'existent pas, j'insère un caractère tab ou ; ?

    Quand ils existent, j'ai besoin du fils personId uniquement, mais pour le test je prend le personIdCategory (plus facile à identifier qu'une suite de chiffres).

    Ca doit me produire un texte comme

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    LOC.RCPers   CT.VD                   MU.5586
    LOC.RCPers   CT.VD   CH.ZEMIS   MU.5586
    LOC.RCPers   CT.VD                   MU.5586
    LOC.RCPers   CT.VD   CH.ZEMIS   MU.5586
    J'ai commencé ceci mais je suis pas sûr de faire comme il faut, vu que je débute en xslt

    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:template match="/">
    			<xsl:for-each select="//eCH0083:record">
    				<xsl:choose>
    					<xsl:when test=".//eCH0044:personIdCategory='LOC.RCPers'">
    						<xsl:value-of select=".//eCH0044:personIdCategory"/><xsl:text>&#x9;</xsl:text>
    					</xsl:when>
    					<xsl:otherwise>
    						<xsl:copy-of select='$vide'/>
    					</xsl:otherwise>
    				</xsl:choose>
    				<xsl:choose>
    					<xsl:when test=".//eCH0044:personIdCategory='CT.VD'">
    						<xsl:value-of select=".//eCH0083:otherPersonId/eCH0044:personIdCategory"/><xsl:text>&#x9;</xsl:text>
    					</xsl:when>
    					<xsl:otherwise>
    						<xsl:copy-of select='$vide'/>
    					</xsl:otherwise>
    				</xsl:choose>
    				<xsl:choose>
    					<xsl:when test=".//eCH0044:personIdCategory='CH.ZEMIS'">
    						<xsl:value-of select="./eCH0044:personIdCategory"/><xsl:text>&#x9;</xsl:text>
    					</xsl:when>
    					<xsl:otherwise>
    						<xsl:copy-of select='$vide'/>
    					</xsl:otherwise>
    				</xsl:choose>
    				<xsl:text>&#xD;&#xA;</xsl:text>
    			</xsl:for-each>
    		</xsl:template>

    J'espère que mes explications sont compréhensibles... merci d'avance si vous pouvez m'indiquer la voie à suivre

    Florian

  2. #2
    Expert éminent
    Avatar de GrandFather
    Inscrit en
    Mai 2004
    Messages
    4 587
    Détails du profil
    Informations personnelles :
    Âge : 54

    Informations forums :
    Inscription : Mai 2004
    Messages : 4 587
    Points : 7 103
    Points
    7 103
    Par défaut
    Bonjour,

    je ne saisis pas la logique de la transformation, il faudrait que tu décrives un peu plus formellement ton problème.
    FAQ XML
    ------------
    « Le moyen le plus sûr de cacher aux autres les limites de son savoir est de ne jamais les dépasser »
    Giacomo Leopardi

  3. #3
    Candidat au Club
    Profil pro
    Inscrit en
    Mars 2007
    Messages
    8
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Mars 2007
    Messages : 8
    Points : 2
    Points
    2
    Par défaut
    pardon (et merci de répondre) je vais essayer autrement :

    A partir de quelque chose 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
    <eCH0083:record>
    	<eCH0083:otherPersonId>
    	  <eCH0044:personIdCategory>CT.VD</eCH0044:personIdCategory>
    	  <eCH0044:personId>3913471</eCH0044:personId>
    	</eCH0083:otherPersonId>
    	<eCH0083:otherPersonId>
    	  <eCH0044:personIdCategory>MU.5586</eCH0044:personIdCategory>
    	  <eCH0044:personId>558612356</eCH0044:personId>
    	</eCH0083:otherPersonId>
    </eCH0083:record>
    <eCH0083:record>
    	<eCH0083:otherPersonId>
    	  <eCH0044:personIdCategory>CT.VD</eCH0044:personIdCategory>
    	  <eCH0044:personId>3913477</eCH0044:personId>
    	</eCH0083:otherPersonId>
    	<eCH0083:otherPersonId>
    	  <eCH0044:personIdCategory>CH.ZEMIS</eCH0044:personIdCategory>
    	  <eCH0044:personId>5684963180</eCH0044:personId>
    	</eCH0083:otherPersonId>
    	<eCH0083:otherPersonId>
    	  <eCH0044:personIdCategory>MU.5586</eCH0044:personIdCategory>
    	  <eCH0044:personId>558612362</eCH0044:personId>
    	</eCH0083:otherPersonId>
    </eCH0083:record>
    je dois obtenir un fichier texte / csv, qui donne ça :

    CT.VD ; ; MU.5586 ;
    CT.VD ; CH.ZEMIS ; MU.5586 ;

  4. #4
    Expert éminent
    Avatar de GrandFather
    Inscrit en
    Mai 2004
    Messages
    4 587
    Détails du profil
    Informations personnelles :
    Âge : 54

    Informations forums :
    Inscription : Mai 2004
    Messages : 4 587
    Points : 7 103
    Points
    7 103
    Par défaut
    Ca n'est pas encore ça.

    Est-ce que ça veut dire que les « CH.ZEMIS » et les « MU.5586 » occupent toujours les mêmes rangs parmi les fils de otherPersonId et que la sortie CSV doit en tenir compte en les remplaçant par un blanc quand ils sont absents ?
    FAQ XML
    ------------
    « Le moyen le plus sûr de cacher aux autres les limites de son savoir est de ne jamais les dépasser »
    Giacomo Leopardi

  5. #5
    Candidat au Club
    Profil pro
    Inscrit en
    Mars 2007
    Messages
    8
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Mars 2007
    Messages : 8
    Points : 2
    Points
    2
    Par défaut
    oui c'est exactement ça !

    J'ai une suite de xsl:choose pour chaque otherPersonId :

    SI idPersonCategory = CT.VD
    insère "CT.VD"
    SINON
    insère ";"

    SI idPersonCategory = CH.ZEMIS
    insère "CH.ZEMIS"
    SINON
    insère ";"

    SI idPersonCategory = MU.5586
    insère "MU.5586"
    SINON
    insère ";"

    J'arrive à tester le idPersonCategory avec xsl:choose et xsl:otherwise, mon problème est que mon résultat me donne toujours le premier fils des otherPersonId.

    Par exemple

    SI idPersonCategory = CH.ZEMIS
    il insère "CT.VD"

    Voici mon test xsl pour l'exemple "CH.ZEMIS":

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    <xsl:choose>
    	<xsl:when test=".//eCH0044:personIdCategory='CH.ZEMIS'">
    		<xsl:value-of select="./eCH0044:personIdCategory"/>ZEMIS<xsl:text>&#x9;</xsl:text>
    	</xsl:when>
    	<xsl:otherwise>
    		<xsl:text>&#x9;</xsl:text>
    	</xsl:otherwise>
    </xsl:choose>
    c'est un problème de positionnement dans les fils de otherPersonId

  6. #6
    Candidat au Club
    Profil pro
    Inscrit en
    Mars 2007
    Messages
    8
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Mars 2007
    Messages : 8
    Points : 2
    Points
    2
    Par défaut
    Suis-je le seul à rencontrer ce problème ?

    je dois mal m'y prendre dans mes explications alors je refais un essai :

    en xml, j'ai une liste de personnes qui ont un identifiant principal et un nombre variable d'autres identifiants (otherPersonId) qui ont eux-même un fils personIdCategory et un fils personId (la valeur qui m'intéresse au final).

    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
    <personne>
    	<identifiants>
    		<localPersonId>
    			<personIdCategory>ID local</personIdCategory>
    			<personId>1234</personId>
    		</localPersonId>
    		<otherPersonId>
    			<personIdCategory>N° CT.VD</personIdCategory>
    			<personId>3913477</personId>
    		</otherPersonId>
    		<otherPersonId>
    			<personIdCategory>N° CH.ZEMIS</personIdCategory>
    			<personId>5684963180</personId>
    		</otherPersonId>
    		<otherPersonId>
    			<personIdCategory>N° MU.1234</personIdCategory>
    			<personId>558612362</personId>
    		</otherPersonId>
    	</identifiants>
    	<nom>Jean</nom>
    </personne>
    <personne>
    	<identifiants>
    		<localPersonId>
    			<personIdCategory>ID local</personIdCategory>
    			<personId>5678 </personId>
    		</localPersonId>
    		<otherPersonId>
    			<personIdCategory>N° CT.VD</personIdCategory>
    			<personId>3913471</personId>
    		</otherPersonId>
    		<otherPersonId>
    			<personIdCategory>N° MU.1234</personIdCategory>
    			<personId>558612356</personId>
    		</otherPersonId>
    	</identifiants>
    	<nom>Paul</nom>
    </personne>
    Je veux traduire cela sous forme de tableau (pour importer dans Excel par exemple) :

    | Personne | ID local | N° CT.VD | N° CH.ZEMIS | N° MU.1234 |
    | Jean | 1234 | 3913477 | 5684963180 | 558612362 |
    | Paul | 5678 | 3913471 | | 558612356 |

    j'ai de la peine à croire que ce problème là n'est pas plus souvent rencontré, mais je n'ai pas trouvé de solution pour l'instant. C'est pourquoi je me permet d'insister...

  7. #7
    Expert éminent
    Avatar de GrandFather
    Inscrit en
    Mai 2004
    Messages
    4 587
    Détails du profil
    Informations personnelles :
    Âge : 54

    Informations forums :
    Inscription : Mai 2004
    Messages : 4 587
    Points : 7 103
    Points
    7 103
    Par défaut
    C'est un problème qui peut être facilement résolu grace à la possibilité offerte en XPath de sélectionner le k-ième noeud d'une sélection ; tu trouveras des exemples ici : http://jerome.developpez.com/xmlxsl/...lpage=&rpage=4
    FAQ XML
    ------------
    « Le moyen le plus sûr de cacher aux autres les limites de son savoir est de ne jamais les dépasser »
    Giacomo Leopardi

  8. #8
    Candidat au Club
    Profil pro
    Inscrit en
    Mars 2007
    Messages
    8
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Mars 2007
    Messages : 8
    Points : 2
    Points
    2
    Par défaut
    Merci GrandFather, je suis parvenu au résultat que je voulais mais ce n'est pas très propre et je pensais qu'il y aurait un meilleure moyen.

    Lorsque j'ai 3 noeuds (le maximum), aucun problème, car cela correspond à la structure de mon tableau.

    Mais lorsque j'ai que 2 ou 1 noeud, je dois les placer au bon endroit dans mon tableau donc je dois faire plusieurs test sur le même k-ième noeud pour savoir à quelle colonne il appartient.

    Voici un bout de mon xslt :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    <xsl:choose>
    	<xsl:when test=".//eCH0083:otherPersonId[2]/eCH0044:personIdCategory[starts-with(.,'MU.')]">
    		<xsl:value-of select=".//eCH0083:otherPersonId[2]/eCH0044:personIdCategory"/><xsl:text>&#x9;</xsl:text>
    	</xsl:when>
    	<xsl:when test=".//eCH0083:otherPersonId[3]/eCH0044:personIdCategory[starts-with(.,'MU.')]">
    		<xsl:value-of select=".//eCH0083:otherPersonId[3]/eCH0044:personIdCategory"/><xsl:text>&#x9;</xsl:text>
    	</xsl:when>
    	<xsl:otherwise>
    		<xsl:copy-of select='$vide'/>
    	</xsl:otherwise>
    </xsl:choose>
    ça marche, mais c'est gérable uniquement parce que j'ai au max 3 noeuds identiques. J'aimerai bien trouver une solution qui marche pour x noeuds. Mais là, faudrait une bonne vieille boucle, ce qui semble être un peu contre nature en xslt...

    Merci encore, et si je trouve quelque chose de mieux je vous redis

  9. #9
    Expert éminent
    Avatar de GrandFather
    Inscrit en
    Mai 2004
    Messages
    4 587
    Détails du profil
    Informations personnelles :
    Âge : 54

    Informations forums :
    Inscription : Mai 2004
    Messages : 4 587
    Points : 7 103
    Points
    7 103
    Par défaut
    Citation Envoyé par fgachet Voir le message
    ça marche, mais c'est gérable uniquement parce que j'ai au max 3 noeuds identiques. J'aimerai bien trouver une solution qui marche pour x noeuds. Mais là, faudrait une bonne vieille boucle, ce qui semble être un peu contre nature en xslt...
    XSLT est effectivement plus orienté récursif qu'itératif, mais il est très facile d'émuler une boucle for avec un template récursif (tu trouveras un exemple dans la FAQ). Si tu veux mettre en oeuvre quelque chose d'un peu plus sophistique pour pouvoir changer de test de chaîne selon la position, tu peux aussi utiliser une table de lookup (tu en trouveras plusieurs exemples en recherchant un peu sur ce forum).
    FAQ XML
    ------------
    « Le moyen le plus sûr de cacher aux autres les limites de son savoir est de ne jamais les dépasser »
    Giacomo Leopardi

Discussions similaires

  1. Conversion de longs fichiers texte en CSV
    Par cantador dans le forum Delphi
    Réponses: 26
    Dernier message: 03/12/2014, 14h56
  2. Réponses: 4
    Dernier message: 18/02/2014, 17h06
  3. Réponses: 2
    Dernier message: 03/02/2014, 08h06
  4. Réponses: 5
    Dernier message: 17/04/2011, 10h52
  5. Qu'apporte le XML en plus d'un fichier texte?
    Par Nasky dans le forum XML/XSL et SOAP
    Réponses: 4
    Dernier message: 07/07/2005, 10h46

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