我想通過使用「;」將我的自定義XML轉換爲csv。作爲值分隔符。這裏是我的XML:XSLT將我的自定義XML轉換爲CSV
<?xml version="1.0" encoding="utf-8"?>
<root>
<operators>
<item>
<lfbis>1234567</lfbis>
<name>Stefan Maier</name>
<company />
<street>Testweg 7</street>
<citycode>95131</citycode>
<city>Hof</city>
<controlbody>BIKO</controlbody>
<productdata>
<item>Rinder</item>
<item>9</item>
</productdata>
</item>
<item>
<lfbis>5671234</lfbis>
<name>Antom Mueller</name>
<company>Berghof</company>
<street>Testweg 8</street>
<citycode>95111</citycode>
<city>Bamberg</city>
<controlbody>BIKO</controlbody>
<productdata>
<item>Rinder</item>
<item>9</item>
</productdata>
</item>
</operators>
</root>
我迄今管理(使用計算器線程 XML to CSV Using XSLT,尤其是使用代碼在他的回答...a version with configurable parameters that you can set programmatically寫道通過@Tomalak)使用以下XSLT轉換:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="utf-8" />
<xsl:param name="delim" select="';'" />
<xsl:param name="quote" select="'"'" />
<xsl:param name="break" select="'
'" />
<xsl:template match="/">
<xsl:apply-templates select="root/operators/item" />
</xsl:template>
<xsl:template match="item">
<xsl:apply-templates />
<!--<xsl:if test="following-sibling::*">-->
<xsl:value-of select="concat($delim, $break)" />
<!--</xsl:if>-->
</xsl:template>
<xsl:template match="*">
<xsl:value-of select="normalize-space()" />
<xsl:if test="following-sibling::*">
<xsl:value-of select="$delim" />
</xsl:if>
</xsl:template>
<xsl:template match="text()" />
</xsl:stylesheet>
...,我使用xsltproc的得到以下結果:
1234567;Stefan Maier;;Testweg 7;95131;Hof;BIKO;Rinder 9;
5671234;Antom Mueller;Berghof;Testweg 8;95111;Bamberg;BIKO;Rinder 9;
現在,我想實現的是, 項目產品數據元素的子元素也被視爲csv結果中的值。所以我需要一個「;」而不是 「」(空格)的值Rinder和之間,比如我的CSV會是什麼樣子:
1234567;Stefan Maier;;Testweg 7;95131;Hof;BIKO;Rinder;9;
5671234;Antom Mueller;Berghof;Testweg 8;95111;Bamberg;BIKO;Rinder;9;
如果您要使用其他人的代碼,請至少聲明他們而不是說你寫了它。您可以從http://stackoverflow.com/a/365372/18771獲取XSLT代碼。 – Tomalak
是的,對不起,我當然使用了stackoverflow線程來編寫xlst樣式表。我向那個線程中的人們道歉,特別是對於樣式表的作者,在我的問題中沒有提及他。我將在未來的職位更加小心。 –