XSL : spliting string in an un identified number of sub-strings

Discussion in 'XML' started by Sebek, Apr 5, 2004.

  1. Sebek

    Sebek Guest

    Hello,
    I'm transforming a XML document in XHTML but I have problems using
    sub-strings, it will be clearer with an exemple:

    What I have:
    <form href="identification.php?PHPSESSID=134134&page=2&param=3" >
    </form>


    what I want:
    <form action="identification.php" >
    <input name="PHPSESSID" value="134134"/>
    <input name="page" value="2"/>
    <input name="param" value="3"/>
    </form>

    My main problem is I don't know how many parameters will
    identification.php have. I should use <xsl: for-each> but how can I
    split a string in an non-known number of substrings?

    Any help would be appreciated.

    PS: I'm using Sablotron with PHP

    Sebek - Sebek <at/> caramail <dot/> com
     
    Sebek, Apr 5, 2004
    #1
    1. Advertising

  2. Sebek wrote:

    > I'm transforming a XML document in XHTML but I have problems using
    > sub-strings, it will be clearer with an exemple:
    >
    > What I have:
    > <form href="identification.php?PHPSESSID=134134&page=2&param=3" >
    > </form>
    >
    >
    > what I want:
    > <form action="identification.php" >
    > <input name="PHPSESSID" value="134134"/>
    > <input name="page" value="2"/>
    > <input name="param" value="3"/>
    > </form>
    >
    > My main problem is I don't know how many parameters will
    > identification.php have. I should use <xsl: for-each> but how can I
    > split a string in an non-known number of substrings?
    >
    > Any help would be appreciated.
    >
    > PS: I'm using Sablotron with PHP


    You can write recursive templates in XSLT which do such processsing,
    with the XML input being

    <form href="identification.php?PHPSESSID=134134&amp;page=2&amp;param=3">
    </form>

    and the stylesheet being

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

    <xsl:template match="form">
    <xsl:copy>
    <xsl:apply-templates select="@*" />
    <xsl:apply-templates select="node()" />
    </xsl:copy>
    </xsl:template>

    <xsl:template match="@href">
    <xsl:variable name="action" select="substring-before(., '?')" />
    <xsl:variable name="queryString" select="substring-after(., '?')" />
    <xsl:attribute name="action"><xsl:value-of select="$action"
    /></xsl:attribute>
    <xsl:call-template name="argsToInputs">
    <xsl:with-param name="queryString" select="$queryString" />
    </xsl:call-template>
    </xsl:template>

    <xsl:template name="argsToInputs">
    <xsl:param name="queryString" />
    <xsl:variable name="firstArg">
    <xsl:choose>
    <xsl:when test="contains($queryString, '&amp;')">
    <xsl:value-of select="string(substring-before($queryString,
    '&amp;'))" />
    </xsl:when>
    <xsl:eek:therwise><xsl:value-of select="string($queryString)"
    /></xsl:eek:therwise>
    </xsl:choose>
    </xsl:variable>
    <xsl:variable name="remainingQueryString"
    select="substring-after($queryString, '&amp;')" />
    <xsl:if test="$firstArg != ''">
    <input name="{substring-before($firstArg, '=')}"
    value="{substring-after($firstArg, '=')}" />
    <xsl:call-template name="argsToInputs">
    <xsl:with-param name="queryString" select="$remainingQueryString" />
    </xsl:call-template>
    </xsl:if>
    </xsl:template>

    <xsl:template match="/">
    <xsl:copy>
    <xsl:apply-templates />
    </xsl:copy>
    </xsl:template>

    </xsl:stylesheet>

    the output is

    <form action="identification.php"><input value="134134"
    name="PHPSESSID"/><input value="2" name="page"/><input value="3"
    name="param"/>
    </form>
    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Apr 5, 2004
    #2
    1. Advertising

  3. Re: spliting string in an un identified number of sub-strings

    "Sebek" <> wrote in message
    news:...
    > Hello,
    > I'm transforming a XML document in XHTML but I have problems using
    > sub-strings, it will be clearer with an exemple:
    >
    > What I have:
    > <form href="identification.php?PHPSESSID=134134&page=2&param=3" >
    > </form>
    >
    >
    > what I want:
    > <form action="identification.php" >
    > <input name="PHPSESSID" value="134134"/>
    > <input name="page" value="2"/>
    > <input name="param" value="3"/>
    > </form>
    >
    > My main problem is I don't know how many parameters will
    > identification.php have. I should use <xsl: for-each> but how can I
    > split a string in an non-known number of substrings?
    >
    > Any help would be appreciated.
    >
    > PS: I'm using Sablotron with PHP
    >
    > Sebek - Sebek <at/> caramail <dot/> com


    First of all, please, do note that the source xml document you provided is
    not a well-formed xml document. Unescaped ampersand in character data is
    illegal in XML. The corrected xml document is:

    <form href="identification.php?PHPSESSID=134134&amp;page=2&amp;param=3" >
    </form>

    The transformation you want is most easily implemented using the
    "str-split-to-words" template from FXSL.

    This transformation:

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:ext="http://exslt.org/common"
    exclude-result-prefixes="ext"
    >


    <xsl:import href="strSplit-to-Words.xsl"/>

    <xsl:eek:utput indent="yes" omit-xml-declaration="yes"/>

    <xsl:template match="/">
    <xsl:variable name="vwordParams">
    <xsl:call-template name="str-split-to-words">
    <xsl:with-param name="pStr" select="/*/@href"/>
    <xsl:with-param name="pDelimiters"
    select="'&amp;?'"/>
    </xsl:call-template>
    </xsl:variable>

    <xsl:variable name="vParams"
    select="ext:node-set($vwordParams)/*"/>
    <form action="{$vParams[1]}">
    <xsl:for-each select="$vParams[position()>1]">
    <input name="{substring-before(.,'=')}"
    value="{substring-after(.,'=')}"/>
    </xsl:for-each>
    </form>
    </xsl:template>
    </xsl:stylesheet>

    when applied upon the source.xml above, produces the desired result:

    <form action="identification.php">
    <input name="PHPSESSID" value="134134"/>
    <input name="page" value="2"/>
    <input name="param" value="3"/>
    </form>


    Hope this helped.


    Cheers,

    Dimitre Novatchev [XML MVP],
    FXSL developer, XML Insider,

    http://fxsl.sourceforge.net/ -- the home of FXSL
    Resume: http://fxsl.sf.net/DNovatchev/Resume/Res.html
     
    Dimitre Novatchev [MVP XML], Apr 5, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mike
    Replies:
    10
    Views:
    760
    Joris Gillis
    Nov 2, 2004
  2. Alex
    Replies:
    2
    Views:
    403
  3. DanielJohnson

    struct point not identified by gcc

    DanielJohnson, Feb 22, 2007, in forum: C Programming
    Replies:
    39
    Views:
    6,278
    CBFalconer
    Mar 7, 2007
  4. Ben
    Replies:
    2
    Views:
    953
  5. Lawrence D'Oliveiro

    Death To Sub-Sub-Sub-Directories!

    Lawrence D'Oliveiro, May 5, 2011, in forum: Java
    Replies:
    92
    Views:
    2,162
    Lawrence D'Oliveiro
    May 20, 2011
Loading...

Share This Page