html - 修剪或删除元素内的前导/试用空格
问题描述
我将html转换为xml。我正在努力消除空格。当我使用 normalize() 函数时,空格被删除,但 text 和 element eg 之间的单个空格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/></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>
推荐阅读
- vba - 来自 vba 的 powershell 调用命令交换 mgmt shell,具有标准输出和标准错误检索,并且没有凭据弹出窗口
- jhipster - JHipster problem with frontend/gateway communication
- unit-testing - Exception when attempting to run Cloverage with compojure auth middleware
- build - VS2017-Build 失败,因为编译器不等到生成以前的项目 dll
- sorting - Auto sorting 2 sheets based on 1 of them
- javascript - How to use Sprite objects/animations in Vue Konva?
- java - 无法删除我的 ip_adress 中的前导零
- typescript - 如何在 tasks.json 中指定 TypeScript 的位置?
- c++ - 推导 LValue 引用类型
- sql - 几何和多边形函数之间的区别 - SQL Server