XSLT添加不需要的换行符。

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

XSLT adds unwanted linebreaks

问题

I'm trying to generate HTML output with XSLT using the Apache Formatting Objects Processor Version 2.3. Each <text> element in the input file represents a line on a DIN A4 Page. Here's a snippet of the input:

&lt;text&gt;This is a longer text where every line will be written into a span html element and somehow there will&lt;/text&gt;
&lt;text&gt;be added a line break where there is none&lt;/text&gt;

My XSLT template:

&lt;xsl:template match=&quot;text&quot;&gt;
  &lt;div&gt;
    &lt;span&gt;
      &lt;xsl:value-of select=&quot;.&quot;/&gt;
    &lt;/span&gt;
  &lt;/div&gt;
&lt;/xsl:template&gt;

Desired output:

&lt;div&gt;&lt;span&gt;This is a longer text where every line will be written into a span html element and somehow there will&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;be added a line break where there is none&lt;/span&gt;&lt;/div&gt;

Actual output:

&lt;div&gt;&lt;span&gt;This is a longer text where every line will be written into a span html element and
                  somehow there will&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;be added a line break where there is none&lt;/span&gt;&lt;/div&gt;

CSS:

div {
  position: absolute;
  white-space: nowrap;
  border: 1px solid black;
}
span {
  white-space: pre;
}

The issue is that when using white-space: pre; for the span styling, it causes unexpected line breaks in the output.

英文:

Im trying to generate HTML output with xslt and for that i use the Apache Formatting Objects Processor Version 2.3.

Each &lt;text&gt; element in the input file represents a line on a DIN A4 Page so the following input is a sentence that extends over 2 lines:

&lt;text&gt;This is a longer text where every line will be written into a span html element and somehow there will&lt;/text&gt;
&lt;text&gt;be added a line break where there is none&lt;/text&gt;

My XSLT template looks like:

&lt;xsl:template match=&quot;text&quot;&gt;
  &lt;div&gt;
    &lt;span&gt;
		&lt;xsl:value-of select=&quot;.&quot;/&gt;
	&lt;/span&gt;
  &lt;/div&gt;
&lt;/xsl:template&gt;

The desired output is:

&lt;div&gt;&lt;span&gt;This is a longer text where every line will be written into a span html element and somehow there will&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;be added a line break where there is none&lt;/span&gt;&lt;/div&gt;

But what i get is:

&lt;div&gt;&lt;span&gt;This is a longer text where every line will be written into a span html element and
                  somehow there will&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;be added a line break where there is none&lt;/span&gt;&lt;/div&gt;

My css:

div {
  position: absolute;
  white-space: nowrap;
  border: 1px solid black;
}
span {
  white-space: pre;
}

As i am using css for my span styling with the property white-space: pre; this looks pretty weird in the output as the somehow there will is somewhere in between those word of the second line.

答案1

得分: 1

你可以尝试(假设使用XSLT 3处理器,如Saxon 9.8或更高版本)使用&lt;xsl:output method=&quot;html&quot; suppress-indentation=&quot;span&quot;/&gt;

英文:

You could try (assuming an XSLT 3 processor like Saxon 9.8 or later) to use &lt;xsl:output method=&quot;html&quot; suppress-indentation=&quot;span&quot;/&gt;.

答案2

得分: 1

以下是翻译好的部分:

HTML输出方法在XSLT中的规则(https://www.w3.org/TR/xslt-xquery-serialization-31/的第7.3节)中提到:

> 当在数据模型的实例中输出一系列空白字符时,在正常处理空白字符的元素内(但不包括pretextarea等元素),HTML输出方法可以使用任何一系列空白字符来表示它,这些空白字符将被HTML用户代理以相同的方式处理。

这不允许在CSS中使用自定义空白设置。HTML序列化器可以假定多个空白字符(包括换行符)将以与单个空格相同的方式呈现,但在pretextarea等元素中除外。随着CSS的发展,这种假设显然不再合理。

在第7.4.3节中的规则更明确地说明了当indent参数采用默认值yes时允许的内容;当indent=no时,规则可能不太清楚。当然,这是3.1版本,如果您使用的是1.0版本的处理器,那么一切都会更加模糊(请记住,在XSLT 1.0发布时,CSS几乎还没有被发明)。

关于您的问题中提到XSL-FO的引用,我对此感到完全困惑,因为您似乎正在生成HTML输出。我建议使用XSLT 3.0处理器,并将indent=no或使用suppress-indentation参数来抑制特定元素中的空白调整。

英文:

The rules for the HTML output method in XSLT (§7.3 of https://www.w3.org/TR/xslt-xquery-serialization-31/) say:

> When outputting a sequence of whitespace characters in the instance of
> the data model, within an element where whitespace characters are
> treated normally (but not in elements such as pre and textarea), the
> HTML output method MAY represent it using any sequence of whitespace
> characters that will be treated in the same way by an HTML user agent.

This doesn't allow for use of custom white-space settings in the CSS. The HTML serializer is allowed to assume that it multiple whitespace characters (including newlines) are going to be rendered in the same way as a single space, except in elements such as pre and textarea. With the developments that have taken place in CSS, this assumption is clearly no longer justified.

The rules in §7.4.3 spell out more precisely what is allowed when the indent parameter takes its default value of yes; it's less clear exactly what the rules are when indent=no. And of course this is 3.1, if you're using a 1.0 processor then it's all much more vague (remember that CSS had hardly been invented when XSLT 1.0 came out).

I'm completely confused by the references in your question to XSL-FO, given that you seem to be producing HTML output. I would suggest using an XSLT 3.0 processor, and either setting indent=no, or using the suppress-indentation parameter to suppress whitespace adjustment in specific elements.

huangapple
  • 本文由 发表于 2023年5月7日 20:36:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76193977.html
匿名

发表评论

匿名网友

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

确定