XSLTでのソリューションは、次のようになります。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" encoding="UTF-8" indent="no"/>
<xsl:template match="*|@*">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<!-- trim whitespaces from the content -->
<xsl:template match="text()">
<!-- remove from tag to content -->
<xsl:variable name="trimmedHead" select="replace(.,'^\s+','')"/>
<xsl:variable name="trimmed" select="replace($trimmedHead,'\s+$','')"/>
<xsl:value-of select="$trimmed"/>
</xsl:template>
<!-- do not trim where text content exist -->
<xsl:template match="text()">
<xsl:if test="not(matches(.,'^\s+$'))">
<xsl:value-of select="."/>
</xsl:if>
</xsl:template>
あなたが使用したいテンプレートを選択することができます。最初のものはコンテンツが存在するときにもすべての空白を削除し、2番目のものは空白または改行だけがあるときだけ削除します。
削除する空白をどのように知っていますか?なぜあなたは空白を取り除いているのですか?*いくつかのタイトルですが、*ではありませんか?あなたのルールは何ですか? –
http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 –