English 中文(简体)
利用XSL产生的超文本清除白空间
原标题:Remove whitespace from HTML generated using XSL

Background

Maintain readable XSL source code while generating HTML without excessive breaks that introduce spaces between sentences and their terminating punctuation. From Rethinking XSLT:

White space in XSLT stylesheets is especially problematic because it serves two purposes: (1) for formatting the XSLT stylesheet itself; and (2) for specifying where whitespace should go in the output of XSLT-processed XML data.

Problem

缩略语

  <xsl:if test="@min-time &lt; @max-time">
    for
    <xsl:value-of select="@min-time" />
    to
    <xsl:value-of select="@max-time" />
    minutes
  </xsl:if>

  <xsl:if test="@setting">
    on <xsl:value-of select="@setting" /> heat
  </xsl:if>
  .

This, for example, generates the following output (with whitespace exactly as shown):

    for
    2
    to
    3
    minutes
  .

所有主要浏览器都生产:

for 2 to 3 minutes .

Nearly flawless, except for the space between the word minutes and the punctuation. The desired output is:

for 2 to 3 minutes.

也许可以通过在XSL模板中删除登革线和新线来消除空间,但这意味着有XSL来源代码。

Workaround

最初,预期产出按变量进行总结,然后写如下:

<xsl:value-of select="normalize-space($step)" />.

在此之前,我试图将<代码>和>;span>要素纳入变量。 <代码><span>ss从来未出现在生成的超文本代码内。 下述法典也不正确:

<xsl:copy-of select="normalize-space($step)" />.

Technical Details

风格已经使用:

<xsl:strip-space elements="*" />
<xsl:output indent="no" ... />

Related

Question

How do you tell the XSLT processor to eliminate that space?

谢谢!

最佳回答

而不是使用<条码> 范围-of 您可以采用身份模板,并增加一个模板,将空间从案文节点上分出来。 你在第一轮工作中只会产生一个变数。

请:

<li><xsl:apply-templates select="$step" mode="nospace" />.</li>

模板:

<xsl:template match="text()" mode="nospace" priority="1" >
    <xsl:value-of select="normalize-space(.)" />
</xsl:template>

<xsl:template match="node() | @*" mode="nospace">
    <xsl:copy>
        <xsl:apply-templates select="node() | @*" mode="nospace" />
    </xsl:copy>
</xsl:template>
问题回答

<>I>。 这一转变:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="t[@max-time > @min-time]">
  <span>
    <xsl:value-of select=
      "concat( for  , @min-time,   to  , @max-time,   minutes )"/>
  </span>
  <xsl:apply-templates select="@setting"/>
  <xsl:text>.</xsl:text>
 </xsl:template>

 <xsl:template match="@setting">
  <span>
    <xsl:value-of select="concat(  on  , .,   heat )"/>
  </span>
 </xsl:template>
</xsl:stylesheet>

<>strong>在以下XML文件上应用时(未介绍):

<t min-time="2" max-time="3" setting="moderate"/>

produces the wanted, correct result:

<span>for 2 to 3 minutes</span>
<span> on moderate heat</span>.

,由浏览器显示为:

for 2 to 3 minutes on moderate heat.

www.un.org/spanish/ecosoc 当这一XML文件适用同样的转变时:

<t min-time="2" max-time="3"/>

<>strong>again the right, Hope result is produced:

<span>for 2 to 3 minutes</span>.

,由浏览器显示为:

页: 1


www.un.org/spanish/ecosoc II. Layout(视频)解决办法:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:my="my:my" xmlns:gen="gen:gen" xmlns:gen-attr="gen:gen-attr"
 exclude-result-prefixes="my gen">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <my:layout>
  <span>for <gen-attr:min-time/> to <gen-attr:max-time/> minutes</span>
  <gen-attr:setting><span> on <gen:current/> heat</span></gen-attr:setting>
  <gen:literal>.</gen:literal>
 </my:layout>

 <xsl:variable name="vLayout" select="document(  )/*/my:layout/*"/>
 <xsl:variable name="vDoc" select="/"/>

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

 <xsl:template match="/">
  <xsl:apply-templates select="$vLayout">
   <xsl:with-param name="pCurrent" select="$vDoc/*"/>
  </xsl:apply-templates>
 </xsl:template>

 <xsl:template match="gen-attr:*">
  <xsl:param name="pCurrent"/>
  <xsl:value-of select="$pCurrent/@*[name() = local-name(current())]"/>
 </xsl:template>

 <xsl:template match="gen-attr:setting">
  <xsl:param name="pCurrent"/>
  <xsl:variable name="vnextCurrent" select=
   "$pCurrent/@*[name() = local-name(current())]"/>
  <xsl:apply-templates select="node()[$vnextCurrent]">
    <xsl:with-param name="pCurrent" select="$vnextCurrent"/>
  </xsl:apply-templates>
 </xsl:template>

 <xsl:template match="gen:current">
  <xsl:param name="pCurrent"/>
   <xsl:value-of select="$pCurrent"/>
 </xsl:template>

 <xsl:template match="gen:literal">
  <xsl:apply-templates/>
 </xsl:template>
</xsl:stylesheet>

这种转变给我们带来了一种想法,即如何使所希望产出的视觉(灵感)得到反映,并用来源XML文件中的希望数据将其“扩散”。

其结果与第一项解决办法相同。 如果这种转变是“自成一体”的,它将产生许多名称空间——如果排在单独的XML档案中的话,这些空间是无益的,也不会产生。





相关问题
Is this syntatically valid for div?

<div class= clear ></div> /*which comes out to be below one in FF (seen via firebug) */ <div class= clear /> /*is this the last empty div declaration is semantically valid ? */

How to apply pagination in IFrame

I have an IFrame with a Table enclosed within it. I want to display 15 rows of a table and than a Next button must be there to move forward. How to apply pagination for IFrame?

Scala and HTML parsing

How do you load an HTML DOM document into Scala? The XML singleton had errors when trying to load the xmlns tags. import java.net._ import java.io._ import scala.xml._ object NetParse { import ...

热门标签