Strange XML formatting with XSLT stylesheet

I was looking for an XSLT stylesheet that can handle the correct indentation of my xml document and find it really nice at http://www.printk.net/~bds/indent.html .

I hope the author does not blame me for this quote:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" encoding="ISO-8859-1"/>
  <xsl:param name="indent-increment" select="'   '"/>

  <xsl:template name="newline">
    <xsl:text disable-output-escaping="yes">
</xsl:text>
  </xsl:template>

  <xsl:template match="comment() | processing-instruction()">
    <xsl:param name="indent" select="''"/>
    <xsl:call-template name="newline"/>    
    <xsl:value-of select="$indent"/>
    <xsl:copy />
  </xsl:template>

  <xsl:template match="text()">
    <xsl:param name="indent" select="''"/>
    <xsl:call-template name="newline"/>    
    <xsl:value-of select="$indent"/>
    <xsl:value-of select="normalize-space(.)"/>
  </xsl:template>

  <xsl:template match="text()[normalize-space(.)='']"/>

  <xsl:template match="*">
    <xsl:param name="indent" select="''"/>
    <xsl:call-template name="newline"/>    
    <xsl:value-of select="$indent"/>
      <xsl:choose>
       <xsl:when test="count(child::*) > 0">
        <xsl:copy>
         <xsl:copy-of select="@*"/>
         <xsl:apply-templates select="*|text()">
           <xsl:with-param name="indent" select="concat ($indent, $indent-increment)"/>
         </xsl:apply-templates>
         <xsl:call-template name="newline"/>
         <xsl:value-of select="$indent"/>
        </xsl:copy>
       </xsl:when>       
       <xsl:otherwise>
        <xsl:copy-of select="."/>
       </xsl:otherwise>
     </xsl:choose>
  </xsl:template>    
</xsl:stylesheet>

It does almost everything I need, except for one unpleasant thing: it does weird 8 spaces in the segment of the root element of my document (but not XML declarations). The result looks like part of the markup:

<?xml version="1.0" encoding="UTF-8"?>
        <database>
           <books>
              <book id="0">
                 <ISBN value="0123456789"/>
                 <title>Some book title Language</title>
                 <hardcover value="false"/>
                 <price value="40.46"/>
                 <in_stock value="100"/>
                 <annotation>Some annotation</annotation>
              </book>
           </books>
        </database>`

I am new to XSLT technology and have spent a lot of time trying to figure out how to fix it, but so far it has failed. I use the standard javax.xml.transform.Transformer class to go through XSLT transcription. Do you have any idea why this is happening?

+3
source share
1

XSLT- .

, <xsl:output indent="yes"/>:

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

, XML- (, ):

<database>            <books>
<book id="0">
<ISBN value="0123456789"/>
<title>Some book title Language</title>
              <hardcover value="false"/>
 <price value="40.46"/>
   <in_stock value="100"/>
    <annotation>Some annotation</annotation>
   </book></books> </database>

XML, :

<database>
   <books>
      <book id="0">
         <ISBN value="0123456789"/>
         <title>Some book title Language</title>
         <hardcover value="false"/>
         <price value="40.46"/>
         <in_stock value="100"/>
         <annotation>Some annotation</annotation>
      </book>
   </books>
</database>
+5

All Articles