如何删除所有换行符? [英] How do i remove all linebreaks?

查看:107
本文介绍了如何删除所有换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的东西:

<node TEXT="   txt A   "/>
<node TEXT="

       txt X

"/>
<node>
   <html>
      <p>
        txt Y
      </p>
   </html>
</node>
<node TEXT="txt B"/>

我想使用XSLT来获得此信息:

and i want to use XSLT to get this:

txt A
txt X
txt Y
txt B

我想剥离@TEXT和CDATA的所有无用的空格和换行符.唯一为输出提供结构的XML输入是<node> -tags.

I want to strip all useless whitespaces and linebreaks of @TEXT's and CDATA's. The only XML-input that is giving structure to the output are the <node>-tags.

推荐答案

以下转换:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>

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

<xsl:template match="node/@TEXT | text()">
  <xsl:if test="normalize-space(.)">
    <xsl:value-of select=
     "concat(normalize-space(.), '&#xA;')"/>
  </xsl:if>

  <xsl:apply-templates />
</xsl:template>

</xsl:stylesheet>

当应用于此XML文档时

when applied against this XML document

<t>
<node TEXT="   txt A   "/>
<node TEXT="       txt X"/>
<node>
    <html>
        <p>        txt Y      </p>
    </html>
</node>
<node TEXT="txt B"/>
</t>

产生想要的结果:

txt A
txt X
txt Y
txt B

txt A
txt X
txt Y
txt B

请注意标准XPath函数的使用规范化空间() ,它会去除所有前导和尾随空格,并用一个空格替换其他空格的每个序列.

Do note the use of the standard XPath function normalize-space(), which strips off all leading and trailing spaces and replaces every sequence of other spaces with just one space.

这篇关于如何删除所有换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