【问题标题】:How do I understand linebreaks in XSLT?我如何理解 XSLT 中的换行符?
【发布时间】:2013-05-23 18:53:11
【问题描述】:

我有一段 XML,如下所示:

<bunch of other things>
<bunch of other things>
<errorLog> error1 \n error2 \n error3 </errorLog>

我想修改此 XML 运行的 XSLT,以在 errors1error3 之后应用换行符。

我可以完全控制 errorLog 的输出或 XSLT 文件的内容,但我不确定如何制作 XML 或 XSLT 以使输出的 HTML 显示换行符。将 XML 输出更改为会导致换行符的某些特殊字符是否更容易,或者我是否修改 XSLT 以将 \n 解释为换行符?

有一个 example on this site 包含类似于我想要的东西,但我的 &lt;errorLog&gt; XSLT 嵌套在另一个模板中,我不确定模板中的模板如何工作。

【问题讨论】:

  • 模板不嵌套。显示你的 xsl 当前的样子。

标签: xslt newline


【解决方案1】:

反斜杠在包括 C 和 Java 在内的许多语言中用作转义字符,但在 XML 或 XSLT 中不使用。如果你把 \n 放在你的样式表中,那不是换行符,它是两个字符反斜杠,后跟“n”。编写换行符的 XML 方式是 &amp;#xa;。但是,如果您在 HTML 中向浏览器发送换行符,它会将其显示为空格。如果希望浏览器显示换行符,则需要发送&lt;br/&gt; 元素。

【讨论】:

    【解决方案2】:

    如果您可以控制 errorLog 元素,那么您也可以在其中使用文字 LF 字符。就 XSLT 而言,它与任何其他字符没有什么不同。

    至于创建带有换行符的 HTML,您需要添加一个 &lt;br/&gt; 元素来代替您在 XML 源代码中的任何标记。如果您可以将每个错误放在一个单独的元素中,那将是最简单的,就像这样

    <errorLog>
      <error>error1</error>
      <error>error2</error>
      <error>error3</error>
    </errorLog>
    

    那么 XSLT 就不必经历拆分文本本身的相当笨拙的过程。

    使用从您的问题中获取的 XML 数据

    <document>
      <bunch-of-other-things/>
      <bunch-of-other-things/>
      <errorLog>error1 \n error2 \n error3</errorLog>
    </document>
    

    这个样式表

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
      <xsl:strip-space elements="*"/>
      <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
    
      <xsl:template match="/document">
        <html>
          <head>
            <title>Error Log</title>
          </head>
          <body>
            <xsl:apply-templates select="*"/>
          </body>
        </html>
      </xsl:template>
    
      <xsl:template match="node()|@*">
        <xsl:copy>
          <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
      </xsl:template>
    
      <xsl:template match="errorLog">
        <p>
          <xsl:call-template name="split-on-newline">
            <xsl:with-param name="string" select="."/>
          </xsl:call-template>
        </p>
      </xsl:template>
    
      <xsl:template name="split-on-newline">
        <xsl:param name="string"/>
        <xsl:choose>
          <xsl:when test="contains($string, '\n')">
            <xsl:value-of select="substring-before($string, '\n')"/>
            <br/>
            <xsl:call-template name="split-on-newline">
              <xsl:with-param name="string" select="substring-after($string, '\n')"/>
            </xsl:call-template>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="$string"/>
            <br/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:template>
    
    </xsl:stylesheet>
    

    会产生这个输出

    <html>
       <head>
          <title>Error Log</title>
       </head>
       <body>
          <bunch-of-other-things/>
          <bunch-of-other-things/>
          <p>error1 <br/> error2 <br/> error3<br/>
          </p>
       </body>
    </html>
    

    【讨论】:

    • 这很有意义。我可以在它自己的错误元素中生成每个错误,就像在你的第一个示例中一样。完成后,我应该如何更改 xsl 以在每个错误元素之间创建换行符?
    猜你喜欢
    • 1970-01-01
    • 2016-05-29
    • 1970-01-01
    • 1970-01-01
    • 2021-07-23
    • 1970-01-01
    • 2013-06-29
    • 1970-01-01
    • 2014-04-08
    相关资源
    最近更新 更多