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

S

Sebek

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
 
M

Martin Honnen

Sebek said:
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>
 
D

Dimitre Novatchev [MVP XML]

Sebek said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top