XSLT:如何基于元素属性添加条件(使用xsl:if)

时间:2019-03-08 21:49:07

标签: xml xslt uml xslt-1.0 xslt-2.0

我想基于它们的属性创建不同的元素。这是 XML输入:

    <?xml version="1.0" encoding="windows-1252"?>
<xmi:XMI xmi:version="2.1" xmlns:uml="http://schema.omg.org/spec/UML/2.1" xmlns:xmi="http://schema.omg.org/spec/XMI/2.1">
    <xmi:Documentation exporter="Enterprise Architect" exporterVersion="6.5"/>
    <uml:Model xmi:type="uml:Model" name="EA_Model" visibility="public">
        <packagedElement xmi:type="uml:Package" xmi:id="EAPK_9E00FA41_6F28_4e47_9AE6_2A04EE92CDBB" name="aktivityD" visibility="public">
            <packagedElement xmi:type="uml:Activity" xmi:id="EAID_AC000001_6F28_4e47_9AE6_2A04EE92CDBB" name="EA_Activity1" visibility="public">
                <node xmi:type="uml:ActivityFinalNode" xmi:id="EAID_6550597C_95E6_4d78_82C8_1E3DF3916946" name="ActivityFinal" visibility="public">
                    <incoming xmi:idref="EAID_CEB43B7F_ED15_4f03_8D9B_8EF054BD458D"/>
                </node>
                <node xmi:type="uml:InitialNode" xmi:id="EAID_1EE374C2_0853_4379_A09C_1586F1F70618" name="ActivityInitial" visibility="public">
                    <outgoing xmi:idref="EAID_A7A0C176_5CCC_4bf2_AED2_AEB05FB741AF"/>
                </node>
                <edge xmi:type="uml:ControlFlow" xmi:id="EAID_A7A0C176_5CCC_4bf2_AED2_AEB05FB741AF" visibility="public" source="EAID_1EE374C2_0853_4379_A09C_1586F1F70618" target="EAID_6057002D_AC33_4eee_9FF9_9A7F17B830F6"/>
                <group xmi:type="uml:ActivityPartition" xmi:id="EAID_D88F2D66_C7A9_447f_8C87_2D28E2E3906F" name="Partition_1" visibility="public">
                    <node xmi:idref="EAID_6057002D_AC33_4eee_9FF9_9A7F17B830F6"/>
                </group>
                <packagedElement xmi:type="uml:Activity" xmi:id="EAID_6057002D_AC33_4eee_9FF9_9A7F17B830F6" name="Activity1" visibility="public" isReadOnly="false" isSingleExecution="false"/>
                <edge xmi:type="uml:ControlFlow" xmi:id="EAID_CEB43B7F_ED15_4f03_8D9B_8EF054BD458D" visibility="public" source="EAID_6057002D_AC33_4eee_9FF9_9A7F17B830F6" target="EAID_6550597C_95E6_4d78_82C8_1E3DF3916946"/>
            </packagedElement>
        </packagedElement>
    </uml:Model>
</xmi:XMI>

我使用此 XSLT:

...
<xsl:template name="model"  match="uml:Model">
    <xsl:element name="uml:Model">
        <xsl:attribute name="xmi:type">
          <xsl:value-of select="@xmi:type"/>
        </xsl:attribute>
        <xsl:attribute name="name">
          <xsl:value-of select="@name" />
        </xsl:attribute>
        <xsl:attribute name="visibility">
          <xsl:value-of select="@visibility" />
        </xsl:attribute>
        <xsl:apply-templates />
    </xsl:element>
</xsl:template>

<xsl:template match="packagedElement[@xmi:type='uml:Package']">
    <xsl:element name="packagedElement">
        <xsl:attribute name="xmi:type">uml:Package</xsl:attribute>
        <xsl:attribute name="xmi:id">
            <xsl:value-of select="$PackID"></xsl:value-of>
        </xsl:attribute>
        <xsl:attribute name="name">sequenceD</xsl:attribute>
        <xsl:attribute name="visibility">public</xsl:attribute> 
        <xsl:apply-templates/>
    </xsl:element>
</xsl:template>


<xsl:template match="packagedElement[@xml:type='uml:Activity']">
    <xsl:element name="packagedElement">
        <xsl:attribute name ="xmi:type">uml:Collaboration</xsl:attribute>
        <xsl:attribute name ="xmi:id">
             <xsl:value-of select="@xmi:id"/>
        </xsl:attribute>
        <xsl:attribute name ="name">EA_Collaboration1</xsl:attribute>
        <xsl:attribute name ="visibility">public</xsl:attribute>
    </xsl:element>
</xsl:template>
...

对于这些情况,我想知道如何为XSLT添加一些条件:

  • 如果属性(在输入xml中)xmi:type为“ uml:Package”,则创建该元素(在xslt中作为第二个模板并具有这些属性)
  • 如果属性(在输入xml中)xmi:type是“ uml:Activity”创建元素,该元素将位于第一个packagedElement下,它也将是packagedElement但具有不同的属性

目前,这是输出XML

<?xml version="1.0" encoding="windows-1252"?>
<xmi:XMI xmlns:uml="http://schema.omg.org/spec/UML/2.1"
         xmlns:xmi="http://schema.omg.org/spec/XMI/2.1"
         xmi:version="2.1">
   <xmi:Documentation exporter="Enterprise Architect" exporterVersion="6.5"/>
   <uml:Model xmi:type="uml:Model" name="EA_Model" visibility="public">
      <packagedElement xmi:type="uml:Package"
                       xmi:id="EAPK_1234567"
                       name="sequenceD"
                       visibility="public"/>
   </uml:Model>
