简单的XSLT将XML修改为CSV



我有下面的XSLT,它在为XML生成结果时非常有效,现在我需要稍微修改它以设置CSV结果。任何建议。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="mon" match="Column[substring(@name, 4, 1) = '_']"     use="concat(parent::*/@rowNum, substring(@name, 1, 3))"/>
<xsl:output indent="yes"/>
<xsl:template match="node()">
    <xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates select="node()"/>
    </xsl:copy>
</xsl:template>
<xsl:template match="DataSet">
    <xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:for-each select="*/Column[generate-id() = generate-id(key('mon', concat(parent::*/@rowNum, substring(@name, 1, 3)))[1])]">
            <xsl:element name="{name(parent::*)}">
                <xsl:attribute name="rowNum">
                    <xsl:value-of select="position()"/>
                </xsl:attribute>
                <xsl:attribute name="mon">
                    <xsl:value-of select="@name"/>
                </xsl:attribute>
                <xsl:copy-of select="parent::*/Column[not(substring(@name, 4, 1) = '_')]"/>
                <xsl:copy-of select="key('mon', concat(parent::*/@rowNum, substring(@name, 1, 3)))"/>
            </xsl:element>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>

如果没有关于源文件的信息,这只能是一个粗略的猜测,但有些东西像这样:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="mon" match="Column[substring(@name, 4, 1) = '_']"     use="concat(parent::*/@rowNum, substring(@name, 1, 3))"/>
<xsl:output method="text"/>
<xsl:template match="DataSet/*">
   <xsl:text>&#10;</xsl:text>
</xsl:template>
<xsl:template match="DataSet">
 <xsl:for-each select="*/Column[generate-id() = generate-id(key('mon', concat(parent::*/@rowNum, substring(@name, 1, 3)))[1])]">
  <xsl:value-of select="name(parent::*)"/>
  <xsl:text>, </xsl:text>   
  <xsl:value-of select="position()"/>
  <xsl:text>, </xsl:text>   
  <xsl:value-of select="@name"/>
  <xsl:text>, </xsl:text>   
  <xsl:value-of select="parent::*/Column[not(substring(@name, 4, 1) = '_')]"/>
  <xsl:text>, </xsl:text>   
  <xsl:value-of select="key('mon', concat(parent::*/@rowNum, substring(@name, 1, 3)))"/>
 </xsl:for-each>
</xsl:template>
</xsl:stylesheet>

最新更新