Add comma as delimiter when creating array of arrays as one field in csv from xml data using xslt

眉间皱痕 提交于 2019-12-13 05:07:56

问题


My actual data is as follows :

<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>

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

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>

Generating below output :

seriesUID   annotationType  coordSys    data    name    label

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

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


回答1:


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>

EDIT: 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.

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>

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

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

How can I check which XSLT processor is being used in Solr?



来源:https://stackoverflow.com/questions/50593998/add-comma-as-delimiter-when-creating-array-of-arrays-as-one-field-in-csv-from-xm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!