Create combo box in xslt

2019-03-03 12:31发布

问题:

I have this value from xml and i have to create a combo box using xslt 1.0

this is the xml i get from database :

<CER_Pot>
  <Record CIMtrek_CERPot="Bus Dev|Ser Del|Sol Del|?" />
</CER_Pot>

and this is how i create combo box in xslt :

<select size="1" style="width:60%;" name="CIMtrek_CI_CER_Pot"
                                                id="CIMtrek_CI_CER_Pot">
                                                <option value="0">Select Fund Pot</option>
                                                <xsl:for-each select="//CER_Pot/Record">
                                                        <option>
                                                            <xsl:if
                                                                test="//Record/CIMtrek_CERPot/text()=@CIMtrek_CI_CER_Pot">
                                                                <xsl:attribute name="selected">true</xsl:attribute>
                                                            </xsl:if>
                                                            <xsl:attribute name="value"><xsl:value-of
                                                                select="@CIMtrek_CERPot" /></xsl:attribute>
                                                        <xsl:value-of select="@CIMtrek_CERPot" />
                                                        </option>
                                                    </xsl:for-each>
                                            </select>

This gives me the combo box without any issue but i would like to have a combo box which will have the values splited based this | delimiter so in this case it would be four rows

Bus Dev
Ser Del
Sol Del
?

How to do this in xslt

Please help me to get his done.

Best Regards

回答1:

Try this XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html"/>

    <xsl:template match="/">
        <select size="1" style="width:60%;" name="CIMtrek_CI_CER_Pot" id="CIMtrek_CI_CER_Pot">
            <option value="0">Select Fund Pot</option>
            <xsl:for-each select="//CER_Pot/Record">
                <xsl:variable name="selectValues">
                    <xsl:call-template name="tokenize">
                        <xsl:with-param name="pText" select="@CIMtrek_CERPot"/>
                        <xsl:with-param name="pDelim" select="'|'" />
                    </xsl:call-template> 
                </xsl:variable>

                <xsl:for-each select="$selectValues/item">
                    <xsl:variable name="curItem" select="." />
                    <option>
                        <xsl:if test="//Record/CIMtrek_CERPot/text()=$curItem">
                            <xsl:attribute name="selected">true</xsl:attribute>
                        </xsl:if>
                        <xsl:attribute name="value"><xsl:value-of select="$curItem"/></xsl:attribute>

                        <xsl:value-of select="$curItem"/>
                    </option>
                </xsl:for-each>
            </xsl:for-each>
        </select>
    </xsl:template>

    <xsl:template name="tokenize">
        <xsl:param name="pText"/>
        <xsl:param name="pDelim"/>

        <xsl:if test="string-length($pText)">
            <item>
                <xsl:value-of select="substring-before($pText, $pDelim)"/>
            </item>

            <xsl:call-template name="tokenize">
                <xsl:with-param name="pText" select="substring-after($pText, $pDelim)"/>
                <xsl:with-param name="pDelim" select="$pDelim" />
            </xsl:call-template>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>