Vinod
Vinod

Reputation: 145

XSLT: Retain span between xml tag after transformation

I have a simple xml and xsl which is as follows:


XML

<p><span>a</span> <span>b</span></p>


XSL

<xsl:variable name="output">
   <xsl:apply-templates select="//p"/>
</xsl:variable>

<xsl:value-of select="$output" />

<xsl:template match="p">
    <xsl:apply-templates/>
</xsl:template>

<xsl:template match="span">
    <xsl:if test="string-length(normalize-space(.)) &gt; 0">
        <xsl:apply-templates/>
    </xsl:if>
</xsl:template>

Please note the space between the closing of first span tag and opening of second span tag.

Currently the output of this transformation gives:


Output

ab

Is there a way in XSLT where in we can retain the space between the span tag after transformation.


Desired Output

a b

Tried specifying xml:space="preserve" in both p and span template match tags. It just puts a lot of space.

Also my scenario doesn't give me option to add a place holder tag which could have been translated to empty whitespace.

Upvotes: 1

Views: 272

Answers (1)

Ian Roberts
Ian Roberts

Reputation: 122364

In the absence of any other overriding templates I would expect that fragment of XSLT to produce your desired result, unless you have an xsl:strip-space instruction at the top level of your stylesheet. If you have one of those declarations then you should remove it, or at least add

<xsl:preserve-space elements="p"/>

to countermand the strip-space for p elements specifically.

Upvotes: 1

Related Questions