XML Schema / XML / XSLT 综合使用之电子表单

一:demo1.xml
<?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet type="text/xsl" href="demo1.xsl"?>
<ef:form xmlns:ef="www.jin.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="www.jin.com demo1.xsd">
	<ef:multipleChoiceField label="多选1" id="duoxuan1" description="描述多选字段">
		<ef:options>
			<ef:option>选项1</ef:option>
			<ef:option checked="true">选项2</ef:option>
			<ef:option>选项3</ef:option>
		</ef:options>
	</ef:multipleChoiceField>
	<ef:group label="组1">
		<ef:dateField label="时间" id="shijian" description="描述时间字段" />
		<ef:numbleField label="数字" id="shuzi" value="666" />
	</ef:group>
	<ef:dateField label="时间1" id="shijian1" value="2017-03-28" />
	<ef:dateField label="时间2" id="shijian2" />
	<ef:stringField label="字符串1" id="zifuchuan" value="默认字符串" />
	<ef:textField label="常字符串" id="DD"> xml xslt schema 6666666 </ef:textField>g
	<ef:group label="组2">
		<ef:singleChoiceField label="单选" id="danxuan">
			<ef:options>
				<ef:option>选项1</ef:option>
				<ef:option>选项2</ef:option>
				<ef:option>选项3</ef:option>
			</ef:options>
		</ef:singleChoiceField>
	</ef:group>
</ef:form>

二:demo1.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ef="www.jin.com">
	<xsl:template match="/">
		<html>
			<head>
				<title>显示表单</title>
				<link type="text/css" rel="stylesheet" href="ef.css" />
			</head>
			<body>
				<xsl:apply-templates />
			</body>
		</html>
	</xsl:template>
	 
	<!-- 组模板 -->
	<xsl:template match="ef:group">
		<fieldSet class="ef-group">
			<legend>
				<xsl:value-of select="@label"></xsl:value-of>
			</legend>
			<xsl:apply-templates />
		</fieldSet>
	</xsl:template> 
	
	
	<!-- 日期字段模板 -->
	<xsl:template match="ef:dateField">
		<div class="ef-date-field">
			<xsl:call-template name="fieldLabel" />
			<div class="ef-input-control">
				<input type="text">
					<xsl:call-template name="inputControlAttribute" />
				</input>
			</div>
		</div>
	</xsl:template> 
	
	<!-- input输入控件属性模板 -->
	<xsl:template name="inputControlAttribute">
		<xsl:attribute name="id">
			<xsl:value-of select="@id" />
		</xsl:attribute>
		<xsl:attribute name="name">
			<xsl:value-of select="@id" />
		</xsl:attribute>
		<xsl:attribute name="value">
			<xsl:value-of select="@value" />
		</xsl:attribute>
	</xsl:template> 
	<!-- 字段标签模板 -->
	<xsl:template name="fieldLabel">
		<label class="ef-field-label">
			<xsl:attribute name="for">
				<xsl:value-of select="@id" />
			</xsl:attribute>
			<xsl:attribute name="title">
				<xsl:value-of select="@description" />
			</xsl:attribute>
			<xsl:value-of select="@label" />
		</label>
	</xsl:template> 
	<!-- 选项属性模板 -->
	<xsl:template name="choiceOptionAttribute">
		<xsl:param name="choiceFieldId" />
		<xsl:attribute name="id">
			<xsl:value-of select="$choiceFieldId" />
			<xsl:number />
		</xsl:attribute>
		<xsl:attribute name="name">
			<xsl:value-of select="$choiceFieldId" />
		</xsl:attribute>
		<xsl:attribute name="value">
			<xsl:choose>
				<xsl:when test="@value">
					<xsl:value-of select="@value" />
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="text()" />
				</xsl:otherwise>
			</xsl:choose>
		</xsl:attribute>
		<xsl:if test="@checked = 'true'">
			<xsl:attribute name="checked">
				<xsl:value-of select="'checked'" />
			</xsl:attribute>
		</xsl:if>
	</xsl:template> 
	<!-- 选项标签模板 -->
	<xsl:template name="optionLabel">
		<xsl:param name="choiceFieldId" />
		<label>
			<xsl:attribute name="for">
				<xsl:value-of select="$choiceFieldId" />
				<xsl:number />
			</xsl:attribute>
			<xsl:value-of select="text()" />
		</label>
	</xsl:template> 
	<!-- 多选字段模板 -->
	<xsl:template match="ef:multipleChoiceField">
		<xsl:variable name="choiceFieldId" select="@id" />
		<div class="ef-multiple-choice-field">
			<xsl:call-template name="fieldLabel" />
			<div class="ef-checkbox-group">
				<xsl:for-each select="ef:options/ef:option">
					<div class="ef-checkbox">
						<input type="checkbox">
							<xsl:call-template name="choiceOptionAttribute">
								<xsl:with-param name="choiceFieldId" select="$choiceFieldId" />
							</xsl:call-template>
						</input>
						<xsl:call-template name="optionLabel">
							<xsl:with-param name="choiceFieldId" select="$choiceFieldId" />
						</xsl:call-template>
					</div>
				</xsl:for-each>
			</div>
		</div>
	</xsl:template> 
	<!-- 数字字段模板 -->
	<xsl:template match="ef:numbleField">
		<div class="ef-numble-field">
			<xsl:call-template name="fieldLabel" />
			<div class="ef-input-control">
				<input type="text">
					<xsl:call-template name="inputControlAttribute" />
				</input>
			</div>
		</div>
	</xsl:template> 
	<!-- 单选字段模板 -->
	<xsl:template match="ef:singleChoiceField">
		<xsl:variable name="choiceFieldId" select="@id" />
		<div class="ef-single-choice-field">
			<xsl:call-template name="fieldLabel" />
			<div class="ef-radio-group">
				<xsl:for-each select="ef:options/ef:option">
					<div class="ef-radio">
						<input type="radio">
							<xsl:call-template name="choiceOptionAttribute">
								<xsl:with-param name="choiceFieldId" select="$choiceFieldId" />
							</xsl:call-template>
						</input>
						<xsl:call-template name="optionLabel">
							<xsl:with-param name="choiceFieldId" select="$choiceFieldId" />
						</xsl:call-template>
					</div>
				</xsl:for-each>
			</div>
		</div>
	</xsl:template> 

	<!-- 字符串字段模板 -->
	<xsl:template match="ef:stringField">
		<div class="ef-string-field">
			<xsl:call-template name="fieldLabel" />
			<div class="ef-input-control">
				<input type="text">
					<xsl:call-template name="inputControlAttribute" />
				</input>
			</div>
		</div>
	</xsl:template> 

	<!-- 长文本字段模板 -->
	<xsl:template match="ef:textField">
		<div class="ef-text-field">
			<xsl:call-template name="fieldLabel" />
			<div class="ef-textarea-control">
				<textarea>
					<xsl:attribute name="id">
						<xsl:value-of select="@id" />
					</xsl:attribute>
					<xsl:attribute name="name">
						<xsl:value-of select="@id" />
					</xsl:attribute>
					<xsl:value-of select="text()" />
				</textarea>
			</div>
		</div>
	</xsl:template>
