Jump to content

How to handle spaces in xslt?


shaileshb

Recommended Posts

Hi,I have an xml file as follows:<para>This is para content. This is para content. This is para content. This is para content. This is para content. This is para content. This is para content. This is para content. This is para content. This is para content. This is para content. This is para content. This is para content. This is para content. This is para content. This is para content. This is para content. This is para content. This is para content. <bold>This is <b>bold</b> content. </bold>This is para content. This is para content. <italic>This is italic content.</italic> This is para content. This is para content.</para>I want to remove the space that is before </bold> element and put the space after </bold> element. For example:content. <bold>This is <b>bold</b> content.</bold> This is Please, can I get an xslt code for this.Thanx

Link to comment
Share on other sites

Hi,I have an xml file as follows:<para>This is para content. This is para content. This is para content. This is para content. This is para content. This is para content. This is para content. This is para content. This is para content. This is para content. This is para content. This is para content. This is para content. This is para content. This is para content. This is para content. This is para content. This is para content. This is para content. <bold>This is <b>bold</b> content. </bold>This is para content. This is para content. <italic>This is italic content.</italic> This is para content. This is para content.</para>I want to remove the space that is before </bold> element and put the space after </bold> element. For example:content. <bold>This is <b>bold</b> content.</bold> This is Please, can I get an xslt code for this.Thanx
Please suggest .....
Link to comment
Share on other sites

You are really only dealing with a collection of text() nodes, the <para>, <bold>, etc are actually getting in the way if you attempt to focus on them. Best to focus on the text() nodes (which are the natural children of the other elements)In doing so, we can take a generic approach. Normalize the spaces in every text() node and add a space to the beginning. You may need to tweak for parent nodes you do not want a leading space in.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">		<xsl:template match="/">		<xsl:apply-templates select="//text()" />	</xsl:template>	<xsl:template match="text()">		<xsl:text> </xsl:text>		<xsl:element name="{name(parent::*)}">			<xsl:value-of select="normalize-space(.) "/>		</xsl:element>		</xsl:template></xsl:stylesheet>

Let me know if it works (or not)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...