XSLT: XML zu CSV mit Kopfzeile
Juli 12, 2010
Folgendes XSLT-Transformation generiert eine CSV-Datei, wobei die XML-Tag-Namen als Kopfzeile ausgegeben werden. Die Tag-Namen müssen für diese Transformation nicht bekannt sein.
XSL:
-
<xsl:stylesheet version=“1.0″ xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”>
-
<xsl
utput omit-xml-declaration=“yes” method=“html” indent=“yes” encoding=“ISO-8859-1″/> -
-
<xsl:strip-space elements=“*” />
-
<!–Erstellung der Kopfzeile–>
-
<xsl:template match=“/*/child::*”>
-
<xsl:for-each select=“*[1]/child::*”>
-
<xsl:if test=“position() != last()”>"<xsl:value-of select=“name()”/>";</xsl:if>
-
<xsl:if test=“position() = last()”>"<xsl:value-of select=“name()”/>"<xsl:text>
</xsl:text></xsl:if>
-
</xsl:for-each>
-
<xsl:apply-templates/>
-
</xsl:template>
-
-
<!–Erstellung der Inhaltszeilen–>
-
<xsl:template match=“/*/*/child::*”>
-
<xsl:for-each select=“child::*”>
-
<xsl:if test=“position() != last()”>"<xsl:value-of select=“normalize-space(.)”/>";</xsl:if>
-
<xsl:if test=“position() = last()”>"<xsl:value-of select=“normalize-space(.)”/>"<xsl:text>
</xsl:text>
-
</xsl:if>
-
</xsl:for-each>
-
-
</xsl:template>
-
</xsl:stylesheet>
Beispiel-XML:
-
<?xml version=“1.0″ encoding=“UTF-8″?>
-
<DATA>
-
<LIST>
-
<PRODUKT>
-
<TYP>AUTO</TYP>
-
<BESCHREIBUNG>VW</BESCHREIBUNG>
-
</PRODUKT>
-
<PRODUKT>
-
<TYP>AUTO</TYP>
-
<BESCHREIBUNG>DAIMLER</BESCHREIBUNG>
-
</PRODUKT>
-
<PRODUKT>
-
<TYP>AUTO</TYP>
-
<BESCHREIBUNG>OPEL</BESCHREIBUNG>
-
</PRODUKT>
-
</LIST>
-
</DATA>
Als Tool zum testen der Transformation hat sich: TWXml2Csv bewährt.
Links zum Thema:
Kopieren mit XSLT
Filed under: Coding | Comments (0)