Hari
Hari

Reputation: 67

Line break in XSLT html output

I am getting line break in xslt html output

My XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
  <xsl:output method="html" indent="yes"/>

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

<xsl:template match="text()">
    <xsl:value-of select='normalize-space()'/>
  </xsl:template>

  <xsl:template match="//Page">
    <xsl:choose>
      <xsl:when test="@id = 'mainscreen'">
        id = 'mainscreen',
        page = true,
      </xsl:when>
      <xsl:otherwise>no mainscreen</xsl:otherwise>
    </xsl:choose>
    <xsl:choose>
      <xsl:when test="@componentname = 'HRMSDPESS'">
        component name: <xsl:value-of select="@componentname"/>
      </xsl:when>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>

My Output:

<?xml version="1.0" encoding="utf-8"?>
        id = 'mainscreen',
        page = true,

        component name: HRMSDPESS

Here see there is a line break above component name, I want to remove that line break in the output.

I tried normalize-space function but there is no change in output,any other way is there to solve the above issue?

Thanks, Hari.

Upvotes: 1

Views: 1249

Answers (3)

Shane
Shane

Reputation: 790

or you could try using

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

Upvotes: 0

Ajeet Singh
Ajeet Singh

Reputation: 1086

    <xsl:template match="//Page">
<xsl:choose>
<xsl:when test="@id = 'mainscreen'">
    id = 'mainscreen',
    page = true,
</xsl:when>
<xsl:when test="@componentname = 'HRMSDPESS'">
   <xsl:text>component name:</xsl:text> <xsl:value-of select="@componentname"/>
</xsl:when>
<xsl:otherwise>no mainscreen</xsl:otherwise>
</xsl:choose>

Upvotes: 1

Tim C
Tim C

Reputation: 70648

There is a line break there, because you have added one in....

  <xsl:when test="@componentname = 'HRMSDPESS'">
    component name: <xsl:value-of select="@componentname"/>
  </xsl:when>

The linebreak is the one after the > of the xsl:when and before the text "component name". You may think of it is as nicely indented, but if a text node has non-whitespace characters in, then all the text, including leading spaces and linebreaks are included.

The solution is to wrap the text in xsl:text

  <xsl:when test="@componentname = 'HRMSDPESS'">
    <xsl:text>component name: </xsl:text><xsl:value-of select="@componentname"/>
  </xsl:when>

This way, the linebreak is part of a "whitespace only" text node, and is now ignored.

Do note, your question says you are outputting html, but you have set the output method to "xml". Additionally, you are not actually outputting html really, but text. If you were outputting html, you should be using a <br /> tag for linebreaks, as browsers will automatically normalize whitespace.

Upvotes: 3

Related Questions