XSL if help needed please

前端 未结 1 596
长发绾君心
长发绾君心 2021-01-28 15:28

i am converting an html form to xml sequence, i am using a recursive function to achieve this

so the input to param \"list\" will be of the form

name=va         


        
相关标签:
1条回答
  • 2021-01-28 15:51

    This stylesheet:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:variable name="SETTINGS" select="/settings"/>
        <xsl:template match="/">
            <xsl:call-template name="tokenize">
                <xsl:with-param name="pString"
                     select="'color=blue&amp;name=value&amp;size=big'"/>
            </xsl:call-template>
        </xsl:template>
        <xsl:template name="tokenize">
            <xsl:param name="pString"/>
            <xsl:param name="pSeperator" select="'&amp;'"/>
            <xsl:choose>
                <xsl:when test="not($pString)"/>
                <xsl:when test="contains($pString,$pSeperator)">
                    <xsl:call-template name="tokenize">
                        <xsl:with-param name="pString"
                             select="substring-before($pString, $pSeperator)"/>
                        <xsl:with-param name="pSeperator" select="$pSeperator"/>
                    </xsl:call-template>
                    <xsl:call-template name="tokenize">
                        <xsl:with-param name="pString"
                             select="substring-after($pString, $pSeperator)"/>
                        <xsl:with-param name="pSeperator" select="$pSeperator"/>
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:variable name="vName"
                         select="normalize-space(substring-before($pString,'='))"/>
                    <xsl:variable name="vValue"
                         select="normalize-space(substring-after($pString,'='))"/>
                    <xsl:if test="$vName and $vValue">
                        <xsl:element name="{$vName}">
                            <xsl:if test="$vName = $SETTINGS/google/option/@from">
                                <xsl:attribute name="attr">special</xsl:attribute>
                            </xsl:if>
                            <xsl:value-of select="$vValue"/>
                        </xsl:element>
                    </xsl:if>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:template>
    </xsl:stylesheet>
    

    With this input:

    <settings>
        <google>
            <option from="color"/>
            <option from="size"/>
        </google>
    </settings>
    

    Output:

    <color attr="special">blue</color>
    <name>value</name>
    <size attr="special">big</size>
    

    Note: Node set comparison

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