修剪或移除元件内部的引导/试验空间

  • 本文关键字:空间 内部 修剪 html xml xslt
  • 更新时间 :
  • 英文 :


I将html转换为xml。我正在努力去除这些空间。当我使用normalize((函数时,空格被移除,但文本和元素之间的单个空格也被移除,例如of<strong>Agricultural</strong>studieslimited<i>according standard commercial</i>practices。下面我定义了我的输入

<html>
<div class="Sec">
<p class="stitle">The need of <strong>              Agricultural             </strong> studies </p>
<div class="subs1">               (a) term for leases               </div>
<div class="subs1">               (b) be limited <i>                 according standard commercial               </i> practices with maximum              </div>
<table class="table"><tr><td><p class="tablepara">                  (1) General Lease                 </p></td>
<td><p class="tablepara">                  49 years                 </p></td></tr>
<tr><td><p class="tablepara">                  General Permit                 </p></td><td/></tr>
<tr><td><p class="tablepara">                  Forest<sup>      1      </sup> Management Agreement                 </p></td>
<td/></tr><tr><td><p class="tablepara">                  (2) Agricultural Lease                 </p></td></tr></table>
</div>
</html> 

我试着用这个xslt

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">
<xsl:output indent="no" omit-xml-declaration="yes" method="html"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>

<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="text()">
<xsl:value-of select="normalize-space()"/>
</xsl:template>

</xsl:stylesheet>

我得到的输出是

<html>
<div class="Sec">
<p class="stitle">The need of<strong>Agricultural</strong>studies</p>
<div class="subs1">(a) term for leases</div>
<div class="subs1">(b) be limited<i>according standard commercial</i>practices with maximum</div>
<table class="table"><tr><td><p class="tablepara">(1) General Lease</p></td><td><p class="tablepara">49 years</p></td></tr>
<tr><td><p class="tablepara">General Permit</p></td><td></td></tr><tr><td><p class="tablepara">Forest<sup>1</sup>Management Agreement</p></td><td></td></tr>
<tr><td><p class="tablepara">(2) Agricultural Lease</p></td></tr></table></div>
</html>

我发现它还删除了文本附近的空格,即<i>元素和<strong>元素周围的空格

of<strong>Agricultural</strong>studies, limited<i>according standard commercial</i>practices

我需要保留的空间

of <strong>Agricultural</strong> studies, limited <i>according standard commercial</i> practices

我的预期输出是

<html>
<div class="Sec">
<p class="stitle">The need of <strong>Agricultural</strong> studies</p>
<div class="subs1">(a) term for leases</div>
<div class="subs1">(b) be limited <i>according standard commercial</i> practices with maximum</div>
<table class="table"><tr><td><p class="tablepara">(1) General Lease</p></td><td><p class="tablepara">49 years</p></td></tr>
<tr><td><p class="tablepara">General Permit</p></td><td></td></tr><tr><td><p class="tablepara">Forest<sup>1</sup> Management Agreement</p></td><td></td></tr>
<tr><td><p class="tablepara">(2) Agricultural Lease</p></td></tr></table></div>
</html>

请有人帮助删除空间一般

这似乎运行得相当好:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">
<xsl:output indent="yes" omit-xml-declaration="yes" method="html"/>

<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text()[preceding-sibling::* and following-sibling::*]">
<xsl:text> </xsl:text>
<xsl:value-of select="normalize-space()" />
<xsl:text> </xsl:text>
</xsl:template>
<xsl:template match="text()[preceding-sibling::*]">
<xsl:text> </xsl:text>
<xsl:value-of select="normalize-space()" />
</xsl:template>
<xsl:template match="text()[following-sibling::*]">
<xsl:value-of select="normalize-space()" />
<xsl:text> </xsl:text>
</xsl:template>
<xsl:template match="text()">
<xsl:value-of select="normalize-space()" />
</xsl:template>
</xsl:stylesheet>

输出(按照您在问题中所做的方式包装,而不是按照XSLT处理器创建的方式包装(:

<html>
<div class="Sec"><p class="stitle">The need of <strong>Agricultural</strong> studies</p>
<div class="subs1">(a) term for leases</div>
<div class="subs1">(b) be limited <i>according standard commercial</i> practices with maximum</div>
<table class="table"><tr><td><p class="tablepara">(1) General Lease</p></td><td><p class="tablepara">49 years</p></td></tr>
<tr><td><p class="tablepara">General Permit</p></td><td></td></tr>
<tr><td><p class="tablepara">Forest <sup>1</sup> Management Agreement</p></td><td></td></tr><tr><td><p class="tablepara">(2) Agricultural Lease</p></td></tr></table></div>
</html>

最新更新