Reputation: 23
I have a xml which I am transforming using an xslt. The xml looks like this
<Function name="Add" returnType="Integer">
<Description>
// Returns the sum of the input parameters
// param [out] result
// param [in] input 1
// param [in] input 2
// return error Ok=0, Warning=1, Error=2
</Description>
</Function>
I want to get rid off the spaces prior to each line under the description tag.
Current Output:
// Returns the sum of the input parameters
// param [out] result
// param [in] input 1
// param [in] input 2
// return error Ok=0, Warning=1, Error=2
Expected output:
// Returns the sum of the input parameters
// param [out] result
// param [in] input 1
// param [in] input 2
// return error Ok=0, Warning=1, Error=2
I have an xslt where I tried translating the input
<xsl:value-of select="translate(Description, ' ','')" />
Unfortunately it removes all the spaces from the Description.
I also tried <xsl:strip-space elements="*"/>
and normalise
<xsl:variable name="description" select="Description"/>
<varoutput>
<xsl:value-of select= "normalize-space($description)"/>
</varoutput>
But no luck. If someone could help me out what can be done.
This is XSl 1.0 as I am using schemas-microsoft-com:xslt
Upvotes: 0
Views: 46
Reputation: 117140
To remove the whitespace at the beginning of each line, you must split the text to individual lines and process each line in turn. To do this in pure XSLT 1.0, you need to use a recursive named template:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Description">
<xsl:copy>
<xsl:call-template name="process-lines">
<xsl:with-param name="text" select="."/>
</xsl:call-template>
</xsl:copy>
</xsl:template>
<xsl:template name="process-lines">
<xsl:param name="text"/>
<xsl:param name="delimiter" select="' '"/>
<xsl:variable name="line" select="substring-before(concat($text, $delimiter), $delimiter)" />
<xsl:variable name="first-char" select="substring(normalize-space($line), 1, 1)" />
<!-- output -->
<xsl:value-of select="$first-char"/>
<xsl:value-of select="substring-after($line, $first-char)"/>
<!-- recursive call -->
<xsl:if test="contains($text, $delimiter)">
<xsl:value-of select="$delimiter"/>
<xsl:call-template name="process-lines">
<xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
When applied to your input example, this will produce:
Result
<?xml version="1.0" encoding="UTF-8"?>
<Function name="Add" returnType="Integer">
<Description>
// Returns the sum of the input parameters
// param [out] result
// param [in] input 1
// param [in] input 2
// return error Ok=0, Warning=1, Error=2
</Description>
</Function>
Upvotes: 0