Come convertire XML in formato CSV Utilizzando XSL

February 9

Come convertire XML in formato CSV Utilizzando XSL


Il crescente utilizzo di tecnologie XML in varie applicazioni ha comportato la necessità di file XML di output in diversi formati in modo da sistemi legacy loro possono riutilizzare. Per molti programmatori e professionisti IT, questa conversione è spesso in termini di tempo e richiede attente considerazioni delle strutture di dati incorporati nel documento XML. Extensible Stylesheet Language (XSL) assiste i programmatori a trasformare i file XML in HTML.

istruzione

1 Visualizzazione XML richiede l'utilizzo di Extensible Stylesheet Language Transformations (XSLT), che è il linguaggio del foglio di stile raccomandata di XML World Wide Web Consortium (W3C) ed è più sofisticato di Cascading Style Sheet (CSS). XSLT è spesso utilizzato per trasformare XML in HTML prima che venga visualizzato da un browser. Un esempio di un file / documento XML (cioè, sample.xml) è la seguente:

<? Xml version = "1.0" encoding = "UTF-8"?>
<studenti>

&lt;student id=&quot;10001&quot;>
&lt;name given=&quot;Mark&quot; family=&quot;Smith&quot;/>
&lt;gender>Male&lt;/gender>
&lt;address street=&quot;1123 Buffalo Street&quot; city=&quot;Indianapolis&quot; state=&quot;IN&quot;/>
&lt;emailAddress>[email protected]&lt;/emailAddress>
&lt;phoneNumber>555-555-5553&lt;/phoneNumber>
&lt;/student>
.....
.....

</ Studenti>

Nel dato file sample.xml, è possibile trasformare il codice XML utilizzando il codice sorgente seguente XSL (vale a dire, sample.xsl):

<? Xml version = "1.0" encoding = "UTF-8"?>
<Xsl: stylesheet version = "1.0" xmlns: xsl = "http://www.w3.org/1999/XSL/Transform&quot;

xmlns=&quot;http://www.w3.org/1999/xhtml&quot;>
&lt;xsl:template match=&quot;/&quot;>
&lt;html>
&lt;table>
&lt;xsl:for-each select=&quot;//student&quot;>
&lt;tr>
&lt;td>
&lt;xsl:value-of select=&quot;@id&quot;/>
&lt;xsl:value-of select=&quot;','&quot;/>
&lt;xsl:value-of select=&quot;name/@given&quot;/>
&lt;xsl:value-of select=&quot;','&quot;/>
&lt;xsl:value-of select=&quot;name/@family&quot;/>
&lt;xsl:value-of select=&quot;','&quot;/>
&lt;xsl:value-of select=&quot;gender&quot;/>
&lt;xsl:value-of select=&quot;','&quot;/>
&lt;xsl:value-of select=&quot;address/@street&quot;/>
&lt;xsl:value-of select=&quot;','&quot;/>
&lt;xsl:value-of select=&quot;address/@city&quot;/>
&lt;xsl:value-of select=&quot;','&quot;/>
&lt;xsl:value-of select=&quot;address/@state&quot;/>
&lt;xsl:value-of select=&quot;','&quot;/>
&lt;xsl:value-of select=&quot;emailAddress&quot;/>
&lt;xsl:value-of select=&quot;','&quot;/>
&lt;xsl:value-of select=&quot;phoneNumber&quot;/>
&lt;/td>
&lt;/tr>
&lt;/xsl:for-each>
&lt;/table>
&lt;/html>
&lt;/xsl:template>

</ Xsl: stylesheet>

2 Per convertire il file in formato CSV sample.xml, è necessario fare riferimento al di sopra del foglio di stile XSL nel documento XML utilizzando le informazioni di intestazione: <? Xml-stylesheet type = "text / xsl" href = "sample.xsl"> . Il documento XML risultante sarà simile a quella mostrata di seguito.

<? Xml version = "1.0" encoding = "UTF-8"?>
<? Xml-stylesheet tipo = "text / xsl" href = "sample.xsl"?>
<studenti>

&lt;student id=&quot;10001&quot;>
&lt;name given=&quot;Mark&quot; family=&quot;Smith&quot;/>
&lt;gender>Male&lt;/gender>
&lt;address street=&quot;1123 Buffalo Street&quot; city=&quot;Indianapolis&quot; state=&quot;IN&quot;/>
&lt;emailAddress>[email protected]&lt;/emailAddress>
&lt;phoneNumber>555-555-5553&lt;/phoneNumber>
&lt;/student>
&lt;student id=&quot;10002&quot;>
&lt;name given=&quot;Jane&quot; family=&quot;Doe&quot;/>
&lt;gender>Female&lt;/gender>
&lt;address street=&quot;1271 Buffalo Street&quot; city=&quot;Indianapolis&quot; state=&quot;IN&quot;/>
&lt;emailAddress>[email protected]&lt;/emailAddress>
&lt;phoneNumber>555-555-5554&lt;/phoneNumber>
&lt;/student>
&lt;student id=&quot;10003&quot;>
&lt;name given=&quot;John&quot; family=&quot;Smith&quot;/>
&lt;gender>Male&lt;/gender>
&lt;address street=&quot;1281 Buffalo Street&quot; city=&quot;Indianapolis&quot; state=&quot;IN&quot;/>
&lt;emailAddress>[email protected]&lt;/emailAddress>
&lt;phoneNumber>555-555-5555&lt;/phoneNumber>
&lt;/student>

</ Studenti>

3 Quando si apre il codice sorgente sample.xml sopra in un browser, è possibile percepire una uscita CSV nel browser, simile a quella mostrata di seguito:

10001, Mark, Smith, Maschio, 1123 Buffalo Street, Indianapolis, IN, marksmith @ abc.com, 555-555-5553
10002, Jane, Doe, Femmina, 1271 Buffalo Street, Indianapolis, IN, janedoe @ abc.com, 555-555-5554
10003, John, Smith, Maschio, 1281 Buffalo Street, Indianapolis, IN, johnsmith @ abc.com, 555-555-5555

Consigli e avvertenze

  • Ci sono altri approcci per la conversione dei dati XML in formato CSV. La scelta dipenderà dal tipo di progetto che si sta lavorando. Ad esempio, se si sta lavorando con un di dati XML memorizzati su database MySQL, allora si può facilmente esportare i dati da MySQL a un foglio di calcolo di Excel e quindi convertire il foglio di calcolo risultante in formato CSV.