在不使用 normalize-space 的情况下删除混合内容元素中的 space
removing space in mixed content element without using normalize-space
我的输入是这样的:
<p><span class="someclass"/> Tekst <i>italic</i> etc..</p>
<p> Tekst <i>italic</i> etc..</p>
我希望输出为:
<p><span class="someclass"/>Tekst <i>italic</i> etc..</p>
<p>Tekst <i>italic</i> etc..</p>
如果我在混合内容模型中使用 normalize-space(.),我也会删除 元素前后的 space。
<p><span class="someclass"/>Tekst<i>italic</>etc..</p>
<p>Tekst<i>italic</i>etc..</p>
有解决这个问题的好方法吗?
如果您只想删除 p
元素的第一个 text()
子节点中的前导白色 space,然后使用 XSLT 3.0,您可以使用
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:math="http://www.w3.org/2005/xpath-functions/math"
exclude-result-prefixes="xs math"
version="3.0">
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="p/text()[1]">
<xsl:value-of select="replace(., '^\s+', '')"/>
</xsl:template>
</xsl:stylesheet>
并且使用 XSLT 2.0,您必须拼出 <xsl:mode on-no-match="shallow-copy"/>
作为身份转换模板
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:math="http://www.w3.org/2005/xpath-functions/math"
exclude-result-prefixes="xs math"
version="2.0">
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p/text()[1]">
<xsl:value-of select="replace(., '^\s+', '')"/>
</xsl:template>
</xsl:stylesheet>
我的输入是这样的:
<p><span class="someclass"/> Tekst <i>italic</i> etc..</p>
<p> Tekst <i>italic</i> etc..</p>
我希望输出为:
<p><span class="someclass"/>Tekst <i>italic</i> etc..</p>
<p>Tekst <i>italic</i> etc..</p>
如果我在混合内容模型中使用 normalize-space(.),我也会删除 元素前后的 space。
<p><span class="someclass"/>Tekst<i>italic</>etc..</p>
<p>Tekst<i>italic</i>etc..</p>
有解决这个问题的好方法吗?
如果您只想删除 p
元素的第一个 text()
子节点中的前导白色 space,然后使用 XSLT 3.0,您可以使用
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:math="http://www.w3.org/2005/xpath-functions/math"
exclude-result-prefixes="xs math"
version="3.0">
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="p/text()[1]">
<xsl:value-of select="replace(., '^\s+', '')"/>
</xsl:template>
</xsl:stylesheet>
并且使用 XSLT 2.0,您必须拼出 <xsl:mode on-no-match="shallow-copy"/>
作为身份转换模板
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:math="http://www.w3.org/2005/xpath-functions/math"
exclude-result-prefixes="xs math"
version="2.0">
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p/text()[1]">
<xsl:value-of select="replace(., '^\s+', '')"/>
</xsl:template>
</xsl:stylesheet>