使用XML + XLST进行实体类代码生成 (二)

博客讲述了制作编码模板时,因对XSLT语法和使用方法陌生而花费不少时间。使用VS.Net 2003创建xslt文件时遇到报错,后发现是未给出xls的namespace所致。还提到使用<xsl:for-each>语句遍历数据字段生成变量,最后给出了写全的xlst。

三,制作编码的模板
制作模块可是颇费功夫的一件事情。由于之前对xslt的语法和使用方法比较陌生,所以在上面花了不少时间。幸好有 仙桃人 的大力帮助,才搞明白了XSLT的用法。

起初,使用VS.Net 2003创建了一个xslt文件,然后,在里面添加了一个template进去。可是,不知怎么了总在XslTransform类Load这个xslt文件时报错,加上当时使用debug mode来运行程序,没有任何提示信息害我浪费不少时间。后来才发现,VS.Net 2003生成的xlst中没有给出xls的namespace,所以无法解释<xsl:template></xsl:template>。真是害我不浅。不过经过一番努力,还是搞定了这些问题。

这里有一些关键点:
(1) 生成属性所需的private变量。
这里主要使用<xsl:for-each></xsl:for-each>语句来遍历数据字段,来生成所需要的变量。

1 None.gif < xsl:for-each  select ="FIELDS/FIELD" >
2 None.gif        dim m_ < xsl:value-of  select ="@Name"   />  as  < xsl:value-of  select ="@Type"   />
3 None.gif </ xsl:for-each >

(2)构建函数中参数的生成。
这里同样使用<xsl:for-each></xsl:for-each>来成才参数,但要注意末尾才是的逗号问题。
1 None.gif < xsl:for-each  select ="PrimaryKeys/FIELD" >
2 None.gif    < xsl:if  test ="position()!=count(../*)" > Byval p < xsl:value-of  select ="@Name"   />  as  < xsl:value-of  select ="@Type"   /> </ xsl:if >
3 None.gif    < xsl:if  test ="position() = count(../*)" > Byval p < xsl:value-of  select ="@Name"   />  as  < xsl:value-of  select ="@Type"   /></ xsl:if >
4 None.gif </ xsl:for-each >

(3)用另外一个template来获取Element的属性(取table name)
1 None.gif      < xsl:template  match ="//TableName" >
2 None.gif         < xsl:value-of  select ="@value"   />
3 None.gif     </ xsl:template >

最后写全的xlst如下面:

 1 None.gif <? xml version="1.0" encoding="UTF-8"  ?>
 2 None.gif < xsl:stylesheet  version ="1.0"  xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" >
 3 None.gif     < xsl:output  method ="xml"  omit-xml-declaration ="yes"  indent ="yes"   />
 4 None.gif     < xsl:template  match ="/Schema" > Namespace Data
 5 None.gif    Public Class Entity < xsl:apply-templates  select ="//TableName"   />
 6 None.gif
 7 None.gif        'variable < xsl:for-each  select ="FIELDS/FIELD" >
 8 None.gif        dim m_ < xsl:value-of  select ="@Name"   />  as  < xsl:value-of  select ="@Type"   />
 9 None.gif         </ xsl:for-each >
10 None.gif        
11 None.gif        Public Sub New()
12 None.gif
13 None.gif        End Sub
14 None.gif
15 None.gif        Public Sub New( < xsl:for-each  select ="PrimaryKeys/FIELD" >< xsl:if  test ="position()!=count(../*)" > Byval p < xsl:value-of  select ="@Name"   />  as  < xsl:value-of  select ="@Type"   /> </ xsl:if >< xsl:if  test ="position() = count(../*)" > Byval p < xsl:value-of  select ="@Name"   />  as  < xsl:value-of  select ="@Type"   /></ xsl:if ></ xsl:for-each > )
16 None.gif         < xsl:for-each  select ="PrimaryKeys/FIELD" >
17 None.gif            m_ < xsl:value-of  select ="@Name"   />  = p < xsl:value-of  select ="@Name"   />
18 None.gif         </ xsl:for-each >
19 None.gif        End Sub
20 None.gif
21 None.gif        Public Sub New( < xsl:for-each  select ="FIELDS/FIELD" >< xsl:if  test ="position()!=count(../*)" > Byval p < xsl:value-of  select ="@Name"   />  as  < xsl:value-of  select ="@Type"   /> </ xsl:if >< xsl:if  test ="position() = count(../*)" > Byval p < xsl:value-of  select ="@Name"   />  as  < xsl:value-of  select ="@Type"   /></ xsl:if ></ xsl:for-each > )
22 None.gif         < xsl:for-each  select ="FIELDS/FIELD" >
23 None.gif            m_ < xsl:value-of  select ="@Name"   />  = p < xsl:value-of  select ="@Name"   />
24 None.gif         </ xsl:for-each >
25 None.gif        End Sub
26 None.gif         < xsl:for-each  select ="FIELDS/FIELD" >
27 None.gif        Public Property  < xsl:value-of  select ="@Name"   /> () As  < xsl:value-of  select ="@Type"   />
28 None.gif            Get
29 None.gif                Return m_ < xsl:value-of  select ="@Name"   />
30 None.gif            End Get
31 None.gif            Set(ByVal Value As  < xsl:value-of  select ="@Type"   /> )
32 None.gif                m_ < xsl:value-of  select ="@Name"   />  = Value
33 None.gif            End Set
34 None.gif        End Property
35 None.gif         </ xsl:for-each >
36 None.gif    End Class
37 None.gifEnd Namespace </ xsl:template >
38 None.gif     < xsl:template  match ="//TableName" >
39 None.gif         < xsl:value-of  select ="@value"   />
40 None.gif     </ xsl:template >
41 None.gif </ xsl:stylesheet >

 

 -- 未完带续

相关链接: 使用XML + XSLT进行实体类代码生成 (一)

转载于:https://www.cnblogs.com/Lancer/archive/2005/11/03/267580.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值