1

我想编写一个 xslt 文件以在图形文件中传输 xmi 文件。但是我遇到了边缘无法连接正确的源节点和目标节点的问题。我已经尝试了两个星期。但我仍然很困惑。请帮我。太感谢了。

原始代码是:

<?xml version="1.0" encoding="UTF-8"?>
<xml xmlns:xmi="#">
<element xmi:id="BasicElement-Line1" name="Line1" xmi:type="association"/>

<element xmi:id="BasicElement-Line2" name="Line2" xmi:type="association"/>

<element xmi:id="BasicElement-Object1" name="Object1" xmi:type="class">
    <ownedAttribute xmi:type="Property" name="input" type="BasicElement-Object2" association="BasicElement-Line1"/>
    <ownedAttribute xmi:type="Property" name="output" type="BasicElement-Object3" association="BasicElement-Line2"/>
</element>

<element xmi:id="BasicElement-Object2" name="Object2" xmi:type="class">
</element>

<element xmi:id="BasicElement-Object3" name="Object3" xmi:type="class">
</element>
</xml>

我的目标代码是:

<?xml version="1.0" encoding="UTF-8"?>
<xmi xmlns:y="##">
   <edge target="N1002D" source="N1001B" id="N10005">
      <y:PolyLineEdge>
         <y:Arrows target="none" source="none" />
      </y:PolyLineEdge>
   </edge>
   <edge target="N1002D" source="N1001B" id="N10010">
      <y:PolyLineEdge>
         <y:Arrows target="none" source="none" />
      </y:PolyLineEdge>
   </edge>
   <node id="N1001B">
      <y:NodeLabel>BasicElement-Object1</y:NodeLabel>
   </node>
   <node id="N1002D">
      <y:NodeLabel>BasicElement-Object2</y:NodeLabel>
   </node>
   <node id="N10033">
      <y:NodeLabel>BasicElement-Object3</y:NodeLabel>
   </node>
</xmi>

因为未来会有更多的“类”元素。所以我使用“{generate-id()}”来定义节点 ID。但是当我这样做时,我发现边缘找不到源节点和目标节点的方式。所以我已经做了两个星期了,对此一无所知。请帮助我,我真的很感激。

4

1 回答 1

2

我对 XMI 和目标格式并不十分熟悉,但这里有一些内容应该符合您的描述。

资源:

<?xml version="1.0" encoding="UTF-8"?>
<xml xmlns:xmi="#">
    <element xmi:id="BasicElement-Line1" name="Line1" xmi:type="association">
         <ownedEnd xmi:type="Property" type="BasicElement-Object1" association="BasicElement-Line1"/>
    </element>

    <element xmi:id="BasicElement-Line2" name="Line2" xmi:type="association">
        <ownedEnd xmi:type="Property" type="BasicElement-Object1" association="BasicElement-Line2"/>
    </element>

    <element xmi:id="BasicElement-Object1" name="Object1" xmi:type="class">
        <ownedAttribute xmi:type="Property" name="input" type="BasicElement-Object2" association="BasicElement-Line1"/>
        <ownedAttribute xmi:type="Property" name="output" type="BasicElement-Object3" association="BasicElement-Line2"/>
    </element>

    <element xmi:id="BasicElement-Object2" name="Object2" xmi:type="class">
    </element>

    <element xmi:id="BasicElement-Object3" name="Object3" xmi:type="class">
    </element>
</xml>

转换为(将命名空间调整为正确的 uri):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xmi="#" xmlns:y="##"
exclude-result-prefixes="xmi" version="1.0">
<xsl:output indent="yes"/>

<xsl:template match="xml">
    <xmi>
        <xsl:apply-templates select="element"/>
    </xmi>
</xsl:template>

<xsl:template match="element[@xmi:type='class']">
    <node id="{generate-id()}">
        <y:NodeLabel>
            <xsl:value-of select="@xmi:id"/>
        </y:NodeLabel>
        <y:UMLClassNode/>
    </node>
</xsl:template>

<xsl:template match="element[@xmi:type='association']">
    <!-- association name -->
    <xsl:variable name="association" select="ownedEnd/@association"/>
    <!-- id of source -->
    <xsl:variable name="ownedEnd-type" select="ownedEnd/@type"/>
    <!-- using association variable to select the correct id of target -->
    <xsl:variable name="ownedAttribute-type"
        select="//element[@xmi:id = $ownedEnd-type]/ownedAttribute[@association = $association]/@type"/>
    <edge id="{ generate-id() }" 
        source="{ generate-id( /xml/element[@xmi:id = $ownedEnd-type] ) }"
        target="{ generate-id( /xml/element[@xmi:id = $ownedAttribute-type] ) }">
        <y:PolyLineEdge>
            <y:Arrows source="none" target="none"/>
        </y:PolyLineEdge>
    </edge>
</xsl:template>

</xsl:stylesheet>

给你:

<xmi xmlns:y="##">
   <edge id="d0e3" source="d0e13" target="d0e20">
      <y:PolyLineEdge>
         <y:Arrows source="none" target="none"/>
      </y:PolyLineEdge>
   </edge>
   <edge id="d0e8" source="d0e13" target="d0e23">
      <y:PolyLineEdge>
         <y:Arrows source="none" target="none"/>
      </y:PolyLineEdge>
   </edge>
   <node id="d0e13">
       <y:NodeLabel>BasicElement-Object1</y:NodeLabel>
       <y:UMLClassNode/>
   </node>
   <node id="d0e20">
       <y:NodeLabel>BasicElement-Object2</y:NodeLabel>
       <y:UMLClassNode/>
   </node>
   <node id="d0e23">
       <y:NodeLabel>BasicElement-Object3</y:NodeLabel>
       <y:UMLClassNode/>
   </node>
</xmi>
于 2014-04-01T19:49:47.440 回答