2011-08-16 42 views
0

我是新来的XSLT,并通过一些早期线索都走在SO Thread i Followed使用XSLT

我的要求是相似的,我需要XML转换为CSV,对于如我有以下XML将XML转换为CSV

<?xml version="1.0" encoding="UTF-8"?> 
<impex> 
<record> 
<Employee/> 
<UID>aa</UID> 
<Name>HR Manager</Name> 
<Groups/> 
<Password>123456</Password> 
</record> 
<record> 
<Employee/> 
<UID>bb</UID> 
<Name>HR Executive</Name> 
<Groups/> 
<Password>123456</Password> 
</record> 
</impex> 

,我需要这个XML转换为以下CSV输出

INSERT_UPDATE Employee;UID[unique=true];name;groups(uid);password 
;"aa";"HR Manager";;"123456" 
;"bb";"HR Executive";;"123456" 

在那里我有管理CSV头dynamicall Y(基于XML元素)

additonaly我也如果某些值缺少照顾我可以提供他们 例如 缺失或其在这种情况下空我需要提供一些默认值所生成的CSV因为这CSV将是其将被导入到系统

任何开始帮助下,我可以往前走将非常感激

+0

我对xslt并不熟悉,所以我很吃惊如何创建标题,因为我目前看到的标题只有值 –

+1

我建议您阅读W3CSchools XSLT教程。他们非常好,你可能会学会在一小时内解决你的问题。http://www.w3.org/TR/xslt – ColinE

+0

肯定会这样做..但任何帮助也将不胜感激,我知道我是问很多,但这是阻碍我的工作:) –

回答

2

不是最漂亮的最终输出,但它的工作原理

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

<xsl:output method="text" /> 

<xsl:template match="/impex"> 
    <xsl:text>INSERT_UPDATE </xsl:text> 
    <xsl:apply-templates select="record[1]/*" mode="header"/> 
    <xsl:apply-templates select="record" /> 
</xsl:template> 


<xsl:template match="*" mode="header" > 
    <xsl:value-of select="name()"/> 
    <xsl:choose> 
     <xsl:when test="position()=last()"> 
      <xsl:text>&#10;</xsl:text> 
     </xsl:when> 
     <xsl:otherwise>;</xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 

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

<xsl:template match="*" > 
    <xsl:value-of select="."/> 
    <xsl:choose> 
     <xsl:when test="position()=last()"> 
      <xsl:text>&#10;</xsl:text> 
     </xsl:when> 
     <xsl:otherwise>;</xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 
</xsl:stylesheet> 
+0

感谢Per for this ..虽然我也必须学习xslt我自己,我这样做,但它可以帮助我开始一点点 –