</xmi:XMI>

我也不知道如何在输出XML中将文本格式化为一行。 对于任何帮助:谢谢!

这是我要创建的输出XML:

<?xml version="1.0" encoding="windows-1252"?>
<xmi:XMI xmi:version="2.1" xmlns:uml="http://schema.omg.org/spec/UML/2.1" xmlns:xmi="http://schema.omg.org/spec/XMI/2.1">
    <xmi:Documentation exporter="Enterprise Architect" exporterVersion="6.5"/>
    <uml:Model xmi:type="uml:Model" name="EA_Model" visibility="public">
        <packagedElement xmi:type="uml:Package" xmi:id="EAPK_5947BE3D_D2DE_4dc8_A0C0_A9AC446264DA" name="sequenceD" visibility="public">
            <packagedElement xmi:type="uml:Collaboration" xmi:id="EAID_CB000000_E3D_D2DE_4dc8_A0C0_A9AC446264D" name="EA_Collaboration1" visibility="public">
                <ownedBehavior xmi:type="uml:Interaction" xmi:id="EAID_IN000000_E3D_D2DE_4dc8_A0C0_A9AC446264D" name="EA_Interaction1" visibility="public">
                    <lifeline xmi:type="uml:Lifeline" xmi:id="EAID_086AF560_4699_47a6_9E42_508896EE53D0" name="Partition_1" visibility="public" represents="EAID_AT000000_4699_47a6_9E42_508896EE53D0"/>
                    <lifeline xmi:type="uml:Lifeline" xmi:id="EAID_89C15D36_76EF_42c8_8267_27793BA745F0" name="Partition_2" visibility="public" represents="EAID_AT000000_76EF_42c8_8267_27793BA745F0"/>
                    <fragment xmi:type="uml:OccurrenceSpecification" xmi:id="EAID_FR000000_4699_47a6_9E42_508896EE53D0" covered="EAID_086AF560_4699_47a6_9E42_508896EE53D0"/>
                    <fragment xmi:type="uml:OccurrenceSpecification" xmi:id="EAID_FR000001_4699_47a6_9E42_508896EE53D0" covered="EAID_086AF560_4699_47a6_9E42_508896EE53D0"/>
                    <fragment xmi:type="uml:OccurrenceSpecification" xmi:id="EAID_FR000000_76EF_42c8_8267_27793BA745F0" covered="EAID_89C15D36_76EF_42c8_8267_27793BA745F0"/>
                    <fragment xmi:type="uml:OccurrenceSpecification" xmi:id="EAID_FR000001_76EF_42c8_8267_27793BA745F0" covered="EAID_89C15D36_76EF_42c8_8267_27793BA745F0"/> 
                    <message xmi:type="uml:Message" xmi:id="EAID_FB6F7794_17CF_42af_870B_4BE2E8CCA386" name="Activity1" messageKind="found" messageSort="synchCall" receiveEvent="EAID_FR000000_4699_47a6_9E42_508896EE53D0"/>
                    <message xmi:type="uml:Message" xmi:id="EAID_F92A2B3B_C28E_43d5_BDDB_78AD13663BF1" name="Activity2" messageKind="complete" messageSort="synchCall" sendEvent="EAID_FR000001_4699_47a6_9E42_508896EE53D0" receiveEvent="EAID_FR000000_76EF_42c8_8267_27793BA745F0"/>
                    <message xmi:type="uml:Message" xmi:id="EAID_BCF881AC_1967_429c_85C3_75487A34866A" messageKind="lost" messageSort="synchCall" sendEvent="EAID_FR000001_76EF_42c8_8267_27793BA745F0"/>
                </ownedBehavior>
                <ownedAttribute xmi:type="uml:Property" xmi:id="EAID_AT000000_4699_47a6_9E42_508896EE53D0"/>
                <ownedAttribute xmi:type="uml:Property" xmi:id="EAID_AT000000_76EF_42c8_8267_27793BA745F0"/>
            </packagedElement>
            <packagedElement xmi:type="uml:Class" xmi:id="EAID_B0D14618_9DE7_43fa_989F_36F68263530A" name="ActivityFinal" visibility="public"/>
            <packagedElement xmi:type="uml:Class" xmi:id="EAID_964B5D05_11B6_4ea1_86CD_391C3A60716C" name="ActivityInitial" visibility="public"/>
        </packagedElement>
    </uml:Model>
</xmi:XMI> 

1 个答案:

答案 0 :(得分:2)

这可以使用具有匹配项的模板来实现,这些匹配项考虑了xmi:type属性的值。

<xsl:template name="model" match="uml:Model">
  <xsl:element ... >
    ...
    <xsl:apply-templates />
  </xsl:element>
</xsl:template>

<xsl:template match="packagedElement[@xmi:type='uml:Package']">
  <xsl:element ... >
    ...
    <xsl:apply-templates />
  </xsl:element>
</xsl:template>

<xsl:template match="packagedElement[@xmi:type='uml:Activity']">
  <xsl:element ... >
    ...
  </xsl:element>
</xsl:template>

...