2015-07-03 27 views
0

我的XML输入像下面如何转换XML数据导入CSV格式XSLT

<?xml version="1.0" encoding="UTF-8" ?> 
<EmployeeRequest xmlns="http://www.example.org" 
    <EmployeeDetails> 
    <FirstName>Khan</FirstName> 
    <LastName>Joshi</LastName> 
    <Age>30</Age> 
    </EmployeeDetails> 
    <EmployeeDetails> 
    <FirstName>Josh</FirstName> 
    <LastName>Luis</LastName> 
    <Age>29</Age> 
    </EmployeeDetails> 
</EmployeeRequest> 

但我的输出应该来像下面。

FirstName, LastName, Age 
Khan, joshi,30 
Josh,Luis,29 

这里的EmployeeDetails是Unbound.Using模板如何解决这个问题?

请让我知道如何得到这个输出

+0

** 1 **请选择XSLT 1.0 ** **或XSLT 2.0 - 不能同时使用。 - ** 2。**请将您的预期输出**作为代码**发布。 - ** 3。**您尝试过什么?向我们展示您尝试的XSLT,以便我们不必从头开始。 –

+0

使用模板如何解决XSLT中的上述需求 –

回答

0

路径//*[not(*)]给你所有的叶元素(如你似乎不感兴趣根EmployeeRequest和容器EmployeeDetails),然后<xsl:value-of select="//*[not(*)]/name()" separator=", "/>将输出的名。这假设一个XSLT 2.0处理器。

如果你想保留它通用的,你可以使用

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

<xsl:output method="text"/> 

<xsl:template match="/"> 
    <xsl:value-of select="/*/*[1]/*/local-name()" separator=", "/> 
    <xsl:text>&#10;</xsl:text> 
    <xsl:apply-templates select="/*/*"/> 
</xsl:template> 

<xsl:template match="*/*"> 
    <xsl:if test="position() > 1"> 
     <xsl:text>&#10;</xsl:text> 
    </xsl:if> 
    <xsl:value-of select="*" separator=", "/> 
</xsl:template> 

</xsl:transform> 

看到http://xsltransform.net/gWmuiK1为例。 。

+0

使用Template给我举一个例子。因为我的EmployeeDetails元素无界 –

+0

HI Martin对此 –

+0

的任何更新我已经使用模板添加了一个示例。 –

0

这是一个纯粹XSLT 1.0溶液:

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

<xsl:output method="text"/> 

<xsl:template match="/"> 
    <xsl:apply-templates select="//e:EmployeeDetails[1]" mode="heading"/> 
    <xsl:apply-templates select="//e:EmployeeDetails"/> 
</xsl:template> 

<xsl:template match="e:EmployeeDetails" mode="heading"> 
    <xsl:for-each select="*"> 
     <xsl:value-of select="local-name()"/> 
     <xsl:call-template name="comma"/> 
    </xsl:for-each> 
    <xsl:text>&#10;</xsl:text> 
</xsl:template> 

<xsl:template match="e:EmployeeDetails"> 
    <xsl:for-each select="*"> 
     <xsl:value-of select="text()"/> 
     <xsl:call-template name="comma"/> 
    </xsl:for-each> 
    <xsl:text>&#10;</xsl:text> 
</xsl:template> 

<xsl:template name="comma"> 
    <xsl:if test="position() != last()">,</xsl:if> 
</xsl:template> 

</xsl:stylesheet> 
+0

谢谢乔。它的工作。 –

+0

不要忘记接受答案;) – Joe