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 :

Mise en forme de document XML avec XSL.


Sujet :

XSL/XSLT/XPATH XML

  1. #1
    Membre à l'essai
    Profil pro
    Inscrit en
    Décembre 2010
    Messages
    10
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Décembre 2010
    Messages : 10
    Points : 14
    Points
    14
    Par défaut Mise en forme de document XML avec XSL.
    Bonjour à tous,

    Je récupère via un web service un fichier XML dont voici un résumé très 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
    <?xml version="1.0" encoding="utf-8"?>
    <?xml-stylesheet href="test.xsl" type="text/xsl"?>
    <root>
    	<Products xmlns="" LastRefresh="1/1/2015 10:11:36 PM">
    		<Product ID="243512" ModelName="16593" ManufacturerID="16593" EAN="8713439165937">
    			<Description>DT Cordless</Description>
    			<Description2>Retail, US-Layout, Black</Description2>
    			<VatRate>21</VatRate>
    			<QuantityOnHand>128</QuantityOnHand>
    			<StockLevel>Good</StockLevel>
    			<Status>S</Status>
    			<Brand>Trust</Brand>
    			<MasterGroup>Input Device</MasterGroup>
    			<ProductGroup>Mouse &amp; Keyboard Combination</ProductGroup>
    			<UnitPrice>14,71</UnitPrice>
    			<Staffels/>
    			<Accruels>
    				<Accruel Code="BEBATBE" Description="Bebat Belgium" UnitPrice="0,30"/>
    				<Accruel Code="RECUPEL" Description="Recupel Belgium" UnitPrice="0,08"/>
    			</Accruels>
    		</Product>
    		<Product ID="243521" ModelName="16599" ManufacturerID="16599" EAN="8713439165999">
    			<Description>DT Cordless</Description>
    			<Description2>Retail, BE-Layout, Black</Description2>
    			<VatRate>21</VatRate>
    			<QuantityOnHand>9</QuantityOnHand>
    			<StockLevel>Low</StockLevel>
    			<Status>S</Status>
    			<Brand>Trust</Brand>
    			<MasterGroup>Input Device</MasterGroup>
    			<ProductGroup>Mouse &amp; Keyboard Combination</ProductGroup>
    			<UnitPrice>13,00</UnitPrice>
    			<Staffels/>
    			<Accruels>
    				<Accruel Code="BEBATBE" Description="Bebat Belgium" UnitPrice="0,30"/>
    				<Accruel Code="RECUPEL" Description="Recupel Belgium" UnitPrice="0,08"/>
    			</Accruels>
    		</Product>
    	</Products>
    </root>
    Voici le fichier XSL que j'ai créé pour la mise en 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
    57
    58
    59
    60
    61
    62
    63
    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE xsl:stylesheet []>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    	<xsl:output method="html" encoding="utf-8" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>
    	<xsl:template match="/">
    		<html xmlns="http://www.w3.org/1999/xhtml">
    			<head>
    				<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    				<title>Mis en forme avec XSL</title>
    			</head>
    			<body>
    				<table border="1">
    					<tr>
    						<td>ID produit</td>
    						<td>Nom</td>
    						<td>ManufacturerID</td>
    						<td>Code EAN</td>
    						<td>Description 1</td>
    						<td>Description 2</td>
    						<td>TVA</td>
    						<td>Stock</td>
    						<td>Prochaine livraison</td>
    						<td>Niveau de stock</td>
    						<td>Statut</td>
    						<td>Marque</td>
    						<td>Gatégorie</td>
    						<td>Sous-catégorie</td>
    						<td>Prix d'achat</td>
     
    						<td>Recupel</td>
    						<td>Bebat</td>
    						<td>Auvibel</td>
    						<td>Reprobel</td>
    					</tr>
    					<xsl:for-each select="root/Products/Product">
    						<tr>
    							<td><xsl:value-of select="@ID"/></td>
    							<td><xsl:value-of select="@ModelName"/></td>
    							<td><xsl:value-of select="@ManufacturerID"/></td>
    							<td><xsl:value-of select="@EAN"/></td>
    							<td><xsl:value-of select="Description"/></td>
    							<td><xsl:value-of select="Description2"/></td>
    							<td><xsl:value-of select="VatRate"/></td>
    							<td><xsl:value-of select="QuantityOnHand"/></td>
    							<td><xsl:value-of select="NextDeliveryDate"/></td>
    							<td><xsl:value-of select="StockLevel"/></td>
    							<td><xsl:value-of select="Status"/></td>
    							<td><xsl:value-of select="Brand"/></td>
    							<td><xsl:value-of select="MasterGroup"/></td>
    							<td><xsl:value-of select="ProductGroup"/></td>
    							<td><xsl:value-of select="UnitPrice"/></td>
     
                                                            <td></td>
                                                            <td></td>
                                                            <td></td>
                                                            <td></td>
       						</tr>
    					</xsl:for-each>
    				</table>			
    			</body>
    		</html>
    	</xsl:template>
    </xsl:stylesheet>
    Tout fonctionne, mais en fait je ne sais pas comment je pourrais afficher dans les 4 dernières colonnes de mon tableau:

    la valeur de l'attribut "UnitPrice" de chaque balise "Accruel" dont l'attribut "Code" = "RECUPEL"
    la valeur de l'attribut "UnitPrice" de chaque balise "Accruel" dont l'attribut "Code" = "BEBATBE"
    la valeur de l'attribut "UnitPrice" de chaque balise "Accruel" dont l'attribut "Code" = "AUVIBEL"
    la valeur de l'attribut "UnitPrice" de chaque balise "Accruel" dont l'attribut "Code" = "REPROBEL"

    J'ai beau chercher sur google une méthode, je tombe sur d'autres cas de figure.

    Mes sources sont en pièces jointes.

    Pourriez-vous m'aider svp?

    D'avance merci
    Fichiers attachés Fichiers attachés

  2. #2
    Modérateur

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

    Informations forums :
    Inscription : Septembre 2004
    Messages : 12 547
    Points : 21 602
    Points
    21 602
    Par défaut
    Hello,

    En gros tu dois apprendre à utiliser les prédicats XPath.
    "Sélectionne les <Accruel>, mais ne garde que ceux dont l'attribut Code est RECUPEL. Là-dedans, sélectionne l'attribut UnitPrice." Ça se dit :

    Code XPath : Sélectionner tout - Visualiser dans une fenêtre à part
    Accruel[@Code = 'RECUPEL']/@UnitPrice
    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

  3. #3
    Membre à l'essai
    Profil pro
    Inscrit en
    Décembre 2010
    Messages
    10
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Décembre 2010
    Messages : 10
    Points : 14
    Points
    14
    Par défaut Merci beaucoup, ça fonctionne!
    Voici la solution complète à mon problème pour ceux que ça pourrait aider.
    Encore merci.

    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
    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE xsl:stylesheet []>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    	<xsl:output method="html" encoding="utf-8" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>
    	<xsl:template match="/">
    		<html xmlns="http://www.w3.org/1999/xhtml">
    			<head>
    				<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    				<title>Mis en forme avec XSL</title>
    			</head>
    			<body>
    				<table border="1">
    					<tr>
    						<td>ID produit</td>
    						<td>Nom</td>
    						<td>ManufacturerID</td>
    						<td>Code EAN</td>
    						<td>Description 1</td>
    						<td>Description 2</td>
    						<td>TVA</td>
    						<td>Stock</td>
    						<td>Prochaine livraison</td>
    						<td>Niveau de stock</td>
    						<td>Statut</td>
    						<td>Marque</td>
    						<td>Gatégorie</td>
    						<td>Sous-catégorie</td>
    						<td>Prix d'achat</td>
    						<td>Recupel</td>
    						<td>Bebat</td>
    						<td>Auvibel</td>
    						<td>Reprobel</td>
    					</tr>
    					<xsl:for-each select="root/Products/Product">
    						<tr>
    							<td><xsl:value-of select="@ID"/></td>
    							<td><xsl:value-of select="@ModelName"/></td>
    							<td><xsl:value-of select="@ManufacturerID"/></td>
    							<td><xsl:value-of select="@EAN"/></td>
    							<td><xsl:value-of select="Description"/></td>
    							<td><xsl:value-of select="Description2"/></td>
    							<td><xsl:value-of select="VatRate"/></td>
    							<td><xsl:value-of select="QuantityOnHand"/></td>
    							<td><xsl:value-of select="NextDeliveryDate"/></td>
    							<td><xsl:value-of select="StockLevel"/></td>
    							<td><xsl:value-of select="Status"/></td>
    							<td><xsl:value-of select="Brand"/></td>
    							<td><xsl:value-of select="MasterGroup"/></td>
    							<td><xsl:value-of select="ProductGroup"/></td>
    							<td><xsl:value-of select="UnitPrice"/></td>
                                                            <td><xsl:value-of select="Accruels/Accruel[@Code = 'RECUPEL']/@UnitPrice"/></td>
    							<td><xsl:value-of select="Accruels/Accruel[@Code = 'BEBATBE']/@UnitPrice"/></td>
    							<td><xsl:value-of select="Accruels/Accruel[@Code = 'AUVIBEL']/@UnitPrice"/></td>
    							<td><xsl:value-of select="Accruels/Accruel[@Code = 'REPROBEL']/@UnitPrice"/></td>
    						</tr>
    					</xsl:for-each>
    				</table>			
    			</body>
    		</html>
    	</xsl:template>
    </xsl:stylesheet>

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

Discussions similaires

  1. Mise en page XML avec XSL et XML externe
    Par snopims dans le forum XSL/XSLT/XPATH
    Réponses: 1
    Dernier message: 03/08/2011, 16h57
  2. Réponses: 2
    Dernier message: 06/11/2008, 17h30
  3. extraire un fichier d'un fichier XML avec XSL ?
    Par pirbd dans le forum XSL/XSLT/XPATH
    Réponses: 6
    Dernier message: 14/09/2005, 11h31
  4. [débutant]XML vers XML avec XSL
    Par tokamak dans le forum XSL/XSLT/XPATH
    Réponses: 10
    Dernier message: 11/07/2005, 11h27
  5. XML vers XML avec XSL
    Par guizz79 dans le forum XSL/XSLT/XPATH
    Réponses: 3
    Dernier message: 27/06/2005, 10h43

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