使用xslt从xml数据在csv中将数组的数组创建为csv中的一个字段时,添加逗号作为定界符 [英] Add comma as delimiter when creating array of arrays as one field in csv from xml data using xslt

查看:145
本文介绍了使用xslt从xml数据在csv中将数组的数组创建为csv中的一个字段时,添加逗号作为定界符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的实际数据如下:

<image name="101272.dcm">
<gobject type="Right"><polyline><vertex index="0" t="0.0" x="636.0" y="1920.0" z="0.0"/><vertex index="1" t="0.0" x="604.0" y="2296.0" z="0.0"/></polyline></gobject>
</image>
<image name="101280.dcm">
<gobject type="Right"><polyline><vertex index="0" t="0.0" x="1776.0" y="392.0" z="0.0"/><vertex index="1" t="0.0" x="1456.0" y="424.0" z="0.0"/></polyline></gobject>
</image>

我成功地根据需要提取了所有其他数据.但是,像Im是xslt的初学者一样,使用逗号作为定界符会很痛苦.

I was successful in extracting all other data as required. But using the comma as delimiter is painful as Im a beginner in xslt.

我正在使用xslt版本2,我的代码是:

Im using xslt version 2 and my code is :

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="/">
            seriesUID,annotationType,coordSys,data,name,label

        <xsl:for-each select="//gobject">
            <xsl:variable name="seriesUID" select="ancestor::image/@name"/>
            <xsl:value-of select="substring-before($seriesUID,'.dcm')"/>
            <xsl:value-of select="concat(',','polygon')"/>
            <xsl:value-of select="@annotationType"/>
            <xsl:value-of select="concat(',','Image')"/>
            <xsl:value-of select="@coordSys"/>
            <xsl:value-of select="concat(',','[')"/>
            <xsl:for-each select="polyline/vertex">
                <xsl:value-of select="concat('','[')"/>
                <xsl:value-of select="@x"/>
                <xsl:value-of select="@y"/>
                <xsl:value-of select="@z"/>
                <xsl:value-of select="concat(']','')"/>
            </xsl:for-each>
            <xsl:value-of select="concat(']','',',')"/>
            <xsl:value-of select="@type"/>
            <xsl:value-of select="concat(',','')"/>
            <xsl:value-of select="@label"/>
            <xsl:value-of select="concat(',','&#xA;')"/>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

生成以下输出:

seriesUID   annotationType  coordSys    data    name    label

1006780多边形图像 ["[3476.0,1196.0,0.0]," [3436.0,1036.0,0.0],]
1006810多边形图像 ["[3064.0,744.0,0.0]," [3300.0,912.0,0.0],]

1006780 polygon Image ["[3476.0,1196.0,0.0],"[3436.0,1036.0,0.0],] Right
1006810 polygon Image ["[3064.0,744.0,0.0],"[3300.0,912.0,0.0],] Right

请帮助我消除 data 字段中的双引号和最后一个COMMA.

Please help me eliminate the double quote marks and the last COMMA in the field data.

推荐答案

如果您只能使用XSLT 2.0,那么这就是您可以做到的方式...

If you can only use XSLT 2.0, then this is how you could do it...

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="text" />

<xsl:template match="/">
    <xsl:text>type,data,label&#10;</xsl:text>
    <xsl:apply-templates select="//parent" />
</xsl:template>

<xsl:template match="parent">
    <xsl:value-of select="@type" />
    <xsl:text>,"[</xsl:text>
    <xsl:value-of select="child1/child2/concat('[',@x,',',@y,',',@z,']')" separator="," />
    <xsl:text>]",hard coded&#10;</xsl:text>
</xsl:template>
</xsl:stylesheet>

我想知道您是否确实在使用XSLT 2.0.除非将XcLT处理器支持在样式表中,否则只需将version="2.0"放在样式表中就不会有任何效果.

I am wondering if indeed you are using XSLT 2.0. Simply putting version="2.0" in a stylesheet won't achieve anything unless you have an XSLT processor that supports it.

在这种情况下,这是先前XSLT的XSLT 1.0版本.

If this is the case, here is an XSLT 1.0 version of the previous XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="text" />

<xsl:template match="/">
    <xsl:text>type,data,label&#10;</xsl:text>
    <xsl:apply-templates select="//parent" />
</xsl:template>

<xsl:template match="parent">
    <xsl:value-of select="@type" />
    <xsl:text>,"[</xsl:text>
    <xsl:for-each select="child1/child2">
        <xsl:if test="position() > 1">,</xsl:if>
        <xsl:value-of select="concat('[',@x,',',@y,',',@z,']')" />        
    </xsl:for-each>
    <xsl:text>]",hard coded&#10;</xsl:text>
</xsl:template>

</xsl:stylesheet>

您应该能够希望将其适应新的XML.

You should be able to adapt this to your new XML hopefully.

注意:要查看您实际拥有的XSLT处理器和版本,请参阅此问题...

Note: To see what XSLT processor and version you actually have, see this question...

查看全文

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