XSL if help needed please

2019-03-05 05:11发布

问题:

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=value&name=value&name=value

The template below does this fine and returns an xml sequence as follows

<name>value</name><name>value</name><name>value</name>

Ok so the problem some of the name value pairs are special and i would like to add a attribute to them so the output would be

<name>value</name><name attr="special">value</name><name>value</name>

so to do this i have an external xml file with a list of special names as follows

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

So if we assume i have a xsl variable $SETTINGS connected to this external document above

<xsl:for-each select="$SETTINGS/google/option"></xsl:for-each>

Should be node with 2 children 1 called color and the other size

wot i want to do is if 1 of these children names = $name add the attribute

something like <xsl:if test="$name = $SETTINGS/google/option/ckild name">

<xsl:template name="tokenize">

        <xsl:param name="list"/> 

        <xsl:variable name="seperator" select="'&amp;'"/>

        <xsl:variable name="first" select="substring-before(concat($list, $seperator), $seperator)"/>
        <xsl:variable name="butfirst" select="substring-after($list, $seperator)"/>

        <xsl:variable name="name" select="normalize-space(substring-before($first, '='))"/>
        <xsl:variable name="value" select="normalize-space(substring-after($first, '='))"/>

        <xsl:if test="string-length($name)>0 and string-length($value)>0">
            <xsl:element name="{$name}">
                <xsl:for-each select="$SETTINGS/google/option">
                        ----->   <xsl:if test="$name = $SETTINGS/google/option/ckild name">
                         <xsl:attribute name="option"/>
                                 </xsl:if>
                </xsl:for-each>
                <xsl:value-of select="$value"/>
            </xsl:element>
        </xsl:if>

        <xsl:if test="$butfirst">
            <xsl:call-template name="tokenize">
                <xsl:with-param name="list" select="$butfirst"/> 
                <xsl:with-param name="seperator" select="$seperator"/> 
            </xsl:call-template>
        </xsl:if>

    </xsl:template>

Many Thanks Tim Dodgson

回答1:

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



标签: xslt