如何在 XSLT 中将空白序列替换为一个空格但不进行修剪?

2023-12-13

功能normalize-space删除前导和尾随空白并将空白字符序列替换为单个空格。我怎么能够only在 XSLT 1.0 中用单个空格替换空白字符序列?例如"..x.y...\n\t..z."(为了便于阅读,空格被点替换)应该变成".x.y.z.".


使用此 XPath 1.0 表达式:

concat(substring(' ', 1 + not(substring(.,1,1)=' ')), 
       normalize-space(), 
       substring(' ', 1 + not(substring(., string-length(.)) = ' '))
      )

为了验证这一点,进行以下转换:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="text()">
  <xsl:value-of select=
  "concat(substring(' ', 1 + not(substring(.,1,1)=' ')),
          normalize-space(),
          substring(' ', 1 + not(substring(., string-length(.)) = ' '))
          )
  "/>
 </xsl:template>
</xsl:stylesheet>

当应用于此 XML 文档时:

<t>
 <t1>   xxx   yyy   zzz   </t1>
 <t2>xxx   yyy   zzz</t2>
 <t3>  xxx   yyy   zzz</t3>
 <t4>xxx   yyy   zzz  </t4>
</t>

产生想要的正确结果:

<t>
   <t1> xxx yyy zzz </t1>
   <t2>xxx yyy zzz</t2>
   <t3> xxx yyy zzz</t3>
   <t4>xxx yyy zzz </t4>
</t>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 XSLT 中将空白序列替换为一个空格但不进行修剪? 的相关文章

随机推荐