Search code examples
xmlxsltxmi

select parent of a tag with XSLT


I want to select the parent of an XMI tag in a for-each loop. Here's my input :

<XMI xmi.version='1.2' xmlns:UML="org.omg.xmi.namespace.UML">
    <XMI.content>
        <UML:Model xmi.id='eee_1045467100313_135436_1' name='Data'>
            <UML:Namespace.ownedElement>
                <UML:Package xmi.id='_9_0_bc102e5_1427365805826_580042_23'
                    name='migration2'>

                <UML:Package xmi.id='_9_0_bc102e5_1427365805826_580042_22'
                    name='migration'>
                    <UML:Class xmi.id='_9_0_bc102e5_1427367042666_255023_151'
                        name='Employee'>
                        <UML:Classifier.feature>
                            <UML:Attribute xmi.id='_9_0_bc102e5_1427367052819_893122_168'
                                name='cin'>
                            </UML:Attribute>
                        </UML:Classifier.feature>
                    </UML:Class>
                    <UML:Class xmi.id='_9_0_bc102e5_1427367042666_255023_152'
                        name='Employee2'>
                        <UML:Classifier.feature>
                            <UML:Attribute xmi.id='_9_0_bc102e5_1427367052819_893122_169'
                                name='cin2'>
                            </UML:Attribute>
                        </UML:Classifier.feature>
                    </UML:Class>
                    <UML:Class xmi.id='_9_0_bc102e5_1427367042666_255023_153'
                        name='Employee3'>
                        <UML:Classifier.feature>
                            <UML:Attribute xmi.id='_9_0_bc102e5_1427367052819_893122_170'
                                name='cin3'>
                            </UML:Attribute>
                        </UML:Classifier.feature>
                    </UML:Class>

                </UML:Package>
              </UML:Package>
            </UML:Namespace.ownedElement>
        </UML:Model>
    </XMI.content>
</XMI>

so to select the UML:Package with XSLT I use the select option of the for-each loop :

<xsl:for-each
                    select="XMI/XMI.content/UML:Model/UML:Namespace.ownedElement/UML:Package">
                    <packagedElement>
                        <xsl:attribute name="xmi:type">uml:package</xsl:attribute>
                        <xsl:attribute name="xmi.id">
                        <xsl:value-of select='@xmi.id' />
                    </xsl:attribute>
                        <xsl:attribute name="name">
                        <xsl:value-of select='@name' />
                    </xsl:attribute>
</xsl:for-each>

Is there any possibility to replace the

select="XMI/XMI.content/UML:Model/UML:Namespace.ownedElement/UML:Package"

PS : I've tried */UML:Package and ../UML:Package but it didn't work out . any help ?


Solution

  • For selecting all UML:Package elements, you can simply write

    select="//UML:Package"