首页 > 解决方案 > 修剪或删除元素内的前导/试用空格

问题描述

我将html转换为xml。我正在努力消除空格。当我使用 normalize() 函数时,空格被删除,但 text 和 element eg 之间的单个空格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>

请有人帮助一般删除空间

标签: htmlxmlxslt

解决方案


这似乎工作得相当好:

<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>

推荐阅读