提问者:小点点

修剪或移除元素内部的前导空格/测试空格[duplicate]


我将html转换为XML。我正在努力清除这些空间。由于我使用了normalize()函数,空格被移除,但文本和元素之间的单个空格也被移除,例如农业研究的限制根据标准商业实践。下面我定义了我的输入

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

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

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>

请帮助删除一般的空格


共1个答案

匿名用户

这似乎相当有效:

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