XSLT将未知的XML转换为CSV [英] XSLT to convert unknown XML to CSV

查看:70
本文介绍了XSLT将未知的XML转换为CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是XSLT的新手,一直在努力解决以下问题。如果有人可以帮助我解决这个问题,我将不胜感激。



这是我的XML文件,但是元素名称可能每次都不同。 XML是在运行时创建的。基本上,我不知道XML文件中的所有元素。可能有更多或更少的元素,但基本上具有以下结构:

 < University> 
< language> en< / language>
< name> Medi University< / name>
< location>罗马< / location>
< country>意大利< / country>
< member>
< teacher>
< name> John Sting< / name>
< joined>
< time>
< start />
< end />
< / time>
< valid> true< / valid>
< / joined>
< name> Paul Ironman< / name>
< joined>
< time>
< start />
< end />
< / time>
< valid> true< / valid>
< / joined>
< / teacher>
< teacherAssistant>
< name> Luna Tutti< / name>
< joined>
< time>
< start> 1.9.2015< / start>
< end>< / end>
< / time>
< valid> true< / valid>
< / joined>
< / teacherAssistant>
< / member>
< telephone> 7538476398754< / telephone>
< email> medi@medi.com< / email>
< / University>

我有这个XSLT文件试图对其进行转换。正如我所说的,XML文件是在运行时创建的,我不知道XML内容。

 < xsl:template match = /> 
< xsl:apply-templates />
< / xsl:template>

< xsl:template match = *>
< xsl:value-of select = name() />
< xsl:value-of select = text() />
< xsl:if test = *>
< xsl:apply-templates />
< / xsl:if>
< / xsl:template>
< / xsl:stylesheet>

上面的代码打印CSV文件,如下所示:

  elemenNameelementValue 
elemenName2elementValue2
elemenName3elementValue3

,依此类推。



我想要的是类似波纹管的东西:

 大学

语言,名称,位置,国家/地区,电话,电子邮件
英语,Medi,罗马,意大利,7538476398754,medi @ medi.com

Teacter

名称,已加入,时间,开始,结束,有效,
John Sting,,,,真实
Paul Ironman,,,,真实

Teacher Assistant

名称,联接,时间,开始,结束,有效,
Luna Tutti,,.1.9.2015,,真实

我希望相关元素像上面一样显示在一行上。



谢谢

解决方案

尝试这个真正通用的样式表

 <?xml版本= 1.0编码= utf-8?> 
< xsl:stylesheet version = 1.0 xmlns:xsl = http://www.w3.org/1999/XSL/Transform>
< xsl:output method = text omit-xml-declaration = yes indent = no />

< xsl:template match = node()>
< xsl:value-of select = name() />
< xsl:text>& #xA;< / xsl:text>
< xsl:call-template name = loop />
< / xsl:template>

