使用XSLT和.NET C#VS 2008替换\ r \n换行符

Kiq*_*net 3 .net xslt replace newline

我使用VS 2008,.net 3.5使用XSLT生成页面html.

我有消息,包含\ r \n(换行符)

我在XSL文件中使用它:

<b>Message: </b><xsl:value-of select="Message"/><br/>
Run Code Online (Sandbox Code Playgroud)

我需要<br/>在xsl中替换\ r \n .我看过几个引用,但没有得到我的问题的解决方案:

我在调用转换XSLT之前使用此代码C#,但不对:

 m = m.Replace(@"\r\n", "&#xD;&#xA;");
            m = m.Replace(@"\n", "&#xA;");
            //m = System.Web.HttpUtility.HtmlDecode(m);

            m = m.Replace(@"\r\n", "<br/>");
            m = m.Replace(@"\n", "<br/>");
            msg = "<Exception>"
            + "<Description>" + d + "</Description>"
            + "<Message>" + m + "</Message>"
            + "<DateTime>" + localTimeString + "</DateTime>"
            + "</Exception>";
Run Code Online (Sandbox Code Playgroud)

我使用这个引用,但不是解决方案

用xsl:text解释换行符?

找不到XSLT替换功能

替换功能仅在XSLT 2.0版中可用,而不是在Visual Studio使用的1.0版中.仅仅因为你指定了version ="2.0"并不意味着Visual Studio支持它.

我使用它像最后一个引用,但我得到错误:

 <xsl:call-template name="string-replace-all">
      <xsl:with-param name="text" select="Message"/>
      <xsl:with-param name="replace" select="\r\n"/>
      <xsl:with-param name="by" select="&lt;br/&gt;"/>
 </xsl:call-template>
Run Code Online (Sandbox Code Playgroud)

建议,任何示例代码都有效吗?

Rob*_*bin 8

上面的调用模板看起来没问题,你只需要模板就可以了!

<!-- XSL FILE -->


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        version="1.0">
  <xsl:variable name="_crlf"><xsl:text>
</xsl:text></xsl:variable>
  <xsl:variable name="crlf" select="string($_crlf)"/>
  <xsl:template match="/">

    <xsl:for-each select="//demo">
      Demo:
      <xsl:call-template name="crlf-replace">
    <xsl:with-param name="subject" select="./text()"/>
      </xsl:call-template>
    </xsl:for-each>
  </xsl:template>

  <xsl:template name="crlf-replace">
    <xsl:param name="subject"/>

    <xsl:choose>
      <xsl:when test="contains($subject, $crlf)">
    <xsl:value-of select="substring-before($subject, $crlf)"/><br/>
    <xsl:call-template name="crlf-replace">
      <xsl:with-param name="subject" select="substring-after($subject, $crlf)"/>
    </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
    <xsl:value-of select="$subject"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>


<!-- XML FILE -->

<?xml version="1.0"?>

<demos>
  <demo>xslt is really fun</demo>
  <demo>you quite often use recursion in xslt</demo>
  <demo>so there!</demo>
</demos>
Run Code Online (Sandbox Code Playgroud)


Dim*_*hev 5

这里有两个问题:

  1. 您不应该尝试替换CRLF - 文本中不存在这样的字符串.这样做的原因是任何兼容的XML解析器通过用单个LF(&#xA)替换任何CR + LF组合来规范化文本节点.的W3C XML规范表示:" 为了简化的应用中,随时随地任务的外部解析实体或内部解析的实体的字面实体值中包含的任何文字两个字符序列'##的xD XA’或独立的字面#xD, XML处理器必须将单个字符#xA传递给应用程序.(在解析之前,可以通过在输入上将所有换行符规范化为#xA来方便地生成此行为.) "

  2. 替换不应该是字符串.它应该是一个节点 - <br />.

解决这两个问题很容易:

<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()" name="replaceNL">
  <xsl:param name="pText" select="."/>

  <xsl:choose>
    <xsl:when test="contains($pText, '&#xA;')">
      <xsl:value-of select=
        "substring-before($pText, '&#xA;')"/>
      <br />
      <xsl:call-template name="replaceNL">
        <xsl:with-param name="pText" select=
          "substring-after($pText, '&#xA;')"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$pText"/>
    </xsl:otherwise>
  </xsl:choose>
 </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

当此转换应用于此XML文档时:

<Exception>
 <Description>Quite common error:
 Missing '('
 </Description>
 <Message>Error1001:
 Syntax error 2002
 </Message>
 <DateTime>localTimeString</DateTime>
</Exception>
Run Code Online (Sandbox Code Playgroud)

产生了想要的正确结果:

<Exception>
    <Description>Quite common error:<br/> Missing '('<br/> </Description>
    <Message>Error1001:<br/> Syntax error 2002<br/> </Message>
    <DateTime>localTimeString</DateTime>
</Exception>
Run Code Online (Sandbox Code Playgroud)