Search code examples
xmlxsltline

Remove some nodes in XML with XSLT


I have an XML file and want to use XSLT to format it into a new XML file, omitting some information from the original file. Deleting some lines works quite well, but I still have problems with some expressions. The code is very long, which is why I only put some excerpts here.

Here my source XML file:

<?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"/>
    </packageImport>
    <packagedElement xmi:type="uml:Class" xmi:id="_ja4qAHI3EemHwJRDr6_Icw" 

<!-- Here I have shortened the code -->

  <Blocks:Block xmi:id="_jfDYUHI3EemHwJRDr6_Icw" base_Class="_ja4qAHI3EemHwJRDr6_Icw"/>
  <Blocks:Block xmi:id="_lIs2sHI3EemHwJRDr6_Icw" base_Class="_lIjswHI3EemHwJRDr6_Icw"/>
  <Blocks:Block xmi:id="_xwtDkXI4EemHwJRDr6_Icw" base_Class="_xwtDkHI4EemHwJRDr6_Icw"/>
  <Blocks:Block xmi:id="_SkqB0HI5EemHwJRDr6_Icw" base_Class="_SkgQ0HI5EemHwJRDr6_Icw"/>
  <Blocks:Block xmi:id="_Z7qroHI5EemHwJRDr6_Icw" base_Class="_Z7g6oHI5EemHwJRDr6_Icw"/>
</xmi:XMI>

Here my XSL Expression, at the end you can see how I could

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:uml="href://org.omg/UML/1.3"
            xmlns:xmi="http://www.omg.org/XMI" version="1.0">

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

<!-- Copy everything -->
<xsl:template match="*">
    <xsl:copy>
        <xsl:copy-of select="@*" />
        <xsl:apply-templates />
    </xsl:copy>
</xsl:template>


 <!-- in the following there are the expressions how I deleted the other lines  -->


<xsl:template match="lowerValue"/>
<xsl:template match="eAnnotations"/>    
<xsl:template match="type"/>
<xsl:template match="profileApplication"/>

<xsl:template match="uml:Model">
    <xsl:apply-templates />
</xsl:template>
</xsl:stylesheet>

In the end I want to delete the Five rows Blocks:Block and informations who are at the top of the Code like Version, schemaLocation

is this possible ?


Solution

  • 1. For removing any empty node from XML, you can use the following:

    <xsl:template match="*[not(node())]" />
    

    2. For removing some specific empty node like Blocks:Block from XML, you can use:

    <xsl:template match="Blocks:Block[not(node())]" />
    

    Note: To use above, you need to add namespace xmlns:Blocks="http://www.eclipse.org/papyrus/sysml/1.4/SysML/Blocks" in your <xsl:stylesheet> definition.

    3. For removing attributes like version and schemaLocation at the top, update your template as below:

        <!-- Copy everything -->
        <xsl:template match="*">
            <xsl:copy>
                <xsl:copy-of select="@*[not(name() = 'xsi:schemaLocation') and not(name() = 'xmi:version')]"/>
                <xsl:apply-templates />
            </xsl:copy>
        </xsl:template>
    

    See demo here: https://xsltfiddle.liberty-development.net/bnnZWu