Using
normalize-space(/)
When this XPath expression is evaluated, the string value of the node ( /
) document is first created, and this is provided as an argument to the XPath standard function normalize-space()
.
By definition, normalize-space()
returns its argument with the canceled characters of the leading and trailing adjacent spaces, and any intermediate such group of adjacent whitespace is replaced by a single space.
Evaluation of the above XPath expression results in:
"I like to eat." "This is my favorite restaurant." "I will definitely be back"
To exclude quotation marks, we also use the translate()
function :
normalize-space(translate(/,'"', ''))
The result of evaluating this expression :
I love eating out. This is my favorite restaurant. I will definitely be back
Finally, to quote this result, we use the concat()
function :
concat('"', normalize-space(translate(/,'"', '')), '"' )
An evaluation of this XPath expression gives exactly the desired result :
"I love eating out. This is my favorite restaurant. I will definitely be back"
XSLT Based Validation :
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:template match="/"> <xsl:value-of select= "concat('"', normalize-space(translate(/,'"', '')), '"' )"/> </xsl:template> </xsl:stylesheet>
When this conversion is applied to the provided XML document (fixed so that it runs correctly):
<p> "I love eating out." <br /> <br /> "This is my favorite restaurant." <br /> "I will definitely be back" </p>
an XPath expression is evaluated, and the result of this evaluation is copied to the output:
"I love eating out. This is my favorite restaurant. I will definitely be back"