Hello there,
I'm transforming some XML using an XSL file,
The XML holds form objects in groups,
The XSL needs to place groups into tables and the form objects into table cells,
However I only want to have 2 table cells to a row.
Here's a sample of my XML...
Code:
<?xml version="1.0"?>
<document xmlns:xf="http://apache.org/cocoon/jxforms/1.0">
<xf:form id="generic-refer" action="refer" method="GET">
<xf:group class="personalDetails">
<xf:label>Personal Details</xf:label>
<xf:input ref="/surname">
<xf:label>Surname</xf:label>
<xf:violations class="error"/>
</xf:input>
<xf:input ref="/firstname">
<xf:label>Firstname</xf:label>
<xf:violations class="error"/>
</xf:input>
<xf:select1 ref="/gender" appearance="full">
<xf:label>Gender</xf:label>
<xf:item>
<xf:label>Male</xf:label>
<xf:value>m</xf:value>
</xf:item>
<xf:item>
<xf:label>Female</xf:label>
<xf:value>f</xf:value>
</xf:item>
</xf:select1>
</group>
</xf:form>
</document>
As you can see there are tags with form element names such as input and select, in the full XML there are submit buttons and so on.
Now my XSL looks like this so far...
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xf="http://apache.org/cocoon/jxforms/1.0">
<xsl:output method="xml" omit-xml-declaration="no"/>
<xsl:template match="/">
<html>
<head>
<title>Form Test</title>
</head>
<body>
<table border="1">
<xsl:apply-templates select="//xf:group[@class='personalDetails']"/>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="xf:group">
<tr>
<td colspan="2"><xsl:value-of select="xf:label"/></td>
</tr>
<xsl:apply-templates select="*[position() div 2 != round(position() div 2)]"/>
</xsl:template>
<xsl:template match="xf:label">
<xsl:value-of select="xf:label"/>
</xsl:template>
<xsl:template match="xf:input">
<xsl:param name="pos" select="position()"/>
<tr>
<td><xsl:value-of select="xf:label"/>#160;
<input name="{@ref}" type="text" value="{xf:value/text()}" class="refTbox">
<xsl:copy-of select="@*[not(name()='ref')]"/>
<xsl:apply-templates select="xf:hint"/>
</input>
</td>
<td>
<xsl:apply-templates select="../*[position()=$pos * 2]" mode="Ievens"/>
</td>
</tr>
</xsl:template>
<xsl:template match="xf:input" mode="Ievens">
<xsl:value-of select="@ref"/>
<input name="{@ref}" type="text" value="{xf:value/text()}" class="refTbox">
<xsl:copy-of select="@*[not(name()='ref')]"/>
<xsl:apply-templates select="xf:hint"/>
</input>
</xsl:template>
<xsl:template match="xf:select1[@appearance='full']">
<xsl:param name="pos" select="position()"/>
<tr>
<td><xsl:value-of select="xf:label"/>#160;
<xsl:variable name="selected" select="xf:value"/>
<xsl:variable name="ref" select="@ref"/>
<xsl:for-each select="xf:item">
<input name="{$ref}" type="radio" value="{xf:value}">
<xsl:copy-of select="@*[not(name()='ref')]"/>
<xsl:if test="xf:value = $selected">
<xsl:attribute name="checked"/>
</xsl:if>
</input>
<xsl:value-of select="xf:label"/>
<br/>
</xsl:for-each>
</td>
<td>
<xsl:apply-templates select="../*[position()=$pos * 2]" mode="s1evens"/>
</td>
</tr>
</xsl:template>
<xsl:template match="xf:select1[@appearance='full']" mode="s1evens">
<xsl:value-of select="xf:label"/>#160;
<xsl:variable name="selected" select="xf:value"/>
<xsl:variable name="ref" select="@ref"/>
<xsl:for-each select="xf:item">
<input name="{$ref}" type="radio" value="{xf:value}">
<xsl:copy-of select="@*[not(name()='ref')]"/>
<xsl:if test="xf:value = $selected">
<xsl:attribute name="checked"/>
</xsl:if>
</input>
<xsl:value-of select="xf:label"/>
<br/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
*phew!!
Ok sorry about the long code post!
But I'm really not sure where to go next, I want basically to pass through a group with any amount of child nodes (form elements) and have them put into rows of two columns each, my code works if i don't include anything but the xf:input nodes, but how do I get it to work universally, can anyone perhaps guide me to a better way of doing this?
TIA,
Blaise.