【问题标题】:xml multi level to csv using xslt使用 xslt 将 xml 多级转换为 csv
【发布时间】:2014-07-10 16:56:35
【问题描述】:

我有一个这样的 XML:

 <PROGRAM>
    <date>10/07/2014</date>
    <name>Name</name>
    <LOTS>
        <LOT>
            <ID>1</ID>
            <type>A</type>
        </LOT>
        <LOT>
            <ID>2</ID>
            <type>B</type>
        </LOT>
</PROGRAM>

我想从转换 (XSLT) 结果中得到这个:

2014 年 10 月 7 日,姓名,1,A
2014 年 10 月 7 日,姓名,2,B

这是我的 xslt:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="text"/>
    <xsl:variable name='newline'>
        <xsl:text></xsl:text>
    </xsl:variable>

<xsl:template match="/">
        <xsl:for-each select="//LOT">
            <xsl:for-each select="//PROGRAM">
                <xsl:value-of select="concat(date,',',name)"/>
            </xsl:for-each>
            <xsl:value-of select="concat(',',ID,',',type,$newline)"/>
        </xsl:for-each>
</xsl:template>

有谁知道 XSLT 可以实现这个目标吗?

谢谢!

【问题讨论】:

    标签: xml xslt csv parent-child


    【解决方案1】:

    来点简单的怎么样:

    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" encoding="UTF-8"/>
    
    <xsl:template match="/">
            <xsl:for-each select="PROGRAM/LOTS/LOT">
                <xsl:value-of select="concat(../../date, ',', ../../name, ',', ID, ',', type)"/>
                <xsl:if test="position()!=last()">
                    <xsl:text>&#10;</xsl:text>
                </xsl:if>
            </xsl:for-each>
    </xsl:template>
    
    </xsl:stylesheet>
    

    【讨论】:

    • @dali 如果您的问题得到解答,请通过接受答案来关闭它。
    猜你喜欢
    • 2018-03-11
    • 2019-02-02
    • 2012-04-04
    • 2017-05-23
    • 2011-10-28
    相关资源
    最近更新 更多