使用XML + 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<xsl:for-each select="FIELDS/FIELD">
2 dim m_<xsl:value-of select="@Name" /> as <xsl:value-of select="@Type" />
3</xsl:for-each>
2 dim m_<xsl:value-of select="@Name" /> as <xsl:value-of select="@Type" />
3</xsl:for-each>
(2)构建函数中参数的生成。
这里同样使用<xsl:for-each></xsl:for-each>来成才参数,但要注意末尾才是的逗号问题。
1<xsl:for-each select="PrimaryKeys/FIELD">
2 <xsl:if test="position()!=count(../*)">Byval p<xsl:value-of select="@Name" /> as <xsl:value-of select="@Type" />, </xsl:if>
3 <xsl:if test="position() = count(../*)">Byval p<xsl:value-of select="@Name" /> as <xsl:value-of select="@Type" /></xsl:if>
4</xsl:for-each>
2 <xsl:if test="position()!=count(../*)">Byval p<xsl:value-of select="@Name" /> as <xsl:value-of select="@Type" />, </xsl:if>
3 <xsl:if test="position() = count(../*)">Byval p<xsl:value-of select="@Name" /> as <xsl:value-of select="@Type" /></xsl:if>
4</xsl:for-each>
(3)用另外一个template来获取Element的属性(取table name)
1 <xsl:template match="//TableName">
2 <xsl:value-of select="@value" />
3 </xsl:template>
2 <xsl:value-of select="@value" />
3 </xsl:template>
最后写全的xlst如下面:
1<?xml version="1.0" encoding="UTF-8" ?>
2<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
3 <xsl:output method="xml" omit-xml-declaration="yes" indent="yes" />
4 <xsl:template match="/Schema">Namespace Data
5 Public Class Entity<xsl:apply-templates select="//TableName" />
6
7 'variable<xsl:for-each select="FIELDS/FIELD">
8 dim m_<xsl:value-of select="@Name" /> as <xsl:value-of select="@Type" />
9 </xsl:for-each>
10
11 Public Sub New()
12
13 End Sub
14
15 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 <xsl:for-each select="PrimaryKeys/FIELD">
17 m_<xsl:value-of select="@Name" /> = p<xsl:value-of select="@Name" />
18 </xsl:for-each>
19 End Sub
20
21 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 <xsl:for-each select="FIELDS/FIELD">
23 m_<xsl:value-of select="@Name" /> = p<xsl:value-of select="@Name" />
24 </xsl:for-each>
25 End Sub
26 <xsl:for-each select="FIELDS/FIELD">
27 Public Property <xsl:value-of select="@Name" />() As <xsl:value-of select="@Type" />
28 Get
29 Return m_<xsl:value-of select="@Name" />
30 End Get
31 Set(ByVal Value As <xsl:value-of select="@Type" />)
32 m_<xsl:value-of select="@Name" /> = Value
33 End Set
34 End Property
35 </xsl:for-each>
36 End Class
37End Namespace</xsl:template>
38 <xsl:template match="//TableName">
39 <xsl:value-of select="@value" />
40 </xsl:template>
41</xsl:stylesheet>
2<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
3 <xsl:output method="xml" omit-xml-declaration="yes" indent="yes" />
4 <xsl:template match="/Schema">Namespace Data
5 Public Class Entity<xsl:apply-templates select="//TableName" />
6
7 'variable<xsl:for-each select="FIELDS/FIELD">
8 dim m_<xsl:value-of select="@Name" /> as <xsl:value-of select="@Type" />
9 </xsl:for-each>
10
11 Public Sub New()
12
13 End Sub
14
15 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 <xsl:for-each select="PrimaryKeys/FIELD">
17 m_<xsl:value-of select="@Name" /> = p<xsl:value-of select="@Name" />
18 </xsl:for-each>
19 End Sub
20
21 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 <xsl:for-each select="FIELDS/FIELD">
23 m_<xsl:value-of select="@Name" /> = p<xsl:value-of select="@Name" />
24 </xsl:for-each>
25 End Sub
26 <xsl:for-each select="FIELDS/FIELD">
27 Public Property <xsl:value-of select="@Name" />() As <xsl:value-of select="@Type" />
28 Get
29 Return m_<xsl:value-of select="@Name" />
30 End Get
31 Set(ByVal Value As <xsl:value-of select="@Type" />)
32 m_<xsl:value-of select="@Name" /> = Value
33 End Set
34 End Property
35 </xsl:for-each>
36 End Class
37End Namespace</xsl:template>
38 <xsl:template match="//TableName">
39 <xsl:value-of select="@value" />
40 </xsl:template>
41</xsl:stylesheet>
-- 未完带续
相关链接: 使用XML + XSLT进行实体类代码生成 (一)