我可以在 XSLT 中创建模板吗? [英] Can I create a template within XSLT?

查看:21
本文介绍了我可以在 XSLT 中创建模板吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 XSLT 从 XML 创建一个 ASP.NET 用户控件.目前我真的一点一点地把结果放在一起:

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>

因为当需要更改 html 结构时,这看起来更简洁,更易于维护.

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.

更新:以下是一些示例输入 XML:

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>

输出应如下所示:

<%@ Control Language="C#" AutoEventWireup="true" Inherits="DevelopmentWeb.WebUserControl1" %>
<asp:TextBox runat="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>

或者这个:

  <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>

这篇关于我可以在 XSLT 中创建模板吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