This transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:ext="http://exslt.org/common">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="pqueryString">
<s name="field1">Hello world!</s>
</xsl:param>
<xsl:variable name="vqs"
select="msxsl:node-set($pqueryString)"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="input[@type='text']/@value">
<xsl:attribute name="value">
<xsl:value-of select=
"../@value[not($vqs/s[@name = current()/../@name])]
|
$vqs/s[@name = current()/../@name]
"/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
when applied on this XML document (corrected the provided to be well-formed):
<html>
<head></head>
<body>
<form id="myForm" method="POST">
<input name="field1" type="text" value="default" />
<input type="submit" onClick="function();"/>
</form>
</body>
</html>
produces the wanted, correct result:
<html>
<head></head>
<body>
<form id="myForm" method="POST">
<input name="field1" type="text" value="Hello world!"></input>
<input type="submit" onClick="function();"></input>
</form>
</body>
</html>
Do note:
The Identity rule is used to copy the document as is.
The query string of the HTTP request is passed as an external parameter named pqueryString
.
The ext:node-set()
extension used here will not be needed in practice, because the parameter will be passed externally.
The only override of the identity rule is for attributes named value
.
The template matching @value
creates an attribute with the same name and its value is either the one specified by the user (contained in the query string param) or if the user didn't specify a value for this attribute, then its current value.