RXC
RXC

Reputation: 1233

Remove Blank Lines from XSL output

I have an xsl file which takes xml and produces a tab delimited file.

My problem is that after every line, there is a blank line.

How do I remove the blank line after each line of actual values?

I tried using <xsl:strip-space elements="*"/> but that did not work.

I also have 2 blank lines before all the values and 2 blank lines after all the values.

Here is my xsl file:

    <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:wd="urn:com.workday/bsvc">
    <xsl:output method="text" encoding="utf-8" media-type="text/plain" />
<!--    <xsl:strip-space  elements="*"/> -->

<xsl:variable name="newline"><xsl:text>
</xsl:text></xsl:variable>
<xsl:variable name="tab"><xsl:text>&#x09;</xsl:text></xsl:variable>
    <xsl:template match="/wd:Report_Entry" xml:space="preserve">
<xsl:value-of select="substring(concat(wd:FAO, ''), 1)" /><xsl:value-of select="$tab"/><xsl:value-of select="substring(concat(wd:FAO_REFERENCE_ID, ''), 1, 8)" /><xsl:value-of select="$tab"/><xsl:value-of select="substring(concat(wd:FAO_TYPE, ''), 1)" /><xsl:value-of select="$tab"/><xsl:value-of select="substring(concat(wd:COMPANY/wd:COMPANY_OF_FAO/@wd:Descriptor, ''), 1)" /><xsl:value-of select="$tab"/><xsl:value-of select="substring(concat(wd:COMPANY/wd:COMPANY_OF_FAO/wd:ID[@wd:type='WID'], ''), 1)" /><xsl:value-of select="$tab"/><xsl:value-of select="substring(concat(wd:COMPANY/wd:COMPANY_OF_FAO/wd:ID[@wd:type='Custom_Worktag_1_ID'], ''), 1)" /><xsl:value-of select="$tab"/><xsl:value-of select="substring(concat(wd:COMPANY/wd:COMPANY_OF_FAO_REFERENCE_ID, ''), 1)" /><xsl:value-of select="$tab"/><xsl:value-of select="substring(concat(wd:COMPANY/wd:COMPANY_OF_FAO_TYPE, ''), 1)" /><xsl:value-of select="$tab"/><xsl:value-of select="substring(concat(wd:COST_CENTER/wd:COST_CENTER, ''), 1)" /><xsl:value-of select="$tab"/><xsl:value-of select="substring(concat(wd:COST_CENTER/wd:COST_CENTER_REFERENCE_ID, ''), 1)" /><xsl:value-of select="$tab"/><xsl:value-of select="substring(concat(wd:COST_CENTER/wd:COST_CENTER_TYPE, ''), 1)" /><xsl:value-of select="$tab"/><xsl:value-of select="substring(concat(wd:FUND/wd:FUND, ''), 1)" /><xsl:value-of select="$tab"/><xsl:value-of select="substring(concat(wd:FUND/wd:FUND_REFERENCE_ID, ''), 1)" /><xsl:value-of select="$tab"/>
    </xsl:template>
</xsl:stylesheet>

Any tips on how to make this better? I am using a variable for the tab and I would also use the newline variable but that would only add an extra blank line.

Upvotes: 2

Views: 5833

Answers (1)

Ian Roberts
Ian Roberts

Reputation: 122374

The strip-space only affects whitespace text nodes read from the input XML document. The problem is more than likely the fact that you've put xml:space="preserve" on the <xsl:template match="/wd:Report_Entry"> in the stylesheet. Whitespace-only text nodes are normally ignored in the stylesheet itself (apart from within <xsl:text>), but by adding xml:space="preserve" you've told it to preserve the newlines between the opening <xsl:template> tag and the first <xsl:value-of>, and between the last value-of and the closing </xsl:template>, and thus they get included in the output tree.

If you remove the xml:space then you can also format your XSLT rather more readably. And the concat and substring(..., 1) calls are un-necessary, try something more like this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:wd="urn:com.workday/bsvc">
    <xsl:output method="text" encoding="utf-8" media-type="text/plain" />
    <xsl:strip-space  elements="*"/>

    <xsl:variable name="newline" select="'&#x0A;'" />
    <xsl:variable name="tab" select="'&#x09;'" />

    <xsl:template match="/wd:Report_Entry">
      <xsl:value-of select="wd:FAO" />
      <xsl:value-of select="$tab"/>
      <xsl:value-of select="substring(wd:FAO_REFERENCE_ID, 1, 8)" />
      <xsl:value-of select="$tab"/>
      <xsl:value-of select="wd:FAO_TYPE" />
      <xsl:value-of select="$tab"/>
      <xsl:value-of select="wd:COMPANY/wd:COMPANY_OF_FAO/@wd:Descriptor" />
      <xsl:value-of select="$tab"/>
      <!-- etc. etc. -->
      <xsl:value-of select="wd:FUND/wd:FUND_REFERENCE_ID" />
      <xsl:value-of select="$newline"/>
    </xsl:template>
</xsl:stylesheet>

or even more concise:

    <xsl:template match="/wd:Report_Entry">
      <xsl:value-of select="concat(
          wd:FAO, $tab,
          substring(wd:FAO_REFERENCE_ID, 1, 8), $tab,
          wd:FAO_TYPE, $tab,
          wd:COMPANY/wd:COMPANY_OF_FAO/@wd:Descriptor, $tab,
          .....
          wd:FUND/wd:FUND_REFERENCE_ID, $newline)" />
    </xsl:template>

Depending on the structure of your input XML you may be able to do something more declarative using templates rather than the hard-coded sequence of value-of instructions, it's hard to be more specific without seeing an example of the input.

Upvotes: 3

Related Questions