简体   繁体   中英

How to Split xml String when using XSLT1.0?

This is my Xml file I want to split month as different,year as different and 000/xxx as different How can we do that in XSLT1.0??

MY xml Code:-

<COMBINED-PAYMENT-HISTORY>January:2019,000/XXX|February:2018,000/XXX|
  </COMBINED-PAYMENT-HISTORY>

Result Expected:-

      <table>
    <tr>
        <td>January</td>

    </tr>
    <tr>
        <td class="AccValue1">2019</td>
        <td class="AccValue1">000/XXX</td>

    </tr>
    <tr>
        <td>February</td>

    </tr>
    <tr>
        <td class="AccValue1">2018</td>
        <td class="AccValue1">000/XXX</td>

    </tr>
</table>

Here is how you can parse the input to an HTML table. Note that this could be simpler if each item were a row instead of a column.

XSLT 1.0 (+EXSLT node-set)

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="COMBINED-PAYMENT-HISTORY">
    <xsl:variable name="items-rtf">
        <xsl:call-template name="tokenize">
            <xsl:with-param name="text" select="normalize-space(.)"/>
        </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="items" select="exsl:node-set($items-rtf)/item" />
    <table>
        <!-- month -->
        <tr>
            <xsl:for-each select="$items">
                <td>
                    <xsl:value-of select="substring-before(., ':')" />
                </td>
            </xsl:for-each>
        </tr>
        <!-- year -->
        <tr>
            <xsl:for-each select="$items">
                <td>
                    <xsl:value-of select="substring-before(substring-after(., ':'), ',')" />
                </td>
            </xsl:for-each>
        </tr>
        <!-- data -->
        <tr>
            <xsl:for-each select="$items">
                <td>
                    <xsl:value-of select="substring-after(., ',')" />
                </td>
            </xsl:for-each>
        </tr>
    </table>    
</xsl:template>

<xsl:template name="tokenize">
    <xsl:param name="text"/>
    <xsl:param name="delimiter" select="'|'"/>
        <xsl:variable name="token" select="substring-before($text, $delimiter)" />
        <item>
            <xsl:value-of select="$token"/>
        </item>
        <xsl:variable name="next" select="substring-after($text, $delimiter)"/>
        <xsl:if test="$next">
            <!-- recursive call -->
            <xsl:call-template name="tokenize">
                <xsl:with-param name="text" select="$next"/>
            </xsl:call-template>
        </xsl:if>
</xsl:template>

</xsl:stylesheet>

Result

<?xml version="1.0" encoding="UTF-8"?>
<table>
  <tr>
    <td>Mar</td>
    <td> Mar</td>
  </tr>
  <tr>
    <td>2019</td>
    <td>2018</td>
  </tr>
  <tr>
    <td>000/XXX</td>
    <td>000/XXX</td>
  </tr>
</table>

Rendered:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM