简体   繁体   中英

XSLT: How to get the element parent attribute + using key()

I have some id of node on different place in xml and I want to get name of parent of that node. Probably I should use also key() but i don´t know how to do it.

In more detail: As you can see in code below I have "uml:Model" and "xmi:Extension". In uml:Extension there is connector which has source and target referencing to node under element group. I want to know in which group (uml:Model/packagedElement/packageElement/group) is that source or target as node.

<uml:Model xmi:type="uml:Model" name="EA_Model" visibility="public">
        <packagedElement xmi:type="uml:Package">
            <packagedElement xmi:type="uml:Activity">
                <group xmi:type="uml:ActivityPartition" xmi:id="EAID_BF8E059A_4010_4630_BB60_72A722391509" name="Course">
                    <node xmi:idref="EAID_4E2127CA_F6AD_4898_B2B8_24F0878B557D"/>
                </group>
                <packagedElement xmi:type="uml:Activity" xmi:id="EAID_4E2127CA_F6AD_4898_B2B8_24F0878B557D" name="selectCourse"/>
                <group xmi:type="uml:ActivityPartition" xmi:id="EAID_638BBC87_3987_4c8e_B910_A67FDFF25ABE" name="Course manager" >
                    <node xmi:idref="EAID_61782877_58D5_41e6_B4BA_3C79CC4ADCFF"/>
                </group>
                <packagedElement xmi:type="uml:Activity" xmi:id="EAID_61782877_58D5_41e6_B4BA_3C79CC4ADCFF" name="select driving school"/>
            </packagedElement>
        </packagedElement>
</uml:Model>
<xmi:Extension extender="Enterprise Architect" extenderID="6.5">
        <connectors>
            <connector xmi:idref="EAID_C43E5114_A121_4a58_948C_3F7865CDDE19">
                <source xmi:idref="EAID_61782877_58D5_41e6_B4BA_3C79CC4ADCFF"/>
                <target xmi:idref="EAID_4E2127CA_F6AD_4898_B2B8_24F0878B557D"/>
            </connector>
        </connectors>
</xmi:Extension>

I want to use similar XSLT:

<xsl:template match="xmi:Extension/connectors">
        <xsl:element name="connectors">
           <xsl:for-each select="connector">
               <connector xmi:idref="{@xmi:idref}">
              <source xmi:idref="  <!--{group/@name}  based on node-->  "/>
              <target xmi:idref="  <!--{group/@name}  based on node-->  "/>
        </xsl:element>
     </xsl:template>

For that input I expect this XML output:

<connector xmi:idref="EAID_C43E5114_A121_4a58_948C_3F7865CDDE19">
                <source xmi:idref="Course Manager"/>
                <target xmi:idref="Course"/>
</connector>

So source and target will have in xmi:idref, name of group.

You are right to ask about using a key : it's the best way to resolve cross-references. But you do not need to "get the element parent attribute": you can define the key to target the parent node directly:

<xsl:key name="grp" match="group" use="node/@xmi:idref" />

then do simply:

<xsl:template match="xmi:Extension/connectors">
    <connectors>
        <xsl:for-each select="connector">
            <connector xmi:idref="{@xmi:idref}">
                <source xmi:idref="{key('grp', source/@xmi:idref)/@name}"/>
                <target xmi:idref="{key('grp', target/@xmi:idref)/@name}"/>         
            </connector>
        </xsl:for-each>
    </connectors>
</xsl:template>

Demo : https://xsltfiddle.liberty-development.net/ncdD7mA

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM