Tobi
Tobi

Reputation: 29

XSLT remove element based on attribute (name)

I am struggling with some basic XSLT. I would like to remove an element from some XML depending on whether it has a specific name or not.

Edit: Here is my "real" XML Code:

              <?xml version="1.0" encoding="UTF-8"?>
            <xmi:XMI xmi:version="20131001" xmlns:xmi="http://www.omg.org/spec/XMI/20131001" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:Blocks="http://www.eclipse.org/papyrus/sysml/1.4/SysML/Blocks" xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" xmlns:uml="http://www.eclipse.org/uml2/5.0.0/UML" xsi:schemaLocation="http://www.eclipse.org/papyrus/sysml/1.4/SysML/Blocks http://www.eclipse.org/papyrus/sysml/1.4/SysML#//blocks">
              <uml:Model xmi:id="_fPkgIHI3EemHwJRDr6_Icw" name="Sysml_project">
                <packageImport xmi:type="uml:PackageImport" xmi:id="_fbVrSnI3EemHwJRDr6_Icw">
                  <importedPackage xmi:type="uml:Model" href="pathmap://UML_LIBRARIES/UMLPrimitiveTypes.library.uml#_0"/>
                </packageImport>
                <packageImport xmi:type="uml:PackageImport" xmi:id="_fcFSIHI3EemHwJRDr6_Icw">
                  <importedPackage xmi:type="uml:Package" href="pathmap://SysML14_LIBRARIES/SysML-Standard-Library.uml#SysML.package_packagedElement_Libraries"/>

        <!-- Here I shortened the Code  -->


               </packagedElement>
                <packagedElement xmi:type="uml:Association" xmi:id="_i8Uz0HI5EemHwJRDr6_Icw" memberEnd="_i8ek0nI5EemHwJRDr6_Icw _i8ek1XI5EemHwJRDr6_Icw">
                  <eAnnotations xmi:type="ecore:EAnnotation" xmi:id="_i8ek0HI5EemHwJRDr6_Icw" source="org.eclipse.papyrus">
                    <details xmi:type="ecore:EStringToStringMapEntry" xmi:id="_i8ek0XI5EemHwJRDr6_Icw" key="nature" value="UML_Nature"/>
                  </eAnnotations>
                  <ownedEnd xmi:type="uml:Property" xmi:id="_i8ek1XI5EemHwJRDr6_Icw" name="quelle" type="_ja4qAHI3EemHwJRDr6_Icw" association="_i8Uz0HI5EemHwJRDr6_Icw"/>
                </packagedElement>


<!-- This packagedElement I won´t delete, because here the xmi:type is uml:Class  -->

 <packagedElement xmi:type="uml:Class" xmi:id="_ja4qAHI3EemHwJRDr6_Icw" name="Quelle">
          <ownedAttribute xmi:type="uml:Property" xmi:id="_i8ek0nI5EemHwJRDr6_Icw" name="produktion" type="_xwtDkHI4EemHwJRDr6_Icw" association="_i8Uz0HI5EemHwJRDr6_Icw">
            <lowerValue xmi:type="uml:LiteralInteger" xmi:id="_rF6SoHJQEemHwJRDr6_Icw"/>
            <upperValue xmi:type="uml:LiteralUnlimitedNatural" xmi:id="_i8ek1HI5EemHwJRDr6_Icw" value="*"/>   
</packagedElement>

                </xmi:XMI>

I would like to remove the 'packagedElement' element if the xmi:type is "uml:Association" and also to remove the element, so I would end up with the following:

<root>
    <packagedElement xmi:type="uml:class" name="test">

    </packagedElement>

</root>

Following is my Xsl Code:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <!--identity template copies everything forward by default-->     
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <!--empty template suppresses this attribute-->
  <xsl:template match="packagedElement[@xmi:type='uml:Association']"/>
</xsl:stylesheet>

Upvotes: 0

Views: 534

Answers (1)

Valdi_Bo
Valdi_Bo

Reputation: 31011

If your script refers to xmi namespace, it must be included in the main element of the script. So stylesheet must contain xmlns:xmi="...".

Below you have an example script. I added also xsl:output to switch the indentation on and xsl:strip-space to filter out unnecessary spaces.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xmi="http://www.omg.org/spec/XMI/20131001">
    <xsl:output method="xml" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="packagedElement[@xmi:type='uml:Association']"/>

    <xsl:template match="node()|@*">
        <xsl:copy><xsl:apply-templates select="node()|@*"/></xsl:copy>
    </xsl:template>
</xsl:stylesheet>

I noticed that in a comment to your question that you added xmlns:xmi="omg.org/XMI" to the stylesheet element, but in your XML source there is a longer version, namely xmlns:xmi="http://www.omg.org/spec/XMI/20131001".

I temporarily changed xmlns:xmi to your "shortened" version and the output became different.

So watch not only for inclusion of the required namespaces, but also whether they refer to the same address.

Upvotes: 2

Related Questions