XML et XSLT en dotNet


 5/27/2015

Exemple simple de l'utilisation de xml et xslt :

Fichier XML :

<?xml version="1.0" encoding="iso-8859-1"?>
<personnes>
  <personne>
    <name>Toto</name>
    <age>15</age>
    <city>Paris</city>
  </personne>
  <personne>
    <name>Dupont</name>
    <age>59</age>
    <city>Rennes</city>
  </personne>
  <personne>
    <name>Durand</name>
    <age>46</age>
    <city>Toulouse</city>
  </personne>
  <personne>
    <name>Martin</name>
    <age>35</age>
    <city>Lyon</city>
  </personne>
</personnes>

Fichier XSLT :

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <html>
      <body>
        <h2>Personnes</h2>
        <table border="1">
          <tr bgcolor="#9acd32">
            <th>Name</th>
            <th>Age</th>
            <th>City</th>
          </tr>
          <xsl:for-each select="personnes/personne">
            <tr>
              <td bgcolor="yellow">
                <xsl:value-of select="name"/>
              </td>
              <td>
                <xsl:value-of select="age"/>
              </td>
              <td>
                <xsl:value-of select="city"/>
              </td>
            </tr>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

Page web :

<asp:Xml ID="Xml1" runat="server"></asp:Xml>

CodeBehind :

XmlDocument xmlDoc = new XmlDocument();
XslTransform xslT = new XslTransform();
xmlDoc.Load(Server.MapPath("XMLFile.xml"));
xslT.Load(Server.MapPath("XSLTFile.xslt"));
Xml1.Document = xmlDoc;
Xml1.Transform = xslT;