XSL\C# – 为什么 xsl:template 在输出中添加行

huangapple go评论61阅读模式
英文:

XSL\C# - Why xsl:template add lines in output

问题

I use xslt file to write files in text mode (xsl:output method="text").

This xslt is transform via C# code.

My xslt is like this:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:utils="urn:myExtension" exclude-result-prefixes="msxsl">
    <!-- Declaration of the output format (text) -->
    <xsl:output method="text" indent="no" />

    <!-- Main template -->
    <xsl:template match="/">
        test
    </xsl:template>
</xsl:stylesheet>

The output is like this, with newline and space at the end:

        test

My question is why newline is added to the output? It's as if the "xsl:template" itself and the indent of the xslt are taken into the output.
Is it possible to change this?

I don't want that tags are taken to indent text.

If I write my template like this:
<xsl:template match="/">test</xsl:template>

or like this:

    <xsl:template match="/">
        <xsl:text>test</xsl:text>
    </xsl:template>

It's okay, the result is
test

But I have a lot of "<xsl:value-of select" inside text, so do I have to put "xsl:text" each time I need it?

My C# code is like this:

var xsltSettings = new XsltSettings(false, true);
var transform = new XslCompiledTransform();
transform.Load(xsltFile, xsltSettings, new XmlUrlResolver());

transform.Transform(xmlFile, outputFile);

I tried with an XmlWriter and got the same result:

using (var writer = XmlWriter.Create(outputFile, transform.OutputSettings))
{
    transform.Transform(xmlFile, writer);
}

Hope this is clear.

英文:

I use xslt file to write files in text mode (xsl:output method="text").

This xslt is transform via C# code.

My xslt is like this

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot; xmlns:msxsl=&quot;urn:schemas-microsoft-com:xslt&quot; xmlns:utils=&quot;urn:myExtension&quot; exclude-result-prefixes=&quot;msxsl&quot;&gt;
    &lt;!-- D&#233;claration du format de sortie (texte) --&gt;
    &lt;xsl:output method=&quot;text&quot; indent=&quot;no&quot; /&gt;

    &lt;!-- Mod&#232;le principal --&gt;
    &lt;xsl:template match=&quot;/&quot;&gt;
        test
    &lt;/xsl:template&gt;
&lt;/xsl:stylesheet&gt;

The output is like this, with newline and space at end :


        test
    

My question is why newline are added to the output ? It's like if the "&lt;xsl:template&gt;" itself and the indent of the xslt are take into the output.
Is it possible to change this ?

I don't want that tags are taken to indent text.

If I write my template like this :
&lt;xsl:template match=&quot;/&quot;&gt;test&lt;/xsl:template&gt;

or like this :

    &lt;xsl:template match=&quot;/&quot;&gt;
        &lt;xsl:text&gt;test&lt;/xsl:text&gt;
    &lt;/xsl:template&gt;

It's ok, the result is
test

But I have a lot of "&lt;xsl:value-of select" inside text so do I have to put "&lt;xsl:text&gt;" each time I need ?

My C# is like this :

var xsltSettings = new XsltSettings(false, true);
var transform = new XslCompiledTransform();
transform.Load(xsltFile, xsltSettings, new XmlUrlResolver());

transform.Transform(xmlFile, outputFile);

I tried with a XmlWriter and same result

using (var writer = XmlWriter.Create(outputFile, transform.OutputSettings))
{
    transform.Transform(xmlFile, writer);
}

Hope to be clear.

答案1

得分: 0

一般规则是,样式表中完全由空白组成的文本节点将被忽略,但如果空白是包含可打印文本的文本节点的一部分,则被视为具有重要性。您已经找到了解决方法:使用 xsl:text

英文:

The general rule is that text nodes in a stylesheet that consist entirely of whitespace are ignored, but whitespace which is part of a text node that also contains printable text is treated as being significant. You've already found the remedy: use xsl:text.

huangapple
  • 本文由 发表于 2023年5月18日 00:10:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76274113.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定