Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting rid of empty lines after deleting nodes using XSLT

Tags:

I am using XSLT to make a very simple transformation in a XML document. I just want to delete all the element nodes with a particular name. It happens that in my source document all these nodes are located at the end of the document, but after the transformation, although the nodes have disappeared as I intended, there are lots of empty lines in their place.

This is strictly a cosmetic issue since I accomplished what I wanted with the transformation, but out of curiosity: how can I get rid of these empty lines ? This is the XSL file I used for the transformation (the element I wanted to remove is named "relations"):

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" />

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

  <xsl:template match="relation"/>

</xsl:stylesheet>
like image 650
Sergio Avatar asked May 22 '12 22:05

Sergio


People also ask

How is XSLT executed?

Execute an XSLT transformation from an XML fileOpen an XML document in the XML editor. Associate an XSLT style sheet with the XML document. Add an xml-stylesheet processing instruction to the XML document. For example, add the following line to the document prolog: <?

What does XSLT mean and give the purpose of XSLT?

XSL (eXtensible Stylesheet Language) is a styling language for XML. XSLT stands for XSL Transformations. This tutorial will teach you how to use XSLT to transform XML documents into other formats (like transforming XML into HTML).


1 Answers

The reason is in the white-space-only text nodes that are immediate siblings to the deleted elements.

Solution: Simply add this XSLT instruction to remove any white-space-only text nodes -- even before the transformation is started:

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

The result may lose indentation -- if so, add this:

 <xsl:output omit-xml-declaration="yes" indent="yes"/>

The complete transformation becomes:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

    <xsl:template match="*">
        <xsl:copy>
            <xsl:copy-of select="@*" />
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="relation"/>
</xsl:stylesheet>

when applied on this XML document (none provided!):

<nums>
  <num>01</num>
  <num>02</num>
  <num>03</num>
  <num>04</num>
  <num>05</num>
  <num>06</num>
  <num>07</num>
  <num>08</num>
  <num>09</num>
  <num>10</num>

  <relation/>
  <relation/>
  <relation/>
  <relation/>
  <relation/>
  <relation/>
  <relation/>
  <relation/>
</nums>

The wanted, correct result (no trailing white-space) is produced:

<nums>
   <num>01</num>
   <num>02</num>
   <num>03</num>
   <num>04</num>
   <num>05</num>
   <num>06</num>
   <num>07</num>
   <num>08</num>
   <num>09</num>
   <num>10</num>
</nums>
like image 127
Dimitre Novatchev Avatar answered Oct 31 '22 14:10

Dimitre Novatchev