Skip to content

Instantly share code, notes, and snippets.

@strlcp
Last active December 23, 2015 07:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save strlcp/6602414 to your computer and use it in GitHub Desktop.
Save strlcp/6602414 to your computer and use it in GitHub Desktop.
xml defining form in symphony xslt to parse it
<!-- static xml use as static xml datasource to configure -->
<entry>
<name type="required">
<text size="40" />
</name>
</entry>
<entry>
<lastname type="optional">
<text size="40" />
<default>name</default>
</lastname>
</entry>
<entry>
<email type="required">
<text size="40" />
</email>
</entry>
<entry>
<subject type="required">
<text size="40" />
</subject>
</entry>
<entry>
<message type="optional">
<textarea size="50, 15" />
</message>
</entry>
<!-- xslt rendering to form -->
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template name="response">
<!-- test if exist / sucess/ error -->
<xsl:choose>
<!-- if not exist -->
<!-- testing wheter invalid or missing on each field, but seems to be same -->
<xsl:when test="not(events/msg) or events/msg[@result != 'success']" >
<form method="post" action="" enctype="multipart/form-data">
<ul>
<li>
<input name="MAX_FILE_SIZE" type="hidden" value="2097152" />
</li>
<xsl:apply-templates select="feedback" />
<input name="action[msg]" type="submit" value="Submit" />
</ul>
</form>
</xsl:when>
<!-- done so far -->
<xsl:when test="events/msg[@result = 'success']">
<xsl:text> your message has been sent </xsl:text>
</xsl:when>
</xsl:choose>
</xsl:template>
<!-- making fom defined by static xml -->
<xsl:template match="*/msg" >
<xsl:copy>
<xsl:apply-templates select="@*|node()" mode="debug" />
</xsl:copy>
</xsl:template>
<xsl:template match="@*|node()" mode="debug">
<xsl:copy>
<xsl:apply-templates select="@*|node()" mode="debug"/>
</xsl:copy>
</xsl:template>
<!-- prining the form based on static xml-->
<xsl:template match="/data/feedback/entry">
<xsl:variable name="node" select=" ./*[1]" />
<xsl:variable name="input" select="$node/*[1]" />
<xsl:variable name="name" select="name($node)" />
<li>
<label>
<xsl:value-of select="name($node)" />
</label>
<xsl:choose>
<xsl:when test="name($input)='text'">
<input type="text">
<xsl:attribute name="name">
<xsl:value-of select="concat('fields[', name($node), ']')" />
<!-- setting class -->
</xsl:attribute>
<xsl:attribute name="class">
<xsl:choose>
<xsl:when test="/data/events/msg/*[name() = $name and ( @type = 'missing' or @type = 'invalid' )]" >
<xsl:value-of select="/data/events/msg/*[name() = $name]/@type" />
</xsl:when>
<xsl:otherwise>
<xsl:choose>
<xsl:when test="not(//data/events/msg)" >
<xsl:value-of select="$node/@type" />
</xsl:when>
<xsl:otherwise>
<xsl:text>done</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:attribute name="value">
<xsl:choose>
<xsl:when test="/data/events/msg/post-values/*[name() = $name]/text()">
<xsl:value-of select="/data/events/msg/post-values/*[name() = $name]/text()" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$node/default" />
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</input>
</xsl:when>
<xsl:when test="name($input)='textarea'" >
<textarea>
<xsl:attribute name="name">
<xsl:value-of select="concat('fields[', name($node), ']')" />
</xsl:attribute>
</textarea>
</xsl:when>
</xsl:choose>
</li>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment