我是XSLT和XML的新手。我一直在通過教程和stackoverflow的例子,並學習了很多新的東西。XML到CSV應用XSLT(格式問題,邏輯錯誤)
當前我正在嘗試將XSLT應用於XML文件以將其轉換爲CSV。我想要實現的是,我在XML文件中提供了一些我想要提取的信息。 CSV文件默認會有一些信息,我將在XSLT中傳遞這些信息。
另外我也使用了基本的XSLT語法,我很快就學會了,我相信這是最糟糕的方式。我會很感激所有的幫助,如果有人能指導我如何使這個解決方案更好。是
我遇到的問題如下:
的信息沒有得到正確的格式,它得到印上一個新的生產線,這是我不想除非我指定一個新行。此外,如果我不縮進XSLT文件,那麼信息可以保持單一,但代碼看起來很亂,我相信有一些簡單的方法來做到這一點。
每個人都有一些許可證,我想在for-each循環內讀取許可證,但我很困惑如何做到這一點。就好像這個人沒有一些許可證,那麼默認情況下,我希望它是空白的。目前在這個例子中我使用了5個狀態。
這是我用來測試的xml文件。
<?xml version="1.0" encoding="UTF-8"?>
<People>
<Person>
<required-tag1>some-information</required-tag1>
<required-tag2>some-information</required-tag2>
<first-name>Mike</first-name>
<last-name>Hewitt</last-name>
<licenses>
<license>
<state xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">IL</state>
</license>
</licenses>
</Person>
<Person>
<required-tag1>some-information</required-tag1>
<required-tag2>some-information</required-tag2>
<first-name>John</first-name>
<last-name>Jhonny</last-name>
<licenses>
<license>
<state xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">NY</state>
</license>
</licenses>
</Person>
<Person>
<required-tag1>some-information</required-tag1>
<required-tag2>some-information</required-tag2>
<first-name>Xmen</first-name>
<last-name>Bat</last-name>
<licenses>
<license>
<state xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">AK</state>
</license>
<license>
<state xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">CA</state>
</license>
<license>
<state xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">IL</state>
</license>
<license>
<state xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">NY</state>
</license>
<license>
<state xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">TX</state>
</license>
</licenses>
</Person>
</People>
我寫的XSLT如下:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:csv="csv:csv" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output encoding="UTF-8" method="text"/>
<xsl:template match="/People">First Name,Last Name,AK,CA,IL,NY,TX,default value1,required-tag1,required-tag2,default value2,default value3
<xsl:for-each select="Person">
<xsl:value-of select="first-name"/>, <xsl:value-of select="last-name"/>,<xsl:choose>
<xsl:when test="licenses/license/state='AK'">
<xsl:value-of select="licenses/license/state"/>,
</xsl:when>
<xsl:otherwise>NA,
</xsl:otherwise></xsl:choose><xsl:choose>
<xsl:when test="licenses/license/state='CA'">
<xsl:value-of select="licenses/license/state"/>,
</xsl:when>
<xsl:otherwise>NA,
</xsl:otherwise></xsl:choose><xsl:choose>
<xsl:when test="licenses/license/state='IL'">
<xsl:value-of select="licenses/license/state"/>,
</xsl:when>
<xsl:otherwise>NA,
</xsl:otherwise></xsl:choose><xsl:choose>
<xsl:when test="licenses/license/state='NY'">
<xsl:value-of select="licenses/license/state"/>,
</xsl:when>
<xsl:otherwise>NA,
</xsl:otherwise></xsl:choose><xsl:choose>
<xsl:when test="licenses/license/state='TX'">
<xsl:value-of select="licenses/license/state"/>,
</xsl:when>
<xsl:otherwise>NA,
</xsl:otherwise></xsl:choose>DEFAULT VALUE1,<xsl:value-of select="required-tag1"/>,<xsl:value-of select="required-tag2"/>,DEFAULT VALUE2,DEFAULT VALUE3<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
我嘗試使用的for-each中的for-each循環牌照但執行5次對所有許可證,如何循環執行條件以測試許可證狀態5次並根據默認保持格式正確。
這是我想輸出是:
First Name,Last Name,AK,CA,IL,NY,TX,default value1,required-tag1,required-tag2,default value2,default value3
Mike, Hewitt,NA,NA,IL,NA,NA,DEFAULT VALUE1,some-information,some-information,DEFAULT VALUE2,DEFAULT VALUE3
John, Jhonny,NA,NA,NA,NY,NA,DEFAULT VALUE1,some-information,some-information,DEFAULT VALUE2,DEFAULT VALUE3
Xmen, Bat,AK,CA,IL,NY,TX,DEFAULT VALUE1,some-information,some-information,DEFAULT VALUE2,DEFAULT VALUE3
我使用XSLT 1.0版本。
謝謝你的幫助。
作爲參考,類似的問題已被問到這裏:http://stackoverflow.com/questions/365312/xml-to-csv-using-xslt你可能會發現那裏幾個有用的想法。 – Tomalak 2014-09-30 20:52:25