2016-07-28 60 views
0

我想使用XSL将XML文件转换为CSV格式。任何人都可以帮助我,我以前从未使用过XSL。这是我第一次。如何使用xsl转换嵌套的xml与复杂的命名空间?

XML文件:

<?xml version="1.0" encoding="UTF-8" ?> 
<databook:databook xmlns:xsi="http://www.ww2g.org/2001/XMLSchema-instance" xmlns:databook="http://www.hello.com/arc/hi/model/databook" xsi:type="databook:HelloModelType"> 
<databook:property> 
<version>1989</version> 
<name>Current</name> 
<fileName>Current</fileName> 
<fileExtension>xls</fileExtension> 
<description>Current Rates</description> 
<created>2016-07-17-01.26.39.677445</created> 
<databook:category id="1" name="Current"/> 
<databook:datasheetNames> 
<name>Current</name> 
<name>Time</name> 
</databook:datasheetNames> 
</databook:property> 
<databook:datasheet name="Current" isColumnBased="true"> 
<databook:columns> 
<databook:column> 
<databook:columnDefinition id="0" name="Gem" showName="Gem" type="String"/> 
<cell>Gem</cell> 
<cell>A1</cell> 
<cell>A2</cell> 
<cell>A3</cell> 
</databook:column> 
<databook:column> 
<databook:columnDefinition id="0" name="IGT" showName="IGT" type="String"/> 
<cell>IGT</cell> 
<cell>B1</cell> 
<cell>B2</cell> 
<cell>B3</cell> 
</databook:column> 
</databook:columns> 
</databook:datasheet> 
<databook:datasheet name="Time" isColumnBased="true"> 
<databook:columns> 
<databook:column> 
<databook:columnDefinition id="0" name="Hello" showName="Hello" type="String"/> 
<cell>Hello</cell> 
<cell>C1</cell> 
<cell>C2</cell> 
<cell>C3</cell> 
</databook:column> 
<databook:column> 
<databook:columnDefinition id="0" name="Hi" showName="Hi" type="String"/> 
<cell>Hi</cell> 
<cell>D1</cell> 
<cell>D2</cell> 
<cell>D3</cell> 
</databook:column> 
</databook:columns> 
</databook:datasheet> 
</databook:databook> 

XSL文件:

<?xml version="1.0"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL /Transform"  xmlns:fo="http://www.w3.org/1999/XSL/Format" > 
<xsl:output method="text" omit-xml-declaration="yes" indent="no"/> 
<xsl:template match="/datasheet"> 
<xsl:for-each select="//datasheet"> 
<xsl:value-of select="@name" /> 
<xsl:for-each select="//column"> 
<xsl:apply-templates select ="cell" /> 
</xsl:for-each> 
</xsl:for-each> 
</xsl:template> 
<xsl:template match="cell">   
<xsl:for-each select="//cell"> 
<xsl:value-of select="cell" /> 
</xsl:for-each> 
</xsl:template> 
</xsl:stylesheet> 
下面

预期输出:

sheetname=Current 
Gem,A1,A2,A3 
IGT,B1,B2,B3 
sheetname=Time 
Hello,C1,C2,C3 
Hi,D1,D2,D3 

欲迭代通张数,列数的每个片材内部然后每列下的单元格值为

回答

0

尝试这种方式:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:databook="http://www.hello.com/arc/hi/model/databook"> 
<xsl:output method="text" /> 

<xsl:template match="/databook:databook"> 
    <xsl:for-each select="databook:datasheet"> 
     <xsl:text>sheetname=</xsl:text> 
     <xsl:value-of select="@name" /> 
     <xsl:text>&#10;</xsl:text> 
     <xsl:for-each select=".//databook:column"> 
      <xsl:for-each select="cell"> 
       <xsl:value-of select="." /> 
       <xsl:if test="position()!=last()"> 
        <xsl:text>,</xsl:text> 
       </xsl:if> 
      </xsl:for-each> 
      <xsl:text>&#10;</xsl:text> 
     </xsl:for-each> 
    </xsl:for-each> 
</xsl:template> 

</xsl:stylesheet> 

注(1)中使用的前缀来解决在输入XML是命名空间中的,(2)的差的那些元件在//.//之间。

+0

非常感谢您的帮助:)我会试试这个。 – user2376404

+0

这段代码以前工作 sheetname = ' – user2376404

+0

@ user2376404不知道是什么你是这个意思。上述不适合你吗? –