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
欲迭代通張數,列數的每個片材內部然後每列下的單元格值爲
非常感謝您的幫助:)我會試試這個。 – user2376404
這段代碼以前工作 sheetname = ' –
user2376404
@ user2376404不知道是什麼你是這個意思。上述不適合你嗎? –