XSLT html 输出中的换行符

Line break in XSLT html output

我在 xslt html 输出中换行

我的 XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
  <xsl:output method="html" indent="yes"/>

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

<xsl:template match="text()">
    <xsl:value-of select='normalize-space()'/>
  </xsl:template>

  <xsl:template match="//Page">
    <xsl:choose>
      <xsl:when test="@id = 'mainscreen'">
        id = 'mainscreen',
        page = true,
      </xsl:when>
      <xsl:otherwise>no mainscreen</xsl:otherwise>
    </xsl:choose>
    <xsl:choose>
      <xsl:when test="@componentname = 'HRMSDPESS'">
        component name: <xsl:value-of select="@componentname"/>
      </xsl:when>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>

我的输出:

<?xml version="1.0" encoding="utf-8"?>
        id = 'mainscreen',
        page = true,

        component name: HRMSDPESS

这里看到组件名称上面有一个换行符,我想在输出中删除那个换行符。

我试过 normalize-space 函数,但输出没有变化,还有其他方法可以解决上述问题吗?

谢谢, 哈里.

    <xsl:template match="//Page">
<xsl:choose>
<xsl:when test="@id = 'mainscreen'">
    id = 'mainscreen',
    page = true,
</xsl:when>
<xsl:when test="@componentname = 'HRMSDPESS'">
   <xsl:text>component name:</xsl:text> <xsl:value-of select="@componentname"/>
</xsl:when>
<xsl:otherwise>no mainscreen</xsl:otherwise>
</xsl:choose>

这里有一个换行符,因为你在....

中添加了一个
  <xsl:when test="@componentname = 'HRMSDPESS'">
    component name: <xsl:value-of select="@componentname"/>
  </xsl:when>

换行符是 xsl:when> 之后和文本 "component name" 之前的换行符。您可能认为它缩进得很好,但如果文本节点中包含非空白字符,则包括前导空格和换行符在内的所有文本。

解决办法是把文字换行在xsl:text

  <xsl:when test="@componentname = 'HRMSDPESS'">
    <xsl:text>component name: </xsl:text><xsl:value-of select="@componentname"/>
  </xsl:when>

这样,换行符是 "whitespace only" 文本节点的一部分,现在被忽略了。

注意,你的问题说你输出的是html,但是你设置的输出方式是"xml"。此外,您实际上并没有真正输出 html ,而是输出文本。如果您要输出 html,您应该使用 <br /> 标记作为换行符,因为浏览器会自动规范化空格。

或者您可以尝试使用

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>