XSLT: How to select attribute of element, based on that element has not other attribute?
我想选择元素的属性,但我知道他没有其他不同的属性。为了澄清起见,如果元素没有属性"接收事件",我想选择属性 xmi:id。
这是我的 xml 输入:
1 2 3 4 5 6 7 8 9 10 11 12 | <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 代码:
1 2 3 4 5 | <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 应该是这样的:
1 2 3 | <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"的第 4 条消息的 xmi:id。也许我应该使用 xsl:key,但使用匹配消息???
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <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> |
注意事项:
-
这将输出没有
receiveEvent 属性的第一个<message> 的xmi:id 。如果可能存在多个此类消息,则需要相应地定义行为或模板。 -
最好在匹配表达式的
or 部分加上括号。