xsl-list
[Top] [All Lists]

RE: xsl split string

2005-12-11 14:36:01
If found a solution but I'm not 100% satisfied. My solution uses a template to 
split the "fields" attribute. The problem is that the template is called a lot 
of times and if I have many <update> nodes then it will take a lot of time. Is 
there a nicer solution?

Here's my stylesheet :

        <xsl:template match="/root">
                <page>
                        <xsl:apply-templates select="*"/>
                </page>
        </xsl:template>

        <xsl:template match="update">
                <sql:execute-query 
xmlns:sql="http://apache.org/cocoon/SQL/2.0";> 
                        <sql:query>
                                UPDATE <xsl:value-of select="$table"/>
                                SET <xsl:apply-templates 
select="@*[not(name()='xk' or name()='xi')]" mode="update"/>
                                WHERE <xsl:apply-templates select="@xk" 
mode="update"/>
                        </sql:query>
                </sql:execute-query>
        </xsl:template> 
        
        <xsl:template match="@xk" mode="update">`UID` = '<xsl:value-of 
select="."/>'</xsl:template>     
        
        <xsl:template match="@*" mode="update">`<xsl:call-template name="split">
                        <xsl:with-param name="string" select="/root/@fields"/>
                        <xsl:with-param name="position" select="position()"/>
                </xsl:call-template>` = <xsl:call-template name="stringOrNULL">
                        <xsl:with-param name="string" select="."/>
                </xsl:call-template> <xsl:if 
test="not(position()=last())">,</xsl:if>
        </xsl:template>
        
        <xsl:template name="split">
                <xsl:param name="string"/>
        <xsl:param name="position"/>
        <xsl:choose>
                        <xsl:when test="$position=1">
                                <xsl:if test="contains($string, 
'|')"><xsl:value-of select="substring-before($string,'|')"/></xsl:if>
                                <xsl:if test="not(contains($string, 
'|'))"><xsl:value-of select="$string"/></xsl:if>
                        </xsl:when>
                        <xsl:otherwise>
                                <xsl:call-template name="split">
                                        <xsl:with-param name="string" 
select="substring-after($string,'|')"/>
                                        <xsl:with-param name="position" 
select="$position - 1"/>
                                </xsl:call-template>
                        </xsl:otherwise>
                </xsl:choose>
        </xsl:template>

        <xsl:template name="stringOrNULL">
                <xsl:param name="string"/>
                <xsl:if test="$string=''">NULL</xsl:if>
                <xsl:if test="not($string='')">'<xsl:value-of 
select="$string"/>'</xsl:if>
        </xsl:template>

Philippe


-----Message d'origine-----
De : Philippe LAPLANCHE 
Envoyé : dimanche 11 décembre 2005 21:31
À : xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Objet : [xsl] xsl split string 

Hi

I have to transform this "compressed" xml :

<root fields="CHP-CODE|CHP-TABLE|CHP-LIBELLE">
         <update xi="5" xk="5" a="CHP-ORDRE" b="champs" c="Ordre"/>
</root>


Into this : 

<page>
        <sql:execute-query xmlns:sql="http://apache.org/cocoon/SQL/2.0";>
                <sql:querry>
                        UPDATE `config`
                                SET `CHP-CODE`='CHP-ORDRE',
                                    `CHP-TABLE`='champs',
                                    `CHP-LIBELLE`='Ordre'
                                WHERE UID='5'
                </sql:querry>
        </sql:execute-query>
</page>

My problem is that I don't know how to extract CHP-CODE, CHP-TABLE and 
CHP-LIBELLE from the fields attribute of the root node. I have to do something 
generic that would work if the fields attribute is longer.

For now I have this stylesheet. I just don't know how to complete the part 
where I put ......

        <xsl:template match="/root">
                <page>
                        <xsl:apply-templates select="*"/>
                </page>
        </xsl:template>

        <xsl:template match="update">
                <sql:execute-query 
xmlns:sql="http://apache.org/cocoon/SQL/2.0";> 
                        <sql:query>
                                UPDATE `config`
                                SET <xsl:apply-templates 
select="@*[not(name()='xk' or name()='xi')]" mode="update"/>
                                WHERE <xsl:apply-templates select="@xk" 
mode="update"/>
                        </sql:query>
                </sql:execute-query>
        </xsl:template> 
        
        <xsl:template match="@*" mode="update">
                `..........` = '<xsl:value-of select="."/>' <xsl:if 
test="not(position()=last())">,</xsl:if>
        </xsl:template>


Philippe



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

---------------------------------------------------------------------------------------
Wanadoo vous informe que cet  e-mail a ete controle par l'anti-virus mail. 
Aucun virus connu a ce jour par nos services n'a ete detecte.







--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



<Prev in Thread] Current Thread [Next in Thread>