2015-07-03 25 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