In XSLT, The normalize-space function is used to clear leading and trailing spaces of elements.

Source: Internet
Author: User
Tags xsl xslt

In XSLT, The normalize-space function is used to clear leading and trailing spaces of elements.

Zuo zhiquan

According to my understanding, XSLT is responsible for displaying data stored in XML files. The Style and style displayed by the results of the same XML file combined with different XSLT can be quite different.

It may also be because of its powerful functions. XSLT has many syntaxes and functions, but it seems that there are very few materials to introduce and it is often labor-intensive to apply it.

Now there is a question: how do I remove spaces before and after the data obtained from XML before analysis in XSLT? For example:

<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 on the surface of a and B, such as <A> </a> <B> </B>, all output elements A and B are not empty. Obviously, no leading and trailing spaces in the elements are deleted during comparison.

How can I delete these spaces? In C # or other languages, trim flew over to fix it.

I searched the internet and found that I could use the normalize-space function.

However, I failed to try it, saying that normalize-space is not supported. Later, I looked at the XSLT file header and almost vomited blood. I used the old version of namespace:

<XSL: stylesheet xmlns: XSL = "http://www.w3.org/TR/WD-xsl">

Change to the namespace of the new version:

<XSL: stylesheet version = "1.0" xmlns: XSL = "http://www.w3.org/1999/XSL/Transform">

The problem is 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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.