Nwlapcug.com


Come convertire XML in formato CSV utilizzando XSL

Come convertire XML in formato CSV utilizzando XSL


Il crescente utilizzo delle tecnologie XML in varie applicazioni ha provocato la necessità per i file XML di output in diversi formati quindi sistemi legacy possono riutilizzarli. A molti programmatori e professionisti IT, questa conversione è spesso richiede tempo e richiede attente considerazioni delle strutture di dati incorporate nel documento XML. EXtensible Stylesheet Language (XSL) assiste i programmatori nella trasformazione di file XML in HTML.

Istruzioni

1

Visualizzazione XML richiede l'utilizzo di XSLT eXtensible Stylesheet Language Transformations (), che è la lingua del foglio di stile consigliato di XML di World Wide Web Consortium (W3C) ed è più sofisticato di Cascading Style Sheet (CSS). XSLT è spesso utilizzato per trasformare XML in HTML prima che vengano visualizzati da un browser. Seguito è riportato un esempio di un documento/file XML (ad esempio, Sample. Xml):

<? xml versione = "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 file Sample. XML specificato, è possibile trasformare il codice XML utilizzando il seguente codice di origine XSL (cioè, Sample. Xsl):

<? xml versione = "1,0" encoding = "UTF-8"? >
< xsl: stylesheet versione = "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 Sample CSV, è necessario fare riferimento al foglio di stile XSL sopra nel documento XML utilizzando le informazioni di intestazione: <? xml-stylesheet type = "text/xsl" href="sample.xsl"? >. Il documento XML risultante sarà simile a quello riportato di seguito.

<? xml versione = "1,0" encoding = "UTF-8"? >
<? xml-stylesheet type = "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 precedente codice sorgente Sample. XML in un browser, si genererà un output CSV nel browser, simile a quello riportato di seguito:

10001, Mark, Smith, uomo, 1123 Buffalo Street, Indianapolis, IN, [email protected], 555-555-5553
10002, Jane Doe, donna, 1271 Buffalo Street, Indianapolis, IN, [email protected], 555-555-5554
10003, John, Smith, uomo, 1281 Buffalo Street, Indianapolis, IN, [email protected], 555-555-5555

Consigli & Avvertenze

  • Ci sono altri approcci per la conversione di dati XML in formato CSV. La scelta dipenderà dal tipo di progetto che si sta lavorando. Ad esempio, se si lavora con dati XML archiviati su database MySQL, quindi 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.