How to use normalize-space operation in String Function Using XPath in Xml

In this article I will explain normalize-space operation of string function.
  • 3219

Introduction

normalize-space operation/function is a part of string function in XPath. This function takes a string argument and returns the argument string with the leading, trailing, and repeating white spaces stripped, that means after apply the normalize-space the text become normalized with no line breaks, and give a proper sentence. If the argument is not present then this function takes the current node and normalize it.

Syntax

string normalize-space(string)

Hear normalize-space is a function that takes a string type argument and give the normalize way of this argument.

Example

The following example shows how we use the normalize-space function,

Code for normalize.xml

<?xml version="1.0" encoding="utf-8" ?>

<?xml-stylesheet type="text/xsl" href="normal.xslt"?>

<text>

  Hey    Hi,

  How   are   you

  !!!!!!

  Today   In this article

  I explain the   normalize function

  which is   used  to normalize    the text

  ,remove white

  spaces

</text>

 

Code for normal.xslt

 

<?xml version="1.0" encoding="utf-8"?>

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

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

  <xsl:template match="/text">

    Unnormalized:<br></br>

    "<xsl:value-of select='.'/>"

    Normalized: "<xsl:value-of select='normalize-space()'/>"

  </xsl:template>

</xsl:stylesheet>

 

Output
 

When applied to the XML file (normalize.xml), the XSLT style sheet above produces the following output:

 

Unormalized:

"

  Hey    Hi,

  How   are   you

  !!!!!!

  Today   In this article

  I explain the   normalize function

  which is   used  to normalize    the text

  ,remove white

  spaces

"

Normalized:

"Hey Hi, How are you !!!!!! Today In this article I explain the normalize function which is used to normalize the text ,remove white spaces"

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.