pml
pml

Reputation: 77

XSLT - node values shall not be copied

I want to transform some xmi files using XSLT. Everything works fine, but I do not understand why the <body> tag values "Version 1.0" and "EAUML Version:1.0" are copied by my template "packagedElement" (see output).

xmi File:

<?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" xmlns:thecustomprofile="http://www.sparxsystems.com/profiles/thecustomprofile/1.0" xmlns:EAUML="http://www.sparxsystems.com/profiles/EAUML/1.0">
    <uml:Model xmi:type="uml:Model" name="EA_Model" visibility="public">
        <packagedElement xmi:type="uml:Package" name="Test" visibility="public">
            ...
        </packagedElement>
    </uml:Model>
    <xmi:Extension>
        <profiles>
            <uml:Profile 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:id="thecustomprofile" nsPrefix="thecustomprofile" name="thecustomprofile" metamodelReference="mmref01">
                <ownedComment xmi:type="uml:Comment" xmi:id="comment01" annotatedElement="thecustomprofile">
                    <body> Version:1.0</body>
                </ownedComment>
                <packagedElement xmi:type="uml:Stereotype" xmi:id="enum" name="enum"/>
                ...
            </uml:Profile>
            <uml:Profile 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:id="8C9E6706-8" nsPrefix="EAUML" name="EAUML" metamodelReference="mmref01">
                <ownedComment xmi:type="uml:Comment" xmi:id="comment01" annotatedElement="8C9E6706-8">
                    <body>EAUML Version:1.0</body>
                </ownedComment>
                ...
            </uml:Profile>
        </profiles>
    </xmi:Extension>
</xmi:XMI>

XSLT file (code inside <xsl:if> is irrelevant for my problem):

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xmi="http://schema.omg.org/spec/XMI/2.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:uml="http://www.omg.org/spec/UML/20131001"
xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore">

<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>

<xsl:template match="/">
    <ecore:EPackage>
        <xsl:apply-templates mode="packagedElement" />
    </ecore:EPackage>
</xsl:template>

<xsl:template match="packagedElement" mode="packagedElement">
    <xsl:if test="(@xmi:type='uml:Package')">

    </xsl:if>
</xsl:template>

</xsl:stylesheet>

output:

<?xml version="1.0"?>
<ecore:EPackage xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" xmlns:xmi="http://schema.omg.org/spec/XMI/2.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:uml="http://www.omg.org/spec/UML/20131001"> Version:1.0  EAUML Version:1.0 </ecore:EPackage>

Upvotes: 1

Views: 69

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117043

What you see is the result of the built-in template rules being applied to nodes that aren't matched explicitly by any one of your templates.

To solve this, either add a template preventing text nodes being copied by default:

<xsl:template match="text()" mode="packagedElement"/>

or - preferably, IMHO - apply templates more selectively.

Upvotes: 3

Related Questions