用 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="//o:Shortcut[@Id=$ref]/a:TargetID" />
        
<xsl:apply-templates mode="Field" select="//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="//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>
原文地址:https://www.cnblogs.com/BigTall/p/47959.html