</xsl:stylesheet>

三:demo1.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="www.jin.com" elementFormDefault="qualified"
	xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ef="www.jin.com">
	<xs:attributeGroup name="fieldAttributeGroup">
		<xs:attribute name="id" type="xs:Name" use="required" />
		<xs:attribute name="label" type="xs:token" use="required" />
		<xs:attribute name="require" type="xs:boolean" use="optional" />
		<xs:attribute name="description" type="xs:token" use="optional" />
	</xs:attributeGroup>
	<xs:complexType name="stringField">
		<xs:attributeGroup ref="ef:fieldAttributeGroup" />
		<xs:attribute name="value" type="xs:token" />
	</xs:complexType>
	<xs:complexType name="textField">
		<xs:simpleContent>
			<xs:extension base="xs:string">
				<xs:attributeGroup ref="ef:fieldAttributeGroup" />
			</xs:extension>
		</xs:simpleContent>
	</xs:complexType>
	<xs:complexType name="numbleField">
		<xs:attributeGroup ref="ef:fieldAttributeGroup" />
		<xs:attribute name="value" type="xs:decimal" />
	</xs:complexType>
	<xs:complexType name="dateField">
		<xs:attributeGroup ref="ef:fieldAttributeGroup" />
		<xs:attribute name="value" type="xs:date" />
	</xs:complexType>
	<xs:complexType name="singleChoiceField">
		<xs:all>
			<xs:element name="options" type="ef:options" />
		</xs:all>
		<xs:attributeGroup ref="ef:fieldAttributeGroup" />
	</xs:complexType>
	<xs:complexType name="multipleChoiceField">
		<xs:all>
			<xs:element name="options" type="ef:options" />
		</xs:all>
		<xs:attributeGroup ref="ef:fieldAttributeGroup" />
	</xs:complexType>
	<xs:complexType name="options">
		<xs:choice minOccurs="2" maxOccurs="unbounded">
			<xs:element name="option" type="ef:option" />
		</xs:choice>
	</xs:complexType>
	<xs:complexType name="option">
		<xs:simpleContent>
			<xs:extension base="xs:normalizedString">
				<xs:attribute name="checked" type="xs:boolean" />
			</xs:extension>
		</xs:simpleContent>
	</xs:complexType>
	<xs:complexType name="group">
		<xs:group ref="ef:field" minOccurs="1" maxOccurs="unbounded" />
		<xs:attribute name="label" type="xs:token" use="required" />
	</xs:complexType>
	<xs:group name="field">
		<xs:choice>
			<xs:element name="dateField" type="ef:dateField" nillable="true" />
			<xs:element name="multipleChoiceField" type="ef:multipleChoiceField" />
			<xs:element name="numbleField" type="ef:numbleField"
				nillable="true" />
			<xs:element name="singleChoiceField" type="ef:singleChoiceField" />
			<xs:element name="stringField" type="ef:stringField"
				nillable="true" />
			<xs:element name="textField" type="ef:textField" nillable="true" />
			<xs:element name="group" type="ef:group" />
		</xs:choice>
	</xs:group>
	<xs:element name="form">
		<xs:complexType>
			<xs:group ref="ef:field" minOccurs="1" maxOccurs="unbounded" />
		</xs:complexType>
		<xs:key name="ID">
			<xs:selector
				xpath=".//ef:dateField | .//ef:multipleChoiceField | .//ef:numbleField | .//ef:singleChoiceField | .//ef:stringField | .//ef:textField" />
			<xs:field xpath="@id" />
		</xs:key>
	</xs:element>
</xs:schema>

四:ef.css

@CHARSET "UTF-8";
.ef-group,.ef-date-field,.ef-multiple-choice-field,.ef-numble-field,.ef-single-choice-field,.ef-string-field,.ef-text-field
{ margin: 5px; width: 100%; } .ef-field-label { float: left; width:
100px; font-weight: bold; } .ef-checkbox,.ef-radio { float: left;
margin-right: 10px; }
 五:结果




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值