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&p aram=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&page =2&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, '&')">
<xsl:value-of select="string(substring-before($queryString,
'&'))" />
</xsl:when>
<xsl:otherwise><xsl:value-of select="string($queryString)"
/></xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="remainingQueryString"
select="substring-after($queryString, '&')" />
<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/