【问题标题】:Changing </BR> to newline in XSLT在 XSLT 中将 </BR> 更改为换行符
【发布时间】:2025-12-31 16:20:22
【问题描述】:

我有以下 XSLT:

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:date="http://exslt.org/dates-and-times"
exclude-result-prefixes="date">
<xsl:output method="html" omit-xml-declaration="yes"/>
  <xsl:template match="//Mail">
    <html>
      <body>
      <p>
        <xsl:value-of xml:space="preserve" select="body"/>
      </p>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

我想在“正文”中输入以下文字:

Beste, <BR/><BR/>
Dit is een automatische mail m.b.t. een registratie in MyRiziv voor de groepering '{0}' met RIZIV-nummer {1}:<BR/><BR/>

现在,当我将其插入 XSLT 时,&lt;BR/&gt; 不断出现。我希望它们被换行符替换。

我期望的输出是:

最好的,

Dit is een automatische mail m.b.t. een registratie in MyRiziv voor de 摸索“{0}”遇到了 RIZIV 编号 {1}:

{0} 和 {1} 将替换为实际值。

我使用以下 C# 代码进行转换:

 XmlDocument xmlDoc = new XmlDocument();

        xmlDoc.AppendChild(xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", "yes"));

        XmlNode rootNode = xmlDoc.CreateElement("Mail");

        XmlNode childElement = xmlDoc.CreateElement("salutation");
        childElement.InnerText = GetTranslation("Salutation"); 
        rootNode.AppendChild(childElement);

        childElement = xmlDoc.CreateElement("body");
        switch (emailType)
        {
            case EmailType.GeneralDataUpdated:
                childElement.InnerText = CreateGeneralDataUpdatedBody(newHci, oldHci, requestor);
                break;
        }
        rootNode.AppendChild(childElement);

XslCompiledTransform xsltTransformer = new XslCompiledTransform();

        using (XmlReader xsltRdr = XmlReader.Create(format))
        {
            xsltTransformer.Load(xsltRdr);
        }

        xmlStream.Position = 0;
        StringWriter results = new StringWriter();

        using (XmlReader xmlRdr = XmlReader.Create(xmlStream))
        {
            xsltTransformer.Transform(xmlRdr, new XsltArgumentList(), results);
        }

        return results.ToString();

谁能帮帮我?

【问题讨论】:

  • 我不清楚您期望的 HTML 输出到底是什么。您能否在问题中包含您的预期输出?
  • 是什么阻止您在 xslt 中不添加新行而不是 &lt;br&gt;
  • 您的&lt;br /&gt; 标签实际上是否在您的输入XML 中的&lt;![CDATA[]&gt; 标签内?您确实需要编辑您的问题以显示您的输入。谢谢。
  • @TimC 我发送的输入实际上是:Ceci est un email automatique concernant un enregistrement dans MyInami pour le groupement '{0}' avec numéro INAMI {1}:

    Le {4}, {2} {3} a modifié les données de votre groupement.

    --------- -------------

  • 那么,CreateGeneralDataUpdatedBody 正在创建输入?通过执行innerText,您将其添加为文本,因此您的输入中不会有任何&lt;BR/&gt; 标签,而是一个包含5 个字符的字符串。您可以尝试使用 innerXml,但 CreateGeneralDataUpdatedBody 需要返回格式正确的 XML(即具有单个根元素)。

标签: xslt newline xslt-2.0


【解决方案1】:

使用apply-template 代替value-of 作为正文,以便您可以在输出中处理BR

试试这个:

<xsl:template match="//Mail">
    <html>
        <body>
            <p>
                <xsl:apply-templates select="body"/>
            </p>
        </body>
    </html>
</xsl:template>

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

<xsl:template match="BR">
    <xsl:text>&#xa;</xsl:text>
</xsl:template>

查看http://xsltransform.hikmatu.com/pPgCcoq上的转换

【讨论】:

最近更新 更多