Reputation: 23
I have an xml which is processed thru a java based sistem.
if any node on the xml contains a space the process will hang or error out.
so if i have this example how can i eliminate the spaces around the text nodes?
It is important to mention that this xpath should be "dinamic" so if the xml changes it will still catch those spaces.
<bookstore>
<book category="cooking">
<title lang="en"> Everyday Italian</title>
<author>Giada De Laurentiis </author>
<year> 2005 </year>
</book>
</bookstore>
Upvotes: 2
Views: 6233
Reputation: 57169
Any place where you use an XPath and you retrieve its value, use normalize-space(YOUREXPR)
, where YOUREXPR
is whatever your current expression is that returns spaces.
It will eliminate trailing and leading spaces, and will collapse any duplicate spaces.
Alternatively, since you said "if any node on the xml contains a space the process will hang or error out.", you can entirely remove all spaces by using translate(YOUREXPR, ' ', '')
, which will turn Everyday Italian
in EverydayItalian
.
If you also want to eliminate newlines and tabs, you can use
translate(YOUREXPR, ' 	

', '')
Update: Based on your comment, it seems like you actually want to clean your file from excessive spaces. In that case, you should use XSLT. Something like the following will create a copy of your file with all excessive spaces in text nodes and attribute nodes removed (untested, but valid, if you are new to XSLT, I suggest to read up on it through any of the many tutorials or books):
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:strip-space elements="*"/>
<xsl:template match="node()">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@*">
<xsl:attribute name="{local-name()}" namespace="{namespace-uri()}">
<xsl:value-of select="normalize-space(.)" />
</xsl:attribute>
</xsl:template>
<xsl:template match="text()">
<xsl:value-of select="normalize-space(.)" />
</xsl:template>
</xsl:template>
Upvotes: 1