Abiodun Adeoye
Abiodun Adeoye

Reputation: 1095

problems in removing white space within text nodes

Am having some issues removing white space within the text nodes. this are the codes i used but still the spaces wouldn't go.

 <xsl:output indent="yes" method="xml"/> 
 <xsl:template match="/">

    <Address>  <xsl:apply-templates/> </Address>
 </xsl:template>

 <xsl:template match="Address/Rowinfo ">

     <xsl:copy>
        <xsl:copy-of select="LocatorDesignator"/>
        <xsl:copy-of select="LocatorName"/>

    </xsl:copy>
    </xsl:template>


 <xsl:template match="Address/Rowinfo/LocatorDesignator">

    <xsl:value-of select = "normalize-space(LocatorDesignator)"/> 
    <xsl:apply-templates/>
     </xsl:template>

 <xsl:template match="Address/Rowinfo/LocatorName">

    <xsl:value-of select = "normalize-space(LocatorName)"/> 
    <xsl:apply-templates/>
 </xsl:template>
 </xsl:stylesheet>

it produces the same result. this is a sample xml data with whites space within its text node.

<Address>
<Rowinfo>
<Locator>mr oga,    Ade  </Locator>
<LocatorDesignator>Dwelling(Part Of),   Null </LocatorDesignator>
</Rowinfo>

my intended output is

   <Locator>mr oga, Ade</Locator>
  <LocatorDesignator>Dwelling(Part Of),Null</LocatorDesignator>

Upvotes: 0

Views: 698

Answers (2)

rene
rene

Reputation: 42414

Your templates are not used

instead of

<xsl:copy-of select="LocatorDesignator"/> 

do

<xsl:apply-templates select="LocatorDesignator"/> 

Or as JWiley suggested, remove the spaces with a translate

<xsl:template match="Address/Rowinfo/LocatorDesignator">  
    <xsl:value-of select = "translate(.,' ', '')"/>  
    <xsl:apply-templates/> 
</xsl:template> 

Or as you have multiple ways to reach Rome:

 <xsl:copy-of select="translate(LocatorDesignator,' ','')"/> 

Upvotes: 0

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243459

A wellknown way of removing unwanted space from a text node is to use the

normalize-space()

function.

Another complementary way of removing away white-space text nodes from the XML document is the XSLT instruction:

<xsl:strip-space elements="*"/>

A third way of removing text nodes that aren't matched by any other, more specific template is by using the following template:

<xsl:template match="text()"/>

This is more powerful (and unwisely used can cause wanted text nodes to be "deleted" in the output).

People typically use a combination of the these three methods for removing unwanted white-space and text nodes.

Upvotes: 2

Related Questions