使用 xslt 将 CSV 转换为 XML - 如何增加列名 [英] CSV to XML using xslt - how to have incrementing column name

查看:27
本文介绍了使用 xslt 将 CSV 转换为 XML - 如何增加列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个 xslt 将 csv 转换为 xml,工作正常,除了所有列的标记都相同.我需要它像这样递增

I have this xslt to convert a csv to xml, works fine, except the tag is the same for all columns. I need it to increment like this

<row>
  <column1></column1>
  <column2></column2>
  <column3></column3>
</row>

当我使用 position() 时,它会将所有列重命名为 column1

when I use position() it renames all the columns to column1

<xsl:element name="{concat('column', position())}">

这是 xslt:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" omit-xml-declaration="yes"/>
    <xsl:variable name="LF" select="'&#xA;'"/>
    <!-- template that matches the root node-->
    <xsl:template match="/">
        <root>
            <xsl:call-template name="texttorows">
                <xsl:with-param name="StringToTransform" select="/root"/>
            </xsl:call-template>
        </root>
    </xsl:template>
    <!-- template that actually does the conversion-->
    <xsl:template name="texttorows">
        <!-- import $StringToTransform-->
        <xsl:param name="StringToTransform" select="''"/>
        <xsl:choose>
            <!-- string contains linefeed-->
            <xsl:when test="contains($StringToTransform,$LF)">
                <!-- Get everything up to the first carriage return-->
                <row>
                    <xsl:call-template name="csvtoxml">
                        <xsl:with-param name="StringToTransform" select="substring-before($StringToTransform,$LF)"/>
                    </xsl:call-template>
                </row>
                <!-- repeat for the remainder of the original string-->
                <xsl:call-template name="texttorows">
                    <xsl:with-param name="StringToTransform">
                        <xsl:value-of select="substring-after($StringToTransform,$LF)"/>
                    </xsl:with-param>
                </xsl:call-template>
            </xsl:when>
            <!-- string does not contain newline, so just output it-->
            <xsl:otherwise>
                <row>
                    <xsl:call-template name="csvtoxml">
                        <xsl:with-param name="StringToTransform" select="$StringToTransform"/>
                    </xsl:call-template>
                </row>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <xsl:template name="csvtoxml">
        <!-- import $StringToTransform-->
        <xsl:param name="StringToTransform" select="''"/>
        <xsl:choose>
            <!-- string contains linefeed-->
            <xsl:when test="contains($StringToTransform,',')">
                <!-- Get everything up to the first carriage return-->
                <xsl:element name="{concat('column', position())}">
                    <xsl:value-of select="substring-before($StringToTransform,',')"/>
                </xsl:element>
                <!-- repeat for the remainder of the original string-->
                <xsl:call-template name="csvtoxml">
                    <xsl:with-param name="StringToTransform">
                        <xsl:value-of select="substring-after($StringToTransform,',')"/>
                    </xsl:with-param>
                </xsl:call-template>
            </xsl:when>
            <!-- string does not contain newline, so just output it-->
            <xsl:otherwise>
                <column>
                    <xsl:value-of select="$StringToTransform"/>
                </column>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

这是一个示例 csv:

Here is a sample csv:

<root>
3779490,916705,CS,60,34.89,Sauce/Cholula
5918104,918958,CS,6,20.63,Pasta/Fresh/Cavatelli/6#/Frozen
5064774,920723,CS,10,45.5,Cheese/Oaxaca
3422752,925230,EA,8,69.6,Chipotle/Powder/Ground
5955640,BB171,CS,30,50.7,Butter/Unsalted
5295326,BC110005,CS,6000,54.95,Oil/Olive/Finishing
</root>

推荐答案

看起来好像 csvtoxml 正在使用一个大字符串调用,并且它递归地通过该字符串进行工作.position() 在这种情况下不起作用,因为您没有使用一组节点.

It looks as though csvtoxml is being called with a large string and it recursively works it's way through that string. position() won't work in this case because you're not working with a set of nodes.

相反,您可以使用计数参数来实现您的目标:

Instead you might be able to achieve what you're after with a counting param:

<xsl:template name="csvtoxml">
    <!-- import $StringToTransform-->
    <xsl:param name="StringToTransform" select="''"/>
    <xsl:param name="ColumnNum" select="1"/>
    <xsl:choose>
        <!-- string contains linefeed-->
        <xsl:when test="contains($StringToTransform,',')">
            <!-- Get everything up to the first carriage return-->
            <xsl:element name="{concat('column', $ColumnNum)}">
                <xsl:value-of select="substring-before($StringToTransform,',')"/>
            </xsl:element>
            <!-- repeat for the remainder of the original string-->
            <xsl:call-template name="csvtoxml">
                <xsl:with-param name="StringToTransform" select="substring-after($StringToTransform,',')" />
                <xsl:with-param name="ColumnNum" select="$ColumnNum + 1" />
            </xsl:call-template>
        </xsl:when>
        <!-- string does not contain newline, so just output it-->
        <xsl:otherwise>
            <xsl:element name="{concat('column', $ColumnNum)}">
                <xsl:value-of select="$StringToTransform" />
            </xsl:element>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

这篇关于使用 xslt 将 CSV 转换为 XML - 如何增加列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