How to write a CSV-parser using XSLT 1.0?

烈酒焚心 提交于 2019-12-08 11:52:48

问题


I need to make a CSV-parser using XSLT 1.0. I have tried a recursive approach, but I can't seem to match the line endrings, so the input to the printLine-template is always empty.

<xsl:template match="/">
  <xsl:call-template name="printLine">
    <xsl:with-param name="line">
      <xsl:value-of select="substring-before(//csvText, '\n')"/>
    </xsl:with-param>
    <xsl:with-param name="remaining">
      <xsl:value-of select="substring-after(//csvText, '\n')"/>
    </xsl:with-param>
  </xsl:call-template>
</xsl:template>

<xsl:template name="printLine">
<xsl:param name="line"/>
<xsl:param name="remaining"/>
    <xsl:value-of select="$line"/><br />
    <xsl:if test="remaining != ''">
    <xsl:call-template name="printLine">
      <xsl:with-param name="line">
        <xsl:value-of select="substring-before($remaining, '\n')"/>
      </xsl:with-param>
      <xsl:with-param name="remaining">
        <xsl:value-of select="substring-after($remaining, '\n')"/>
      </xsl:with-param>
    </xsl:call-template>
    </xsl:if>
</xsl:template>

回答1:


I found a solution to this:

<xsl:variable name="ls"><xsl:text>
</xsl:text></xsl:variable>
<xsl:variable name="fs"><xsl:text>  </xsl:text></xsl:variable>

<xsl:template match="/">
  <xsl:call-template name="printLine">
    <xsl:with-param name="line" select="substring-before(//csvText, $ls)"/>
    <xsl:with-param name="remaining" select="substring-after(//csvText, $ls)"/>
  </xsl:call-template>
</xsl:template>

<xsl:template name="printLine">
<xsl:param name="line"/>
<xsl:param name="remaining"/>
    <xsl:call-template name="printFields">
        <xsl:with-param name="line" select="$line"/>
    </xsl:call-template>
    <xsl:if test="$remaining != ''">
    <xsl:call-template name="printLine">
      <xsl:with-param name="line" select="substring-before($remaining, $ls)"/>
      <xsl:with-param name="remaining" select="substring-after($remaining, $ls)"/>
    </xsl:call-template>
    </xsl:if>
</xsl:template>

<xsl:template name="printFields">
    <xsl:param name="line"/>
    <!-- render each line and access each field using the getFieldByIndex-template. Example: -->
    <div>
        <h3>
            <xsl:call-template name="getFieldByIndex">
                <xsl:with-param name="index" select="1"/>
                <xsl:with-param name="line" select="$line"/>
            </xsl:call-template>
        </h3>
        <p>
            <xsl:call-template name="getFieldByIndex">
                <xsl:with-param name="index" select="4"/>
                <xsl:with-param name="line" select="$line"/>
            </xsl:call-template>
        </p>
    </div>
</xsl:template>

<xsl:template name="getFieldByIndex">
    <xsl:param name="index"/>
    <xsl:param name="line"/>
    <xsl:choose>
        <xsl:when test="$index > 0">
            <xsl:call-template name="getFieldByIndex">
                <xsl:with-param name="index" select="$index -1"/>
                <xsl:with-param name="line" select="substring-after($line, $fs)"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="substring-before($line,$fs)"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

The line- and field separators are stored in the ls and fs variables. This example traverses a tab-serparated file. The printFields-template has to be customized for each use.



来源:https://stackoverflow.com/questions/8119779/how-to-write-a-csv-parser-using-xslt-1-0

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!