XSLT:如何根据该元素没有其他属性来选择该元素的属性?

时间:2019-04-13 09:59:19

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

我想选择element的属性,但我知道他没有其他不同的属性。为了澄清起见,如果元素没有属性“接收事件”,我想选择属性xmi:id。

这是我的xml输入:


    <packagedElement xmi:type="uml:Package" xmi:id="EAPK_2A526457_CD3B_48f1_8A97_63CD2A91C147" name="loopSequence" visibility="public">
                <packagedElement xmi:type="uml:Collaboration" xmi:id="EAID_CB000000_457_CD3B_48f1_8A97_63CD2A91C14" name="EA_Collaboration1" visibility="public">
                    <ownedBehavior xmi:type="uml:Interaction" xmi:id="EAID_IN000000_457_CD3B_48f1_8A97_63CD2A91C14" name="EA_Interaction1" visibility="public">
                        <message xmi:type="uml:Message" xmi:id="EAID_A42D4D8F_EFB6_4b4e_A861_1FEE1946B30D" name="selectDrivingSchool" messageKind="found" messageSort="synchCall" receiveEvent="EAID_FR000000_F30E_4171_884F_3A3DFE7EB88F"/>
                        <message xmi:type="uml:Message" xmi:id="EAID_67E4B669_8E61_4571_AD23_245CC7F62F4C" name="getCourse" messageKind="complete" messageSort="synchCall" sendEvent="EAID_FR000001_F30E_4171_884F_3A3DFE7EB88F" receiveEvent="EAID_FR000001_07E9_42c1_AFDB_1285EC859B12"/>
                        <message xmi:type="uml:Message" xmi:id="EAID_4B784479_1F3B_425c_A286_B5F0D75E1832" name="selectCourse" messageKind="complete" messageSort="synchCall" sendEvent="EAID_FR000000_07E9_42c1_AFDB_1285EC859B12" receiveEvent="EAID_FR000001_7564_47cf_9C22_DF3A6E7302AD"/>
                        <message xmi:type="uml:Message" xmi:id="EAID_6C98A08D_1BD1_42bf_9662_59F6FE2E4442" name="end" messageKind="lost" messageSort="synchCall" sendEvent="EAID_FR000000_7564_47cf_9C22_DF3A6E7302AD"/>
                    </ownedBehavior>
                </packagedElement>
                <packagedElement xmi:type="uml:Class" xmi:id="EAID_B6F68D66_8F02_4d02_91B5_2BD7B1742A51" name="Activity Final" visibility="public"/>
                <packagedElement xmi:type="uml:Class" xmi:id="EAID_CAFE5DAF_4C2B_47e5_8704_B4A5D3528513" name="Activity Initial" visibility="public"/>
    </packagedElement>

这是我的XSLT代码:

 <xsl:template  match="packagedElement[@xmi:type='uml:Class' and @name='ActivityFinal' or @name='Activity Final']">
        <node xmi:type="uml:ActivityFinalNode" xmi:id= "EAID_ACTIVITY{substring(@xmi:id,14,28)}" name="{@name}" visibility="{@visibility}">
            <incoming xmi:idref=""/>
        </node>
 </xsl:template>

输出的XML应该是这样的:

<node xmi:type="uml:ActivityFinalNode" xmi:id="EAID_ACTIVITY_8F02_4d02_91B5_2BD7B1742A51" name="Activity Final" visibility="public">
    <incoming xmi:idref="EAID_6C98A08D_1BD1_42bf_9662_59F6FE2E4442"/>
</node>

因此,我需要获取没有属性“ receiveEvent”的第四条消息的xmi:id。也许我应该使用xsl:key,但是要有匹配消息???

1 个答案:

答案 0 :(得分:1)

not()函数可以做到这一点。包裹清楚:

<xsl:template match="packagedElement[
    @xmi:type='uml:Class' 
    and (
        @name='ActivityFinal'
        or @name='Activity Final'
    )
]">
    <node
        xmi:type="uml:ActivityFinalNode"
        xmi:id="EAID_ACTIVITY{substring(@xmi:id, 14, 28)}"
        name="{@name}"
        visibility="{@visibility}"
    >
        <incoming xmi:idref="{.//message[not(@receiveEvent)]/@xmi:id}" />
    </node>
</xsl:template>

注意:

  • 这将输出第一个 xmi:id中没有<message>属性的receiveEvent。如果可能存在多个此类消息,则需要相应地定义行为或模板。
  • 最好将括号放在匹配表达式的or部分。