XML到CSV应用XSLT(格式化问题,逻辑错误)

时间:2014-09-30 19:34:49

标签: xml csv xslt xslt-1.0

我是XSLT和XML的新手。我一直在阅读教程和stackoverflow示例,并学习了很多新东西。

目前我正在尝试在XML文件上应用XSLT以将其转换为CSV。我想要实现的是,我在XML文件中有一些信息,我想提取。默认情况下,CSV文件将包含一些信息,我将在XSLT中传递这些信息。

我也使用了快速学习的基本XSLT语法,我相信这是最糟糕的方法。如果有人能指导我如何更好地解决这个问题,我将非常感谢所有的帮助。

我遇到的问题如下:

  1. 信息没有正确格式化,它被打印在一个新行上,除非我指定新行,否则我不想要这样。此外,如果我不缩进XSLT文件,那么信息可以保持单一,但代码看起来很乱,我相信有一些简单的方法可以做到这一点。

  2. 每个人都会有一些许可证,我想在for-each循环中阅读,但我很困惑如何做到这一点。好像这个人没有一些许可证,那么默认情况下我希望它是空白的。目前在这个例子中我使用了5个州。

  3. 这是我用来测试的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>&#xa;</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版。

    谢谢你的帮助。

1 个答案:

答案 0 :(得分:3)

对于新行的问题,请考虑当前如何编写NA作为示例

<xsl:otherwise>NA,
</xsl:otherwise>

现在,如果你有这个......

<xsl:otherwise>
</xsl:otherwise>

然后XSLT根本不会输出新行。 xsl:otherwise下有一个文本节点,但它只是一个空白节点,因此会被忽略。但是一旦你向它添加普通文本,比如NA,那么XSLT会认为所有文本都很重要,并输出它包括换行符。它没有标准化任何空格。

解决方案是将所有此类文本包装在xsl:text

<xsl:otherwise>
   <xsl:text>NA</xsl:text>
</xsl:otherwise>

至于您的逻辑问题,您目前正在执行此操作以检查状态

<xsl:when test="licenses/license/state='IL'">
  <xsl:value-of select="licenses/license/state"/>,
</xsl:when>

但在每个陈述中使用licenses/license/state之间存在细微差别。在xsl:when你要问的是#34;是否有这种状态的许可证&#34;,但是在你的xsl:value-of中,它将获得列表中的第一个状态!

现在,你可以写这个

<xsl:when test="licenses/license/state='IL'">
  <xsl:value-of select="licenses/license[state='IL']/state"/>
  <xsl:text>,</xsl:text>
</xsl:when>

但这是多余的。最好将其简化为

<xsl:when test="licenses/license/state='IL'">
  <xsl:text>IL,</xsl:text>
</xsl:when>

值得一提的是,您显然有很多重复的代码,因此您可以使用命名模板删除其中的一部分。

试试这个XSLT作为例子

<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">
  <xsl:text>First Name,Last Name,AK,CA,IL,NY,TX,default value1,required-tag1,required-tag2,default value2,default value3</xsl:text>
  <xsl:text>&#xa;</xsl:text>
    <xsl:for-each select="Person">
        <xsl:value-of select="first-name"/>
        <xsl:text>, </xsl:text>
        <xsl:value-of select="last-name"/>
        <xsl:text>, </xsl:text>
        <xsl:call-template name="state">
          <xsl:with-param name="stateCode" select="'AK'" />
        </xsl:call-template>
      <xsl:call-template name="state">
          <xsl:with-param name="stateCode" select="'CA'" />
        </xsl:call-template>
       <xsl:call-template name="state">
          <xsl:with-param name="stateCode" select="'IL'" />
        </xsl:call-template>
      <xsl:call-template name="state">
          <xsl:with-param name="stateCode" select="'NY'" />
        </xsl:call-template>
        <xsl:call-template name="state">
          <xsl:with-param name="stateCode" select="'TX'" />
        </xsl:call-template>
        <xsl:text>DEFAULT VALUE1,</xsl:text>
        <xsl:value-of select="required-tag1"/>
        <xsl:text>,</xsl:text>
        <xsl:value-of select="required-tag2"/>
        <xsl:text>,DEFAULT VALUE2,DEFAULT VALUE3</xsl:text>
        <xsl:text>&#xa;</xsl:text>
    </xsl:for-each>
</xsl:template>

<xsl:template name="state">
  <xsl:param name="stateCode" />
  <xsl:choose>
      <xsl:when test="licenses/license[state=$stateCode]">
        <xsl:value-of select="$stateCode"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text>NA</xsl:text>
      </xsl:otherwise>
  </xsl:choose>
  <xsl:text>,</xsl:text>
</xsl:template>
</xsl:stylesheet>

重复使用xsl:call-template,这仍然是非常重复的。如果你只想要5个固定状态,你可以将状态放在外部XML文档中,并作为示例循环遍历。那可能是另一个问题.....

相关问题