我正在尝试将一个 XML 文件转换为另一个文件,但在保持输入文件的相同层次结构时遇到了问题。
这是输入:
<XMI xmi.version='1.2' xmlns:UML="org.omg.xmi.namespace.UML">
<XMI.content>
<UML:Model xmi.id='eee_1045467100313_135436_1' name='Data'>
<UML:Namespace.ownedElement>
<UML:Package xmi.id='_9_0_bc102e5_1427365805826_580042_23' name='migration_kbir'>
<UML:Namespace.ownedElement>
<UML:Package xmi.id='_9_0_bc102e5_1427365805826_580042_22' name='migration_s8ir'>
<UML:Package xmi.id='_9_0_bc102e5_1427365805826_580042_22' name='migration_s8ir2'>
<UML:Class xmi.id='_9_0_bc102e5_1427367042666_255023_151' name='Employee'>
<UML:Classifier.feature>
<UML:Attribute xmi.id='_9_0_bc102e5_1427367052819_893122_168' name='cin'>
</UML:Attribute>
</UML:Classifier.feature>
</UML:Class>
<UML:Class xmi.id='_9_0_64701d4_1429716452808_363115_43' name='AssoEmpl'>
</UML:Class>
<UML:Generalization xmi.id='_9_0_64701d4_1429719509242_100032_198' child='_9_0_64701d4_1429719498101_197360_182' parent='_9_0_64701d4_1429716437842_892182_26' />
</UML:Package>
</UML:Package>
</UML:Namespace.ownedElement>
</UML:Package>
</UML:Namespace.ownedElement>
</UML:Model>
</XMI.content>
</XMI>
这是我得到的输出:
<xmi:XMI xmlns:xmi="http://www.omg.org/XMI" xmlns:UML="org.omg.xmi.namespace.UML"
xmi.version="2.1" timestamp="">
<packagedElement xmi:type="uml:package"
xmi.id="_9_0_bc102e5_1427365805826_580042_23" name="migration_kbir" />
<packagedElement xmi:type="uml:package"
xmi.id="_9_0_bc102e5_1427365805826_580042_22" name="migration_s8ir" />
<packagedElement xmi:type="uml:package"
xmi.id="_9_0_bc102e5_1427365805826_580042_22" name="migration_s8ir2">
<packagedElement xmi:type="uml:Class"
xmi.id="_9_0_bc102e5_1427367042666_255023_151" name="Employee">
<generalization xmi.id="_9_0_bc102e5_1427367042666_255023_151">
<general xmi:type="uml:Class" />
</generalization>
<ownedAttribute xmi.id="_9_0_bc102e5_1427367052819_893122_168"
name="cin" />
</packagedElement>
<packagedElement xmi:type="uml:Class"
xmi.id="_9_0_64701d4_1429716452808_363115_43" name="AssoEmpl">
<generalization xmi.id="_9_0_64701d4_1429716452808_363115_43">
<general xmi:type="uml:Class" />
</generalization>
</packagedElement>
</packagedElement>
</xmi:XMI>
但我期待这样的事情:
<xmi:XMI xmlns:xmi="http://www.omg.org/XMI" xmlns:UML="org.omg.xmi.namespace.UML" xmi.version="2.1" timestamp="">
<packagedElement xmi:type="uml:package" xmi.id="_9_0_bc102e5_1427365805826_580042_23" name="migration_kbir">
<packagedElement xmi:type="uml:package"xmi.id="_9_0_bc102e5_1427365805826_580042_22" name="migration_s8ir">
<packagedElement xmi:type="uml:package"xmi.id="_9_0_bc102e5_1427365805826_580042_22" name="migration_s8ir2">
<packagedElement xmi:type="uml:Class"xmi.id="_9_0_bc102e5_1427367042666_255023_151" name="Employee">
<generalization xmi.id="_9_0_bc102e5_1427367042666_255023_151">
<general xmi:type="uml:Class" />
</generalization>
<ownedAttribute xmi.id="_9_0_bc102e5_1427367052819_893122_168"name="cin" />
</packagedElement>
<packagedElement xmi:type="uml:Class" xmi.id="_9_0_64701d4_1429716452808_363115_43" name="AssoEmpl">
<generalization xmi.id="_9_0_64701d4_1429716452808_363115_43">
<general xmi:type="uml:Class" />
</generalization>
</packagedElement>
</packagedElement>
</packagedElement>
</packagedElement>
</xmi:XMI>
最后这是我的转换文件:
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:UML="org.omg.xmi.namespace.UML"
xmlns:xmi="http://www.omg.org/XMI"
>
<xsl:output indent="yes" method="xml" />
<xsl:template match="/">
<xmi:XMI>
<xsl:attribute name="xmi.version">2.1</xsl:attribute>
<xsl:attribute name="timestamp">
<xsl:value-of select="@timestamp" />
</xsl:attribute>
<xsl:for-each select="//UML:Package">
<packagedElement>
<xsl:attribute name="xmi:type">uml:package</xsl:attribute>
<xsl:attribute name="xmi.id">
<xsl:value-of select="@xmi.id" />
</xsl:attribute>
<xsl:attribute name="name">
<xsl:value-of select="@name" />
</xsl:attribute>
<xsl:choose>
<xsl:when test="UML:Class">
<xsl:for-each select="UML:Class">
<packagedElement>
<xsl:attribute name="xmi:type">uml:Class</xsl:attribute>
<xsl:attribute name="xmi.id">
<xsl:value-of select="@xmi.id" />
</xsl:attribute>
<xsl:attribute name="name">
<xsl:value-of select="@name" />
</xsl:attribute>
<generalization>
<xsl:attribute name="xmi.id">
<xsl:value-of select="@xmi.id" />
</xsl:attribute>
<general xmi:type="uml:Class" />
</generalization>
<xsl:for-each select="UML:Classifier.feature/UML:Attribute">
<ownedAttribute>
<xsl:attribute name="xmi.id">
<xsl:value-of select="@xmi.id" />
</xsl:attribute>
<xsl:attribute name="name">
<xsl:value-of select="@name" />
</xsl:attribute>
</ownedAttribute>
</xsl:for-each>
</packagedElement>
</xsl:for-each>
</xsl:when>
</xsl:choose>
</packagedElement>
</xsl:for-each>
</xmi:XMI>
</xsl:template>
</xsl:stylesheet>
我应该在转换代码中做什么来保持输入文件的层次结构?