Can I create a template within XSLT?
I want to create a ASP.NET user control from an XML using XSLT. Currently I really put the result together bit by bit:
<xsl:template match="TextField">
<xsl:apply-templates select="Label" />
<xsl:text><![CDATA[<asp:TextBox ID="]]></xsl:text>
<xsl:value-of select="@id"/>
<xsl:if test="@defaultValue">
<xsl:text><![CDATA[" value="]]></xsl:text>
<xsl:value-of select="@defaultValue"/>
</xsl:if>
<xsl:text><![CDATA[" runat="server"></asp:TextBox>]]></xsl:text>
<xsl:copy-of select="$br"/>
</xsl:template>
It would be rally cool if I could instead do something like this:
<xsl:template match="TextField">
<xsl:apply-templates select="Label" />
<xsl:variable name="localTemplate" select="expression">
<xsl:text><![CDATA[
<asp:TextBox ID="{theID}" value="{theDefaultValue}" runat="server"></asp:TextBox>
]]></xsl:text>
<xsl:copy-of select="$br"/>
</xsl:variable>
<!-- replace {theID} and {theDefaultValue} from the corresponding
values of the input XML and then return the content of that
variable
-->
</xsl:template>
Because that seems much cleaner and more easy to maintain later on when there is the need to change the html structure.
Is there a way to achieve this and if yes, how would that look like? I don't need a working solution, just some hints on what to do.
Update: Here is some sample input XML:
<?xml version="1.0" encoding="utf-8" ?>
<Form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="XmlForm.xsd">
<Validation enabled="false" enableValidationSummary="true" />
<FieldSet>
<TextField id="firstInput" css-class="textfield-css-class" />
<TextField id="secondInput" defaultValue="Wrdlbmrpft">
<Label translatable="true" >Label Text</Label>
</TextField>
</FieldSet>
</Form>
The output should look like this:
<%@ Control Language="C#" AutoEventWireup="true" Inherits="DevelopmentWeb.WebUserControl1" %>
<asp:TextBox runa开发者_Go百科t="server" ID="firstInput" CssClass="textfield-css-class"></asp:TextBox>
<asp:Label runat="server" AssociatedControlID="secondInput">Label Text</asp:Label>
<asp:TextBox runat="server" ID="secondInput">Wrdlbrmpft</asp:TextBox>
I don't think there would be any reason to create a variable. You should be able to do something like this:
<xsl:template match="TextField">
<xsl:apply-templates select="Label" />
<asp:TextBox ID="{@id}" value="{@defaultValue}" runat="server"/>
<xsl:copy-of select="$br"/>
</xsl:template>
or this:
<xsl:template match="TextField">
<xsl:apply-templates select="Label" />
<asp:TextBox runat="server">
<xsl:if test="@id">
<xsl:attribute name="ID">
<xsl:value-of select="@id"/>
</xsl:attribute>
</xsl:if>
<xsl:if test="@defaultValue">
<xsl:attribute name="value">
<xsl:value-of select="@defaultValue"/>
</xsl:attribute>
</xsl:if>
</asp:TextBox>
<xsl:copy-of select="$br"/>
</xsl:template>
Define appropriate namespace for prefix asp, e.g.:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:asp="System.Web.UI.WebControls">
In your template simply write:
<asp:TextBox ID="{@id}" runat="server">
<xsl:if test="@defaultValue">
<xsl:attribute name="value">
<xsl:value-of select="@defaultValue"/>
</xsl:attribute>
</xsl:if>
</asp:TextBox>
精彩评论