Re: Xml vers Csv

Stephane Bortzmeyer <[email protected]>
Newsgroups gmane.text.xml.french.tech
Message-ID <[email protected]>
On Wed, Dec 20, 2006 at 09:38:48AM +0100,
 NIANG djibril <[email protected]> wrote 
 a message of 38 lines which said:
> Je suis débutant en Xml. J'ai une fonction Php qui me crée un
> fichier xml. je voudrais à partir de ce fichier créer le fichier csv
> correspondant et l'afficher.

D'abord, il faut bien comprendre que la spécification du problème est
un peu courte. Par exemple, si le fichier XML est :

<foo>
<bar>1</bar>
<bar>2</bar>
<baz>3</baz>
</foo>

Quel doit être le CSV résultant ? D'une manière générale, si un
élément peut apparaitre plusieurs fois, la traduction en CSV va être
problématique.

Ensuite, une fois le problème mieux spécifié, vous avez plusieurs
solutions : XSLT en est une mais n'importe quel langage de
programmation conviendra. À titre d'exemple, j'ai joint un fichier XML
décrivant des langues, un script XSLT qui produit le CSV (également
attaché) et un script en un langage impératif traditionnel (Python)
qui produit le même CSV.




-- Binary/unsupported file stripped by Ecartis --
-- Type: application/xml


-- Attached file included as plaintext by Ecartis --

<?xml version='1.0' encoding="iso-8859-1"?>
<!DOCTYPE stylesheet [
<!ENTITY newln "&#xA;">
]>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:str="http://exslt.org/strings"
  version='1.0'>
  
  <xsl:output method="text" encoding="utf-8"/>
  
  <xsl:template match="languages">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="language">
    <xsl:apply-templates select="code"/>
    <xsl:text>,</xsl:text>
    <xsl:apply-templates select="name"/>
    <xsl:text>&newln;</xsl:text>
  </xsl:template>

  <xsl:template match="name">
    <xsl:value-of select="text()"/>
  </xsl:template>

  <xsl:template match="code">
    <xsl:value-of select="text()"/>
  </xsl:template>

  <xsl:template match="text()">
    <!-- Ignore everything else -->
  </xsl:template>

</xsl:stylesheet>

-- Binary/unsupported file stripped by Ecartis --
-- Type: text/x-python


-- Binary/unsupported file stripped by Ecartis --
-- Type: text/comma-separated-values
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.