< xsl:template name = loop>
<!-输出标头->
< xsl:for-each select = ./* [count(*)= 0]>
< xsl:value-of select = name() />
< xsl:if test = position()!= last()>
< xsl:text>,< / xsl:text>
< / xsl:if>
< / xsl:for-each>
< xsl:text>& #xA;< / xsl:text>

< ;!-输出值->
< xsl:for-each select = ./* [count(*)= 0]>
< xsl:value-of select =。 />
< xsl:if test = position()!= last()>
< xsl:text>,< / xsl:text>
< / xsl:if>
< / xsl:for-each>
< xsl:text>& #xA;< / xsl:text>

< ;!-具有子节点的处理节点->
< xsl:for-each select = ./* [count(*)!= 0]>
< xsl:value-of select = name() />
< xsl:text>& #xA;< / xsl:text>
< xsl:call-template name = loop />
< / xsl:for-each>
< / xsl:template>

< / xsl:stylesheet>如michael.hor257k所述,在没有某些约束的情况下,新表从具有子节点的每个节点开始。






请尝试更换

 <!-具有子节点的处理节点-> 
< xsl:for-each select = ./* [count(*)!= 0]>
< xsl:choose>
< xsl:when test = name()='teacher'>
< xsl:text>老师< / xsl:text>
< / xsl:when>
< xsl:when test = name()='teacherAssistant'>
< xsl:text>教师助手< / xsl:text>
< / xsl:when>
< xsl:otherwise>
< xsl:value-of select = name() />
< / xsl:otherwise>
< / xsl:choose>

< xsl:text>& #xA;< / xsl:text>
< xsl:call-template name = loop />
< / xsl:for-each>


I am new to XSLT and have been struggling with the following problem. I appreciate if anyone could help me how to go around this.

This is my XML file, but the element names could differ each time. The XML is created on the run. Basically I don't know all the elements in the XML file. There could be more or less elements, but basically has the following structure:

    <University>
    <language>en</language>
    <name>Medi University</name>
    <location>Rome</location>
    <country>Italy</country>
    <member>
        <teacher>
                <name>John Sting</name>
                <joined>
                    <time>
                    <start/>
                        <end/>
                     </time>
                    <valid>true</valid>
                </joined>
                <name>Paul Ironman</name>
                <joined>
                    <time>
                    <start/>
                        <end/>
                     </time>
                    <valid>true</valid>
                </joined>
        </teacher>
        <teacherAssistant>
               <name>Luna Tutti</name>
                <joined>
                    <time>
                         <start>1.9.2015</start>
                        <end></end>
                    </time>
                    <valid>true</valid>
                </joined>
        </teacherAssistant>
     </member>
    <telephone>7538476398754</telephone>
    <email>medi@medi.com</email>
</University>

I have this XSLT file that tries to transform that. As I said the XML file is created on run time and I don't know the XML content.

 <xsl:template match="/">
      <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="*">
   <xsl:value-of select="name()"/>
      <xsl:value-of select="text()"/>
      <xsl:if test="*">
          <xsl:apply-templates/>
      </xsl:if>
  </xsl:template>
</xsl:stylesheet>

The above code prints the CSV file like this:

elemenNameelementValue
elemenName2elementValue2
elemenName3elementValue3

and so on.

What I want is something like bellow:

University

language, name, location, country,telephone, email
english, Medi, Rome,Italy,7538476398754,medi@medi.com

Teacter

name, joined, time, start,end,valid,
John Sting, , , , ,true
Paul Ironman, , , , , true

Teacher Assistant

name, joined, time, start,end,valid,
Luna Tutti, , ,1.9.2015, , true 

I want related elements to appear on one line as in above.

Thanks

解决方案

Try this truly generic stylesheet

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" omit-xml-declaration="yes" indent="no"/>

    <xsl:template match="node()">
        <xsl:value-of select="name()"/>
        <xsl:text>&#xA;</xsl:text>
        <xsl:call-template name="loop"/>
    </xsl:template>

    <xsl:template name="loop">
        <!-- Output headers -->
        <xsl:for-each select="./*[count(*) = 0]">
            <xsl:value-of select="name()"/>
            <xsl:if test="position() != last()">
                <xsl:text>,</xsl:text>
            </xsl:if>
        </xsl:for-each>
        <xsl:text>&#xA;</xsl:text>

        <!-- Output values -->
        <xsl:for-each select="./*[count(*) = 0]">
            <xsl:value-of select="."/>
            <xsl:if test="position() != last()">
                <xsl:text>,</xsl:text>
            </xsl:if>
        </xsl:for-each>
        <xsl:text>&#xA;</xsl:text>

        <!-- Process nodes having childs -->
        <xsl:for-each select="./*[count(*) != 0]">
            <xsl:value-of select="name()"/>
            <xsl:text>&#xA;</xsl:text>
            <xsl:call-template name="loop"/>
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>

As mentioned by michael.hor257k, without some constraints, new table starts for each node having child nodes.


Try this for replacement

<!-- Process nodes having childs -->
<xsl:for-each select="./*[count(*) != 0]">
    <xsl:choose>
        <xsl:when test="name() = 'teacher'">
            <xsl:text>Teacher</xsl:text>
        </xsl:when>
        <xsl:when test="name() = 'teacherAssistant'">
            <xsl:text>Teacher Assistant</xsl:text>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="name()"/>
        </xsl:otherwise>
    </xsl:choose>

    <xsl:text>&#xA;</xsl:text>
    <xsl:call-template name="loop"/>
</xsl:for-each>

这篇关于XSLT将未知的XML转换为CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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