用 XSLT 將 PowerDesign 9.5 的 cdm 文件直接轉化為源代碼
用 XSLT 將 PowerDesign 9.5 的 cdm 文件直接轉化為源代碼
工作中發現一旦項目中使用powerdesign進行建模之后,最后進行編碼的時候幾乎還需要自己手工做一遍,因為架構不同,使用powerdesign生成的C#源代碼幾乎不能使用。所以沒辦法只能自己來做這個轉換。
第一版轉換程序用的C#編的程序,一旦要修改,就要重新編譯,不爽。所以這次用純的XSLT來做這個轉換。因為cdm文件比較復雜,作第一版的時候曾經嘗試過用XSLT,但是最后因為自己xslt功力不夠,只好轉用代碼實現。
經過半天奮戰,半成品如下。我會根據工作進度及時修改的![]()
<?xml version="1.0" encoding="gb2312"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:a="attribute" xmlns:c="collection" xmlns:o="object">
<xsl:output method="text" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<html>
<head>
<title>C# source from CDM file</title>
</head>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<!-- 覆蓋缺省的文字處理過程 -->
<xsl:template match="text()|@*">
</xsl:template>
<!-- /Model -->
<xsl:template match="Model">
<xsl:apply-templates/>
</xsl:template>
<!-- /Model/o:RootObject -->
<xsl:template match="o:RootObject">
<xsl:apply-templates/>
</xsl:template>
<!-- /Model/o:RootObject/c:Children -->
<xsl:template match="c:Children">
<xsl:apply-templates/>
</xsl:template>
<!-- /Model/o:RootObject/c:Children/o:Model -->
<xsl:template match="o:Model">
<xsl:apply-templates/>
</xsl:template>
<!-- o:Model/o:Packages -->
<xsl:template match="c:Packages">
<xsl:apply-templates/>
</xsl:template>
<!-- o:Model/o:Packages/o:Package -->
<xsl:template match="o:Package">
// <xsl:value-of select="a:Name" />
namespace <xsl:value-of select="a:Code" />
{
<xsl:apply-templates/>
} // namespace <xsl:value-of select="a:Code" />
</xsl:template>
<xsl:template match="c:ConceptualDiagrams">
</xsl:template>
<xsl:template match="c:Relationships">
</xsl:template>
<xsl:template match="c:Domains">
</xsl:template>
<xsl:template match="c:ChildExtendedDependencies">
</xsl:template>
<xsl:template match="c:TargetModels">
</xsl:template>
<!-- o:Model/o:Packages/o:Package/c:Entities -->
<xsl:template match="c:Entities">
<xsl:apply-templates/>
</xsl:template>
<!-- o:Model/o:Packages/o:Package/c:Entities/o:Entity -->
<xsl:template match="o:Entity">
// class <xsl:value-of select="a:Name" />
public class <xsl:value-of select="a:Code" />
{
<xsl:apply-templates/>
}// class <xsl:value-of select="a:Code" />
</xsl:template>
<xsl:template match="c:Identifiers">
</xsl:template>
<xsl:template match="c:PrimaryIdentifier">
</xsl:template>
<!-- o:Model/o:Packages/o:Package/c:Entities/o:Entity/c:Attributes -->
<xsl:template match="c:Attributes">
<xsl:apply-templates />
</xsl:template>
<!-- 開始寫入數據字段定義和初始值 -->
<!-- o:Model/o:Packages/o:Package/c:Entities/o:Entity/c:Attributes/o:EntityAttribute -->
<xsl:template match="o:EntityAttribute">
<xsl:apply-templates mode="FieldRefer" select="c:DataItem" /><xsl:if test="a:Mandatory=0"> = null</xsl:if>;
</xsl:template>
<!-- o:Model/o:Packages/o:Package/c:Entities/o:Entity/c:Attributes/o:EntityAttribute/c:DataItem -->
<!-- 根據DataItem中的子元素,分別定向到 DataItem 或 Shortcut 中 -->
<xsl:template match="c:DataItem" mode="FieldRefer">
<xsl:apply-templates mode="FieldJump" />
</xsl:template>
<!-- o:Model/o:Packages/o:Package/c:Entities/o:Entity/c:Attributes/o:EntityAttribute/c:DataItem/o:DataItem -->
<xsl:template match="o:DataItem" mode="FieldJump">
<!-- 跳到 o:Model/o:Packages/o:Package/c:DataItems/o:DataItem -->
<xsl:variable name="ref" select="@Ref" />
<xsl:apply-templates mode="Field" select="../../../../../../c:DataItems/o:DataItem[@Id=$ref]" />
</xsl:template>
<!-- o:Model/o:Packages/o:Package/c:Entities/o:Entity/c:Attributes/o:EntityAttribute/c:DataItem/o:Shortcut -->
<xsl:template match="o:Shortcut" mode="FieldJump">
<!-- 取得 o:Model/o:Packages/o:Package/c:Entities/o:Shortcut 信息并跳到 DataItem -->
<xsl:variable name="ref" select="@Ref" />
<xsl:variable name="tag" select="http://o:Shortcut[@Id=$ref]/a:TargetID" />
<xsl:apply-templates mode="Field" select="http://c:DataItems/o:DataItem[a:ObjectID=$tag]" />
</xsl:template>
<!-- o:Model/o:Packages/o:Package/c:DataItems/o:DataItem -->
<!-- 顯示字段的類型:如果沒有domain則解釋映射dataType的值 -->
<xsl:template match="o:DataItem" mode="Field">
<xsl:variable name="domainId" select="c:Domain/o:Domain/@Ref" />
[XmlElement("<xsl:value-of select="a:Code" />")] public <xsl:if test="string-length($domainId) != 0" >
<xsl:apply-templates mode="FieldType" select="http://c:Domains/o:Domain[@Id=$domainId]" />
</xsl:if><xsl:if test="string-length($domainId) = 0" >
<xsl:call-template name="SqlType2CsType">
<xsl:with-param name="type" select="a:DataType" />
<xsl:with-param name="len" select="a:Length" />
</xsl:call-template>
</xsl:if> m_<xsl:value-of select="a:Code" />
</xsl:template>
<!-- o:Model/c:Domains/o:Domain -->
<!-- 顯示類型 -->
<xsl:template match="o:Domain" mode="FieldType"><xsl:value-of select="a:Code" />Type</xsl:template>
<!-- 轉換 SQL 類型到 C# 類型 -->
<xsl:template name="SqlType2CsType">
<xsl:param name="type" />
<xsl:param name="len" />
<xsl:choose>
<xsl:when test="$type='VA'">string</xsl:when>
</xsl:choose>
</xsl:template>
<!-- o:Model/o:Packages/o:Package/c:Relationships/ -->
<!-- 將實體之間關系轉化為字段 -->
<xsl:template match="c:Relationships" mode="Field">
</xsl:template>
<!-- o:Model/o:Packages/o:Package/c:Relationships/ -->
<!-- 將實體之間關系轉化為字段 -->
<xsl:template match="o:Relationship" mode="Field">
</xsl:template>
</xsl:stylesheet>

公眾號:老翅寒暑
浙公網安備 33010602011771號