How do i remove all linebreaks?

后端 未结 2 1382
情深已故
情深已故 2020-12-15 22:55

I have something like this:




   
      

t

相关标签:
2条回答
  • 2020-12-15 23:11

    The following transformation:

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

    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>
    

    produces the wanted result:

    txt A
    txt X
    txt Y
    txt B

    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.

    0 讨论(0)
  • 2020-12-15 23:13

    You probably want

     <xsl:strip-space elements="node"/>
    

    explained here. And this article has a lot more details.

    0 讨论(0)
提交回复
热议问题