How to Split xml String when using XSLT1.0?

后端 未结 1 1583
眼角桃花
眼角桃花 2021-01-28 23:23

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:-

<         


        
相关标签:
1条回答
  • 2021-01-29 00:00

    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:

    0 讨论(0)
提交回复
热议问题