The normalize-space function is used in XSLT to clear the Spaces before and after elements

As the straight left

As I understand it, XML files store data, which XSLT is responsible for displaying. The same XML file, combined with different XSLT, can result in very different styles and styles.

Perhaps also because of its power, XSLT has a lot of syntax and functions, but it seems to have little information and is often a labor of application.

One question is, how do you remove the whitespace before and after data taken from XML before parsing it in XSLT? Such as:

<xsl:choose>

<xsl:when test=”.[a=”]” >

<p> Element A is empty </p>

</xsl:when>

<xsl:when test=”.[b=”]” >

<p> Element B is empty </p>

</xsl:when>

<xsl:otherwise>

<p> Elements A and B are not empty </p>

</xsl:otherwise>

</xsl:choose>

 

As a result, data that appears to be empty for a and B, such as <a> </a><b> </b>, all outputs the statement that elements A and B are not empty. Obviously, the Spaces before and after elements are not removed during the comparison.

How can I remove these Spaces? If in C# or whatever, a “Trim” would have flown in and fixed it.

After a long search on the Internet, you can use normalize-space.

But I couldn’t get away with it, saying it didn’t support Normalize-space. When I looked at the XSLT header, I almost spat blood. I was using an older version of the namespace:

<xsl:stylesheet xmlns:xsl=”www.w3.org/TR/WD-xsl”>

Change the namespace to the new version:

The < XSL: stylesheet version = “1.0” XMLNS: XSL = “www.w3.org/1999/XSL/Tr…” >

Problem solved.

<xsl:choose>

<xsl:when test=”normalize-space(a)=”” >

<p> Element A is empty </p>

</xsl:when>

<xsl:when test=”normalize-space(b)=”” >

<p> Element B is empty </p>

</xsl:when>

<xsl:otherwise>

<p> Elements A and B are not empty </p>

</xsl:otherwise>

</xsl:choose>