csteinhilber
Programmer
This probably doesn't fall under "best practices", but, again, I'm trying to work with legacy data here.
I have XML that looks like:
I would like to be able to transform this into an HTML form, with the preferred output being
As you can see, each field will have different sets of nodes... which will hopefully translate directly to different sets of attributes for the HTML tags. So I really want an XSL template that is generic enough to handle whatever nodes are present and ignore those that aren't.
I thought of something like
But, of course, it doesn't work in real life.
If there a way to dynamically produce attributes (in this case for the input tag) based on the name/value pairs of an unknown number of nodes?
Thanks in advance!
-Carl
I have XML that looks like:
Code:
:
<fields>
<field>
<type>checkbox</type>
<name>name 1</name>
<checked>checked</checked>
<label>Checkbox 1</label>
<id>1234</id>
</field>
<field>
<type>text</type>
<name>name 2</name>
<value>Default value</value>
</field>
<field>
<type>checkbox</type>
<name>name 3</name>
<label>Checkbox 2</label>
<value>Yes</value>
<onClick>someFunction()</onClick>
</field>
</fields>
:
I would like to be able to transform this into an HTML form, with the preferred output being
Code:
<input type="checkbox" name="name 1" checked="checked" id="1234" /> Checkbox 1<br />
<input type="text" name="name 2" value="Default value" /><br />
<input type="checkbox" name="name 3" value="Yes" onClick="someFunction()" /> Checkbox 2<br />
As you can see, each field will have different sets of nodes... which will hopefully translate directly to different sets of attributes for the HTML tags. So I really want an XSL template that is generic enough to handle whatever nodes are present and ignore those that aren't.
I thought of something like
Code:
:
<xsl:apply-templates select="fields/*" />
:
<xsl:template match="field">
<input>
<xsl:for-each select="./*">
<xsl:attribute name="name()">
<xsl:value-of select="@name()" />
</xsl:attribute>
</xsl:for-each>
</input>
</xsl:template>
But, of course, it doesn't work in real life.
If there a way to dynamically produce attributes (in this case for the input tag) based on the name/value pairs of an unknown number of nodes?
Thanks in advance!
-Carl