【问题标题】:Remove whitespace from XSL从 XSL 中删除空格
【发布时间】:2011-05-30 03:31:32
【问题描述】:

我希望删除所有空格,以便我的最终代码看起来像一个文本块。

这是我的标题

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml"
doctype-public="-W3CDTD XHTML 1.0 Strict//EN"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
omit-xml-declaration="yes"
encoding="UTF-8"
indent="no" />

这似乎大部分时间都有效,但我遇到了问题。见source

问题区域似乎是

<!-- clinical research coordinator -->
<xsl:template match="clinical-research-coordinators">

<xsl:variable name="id" select="item/@id" />
<xsl:variable name="entry" select="//people/entry[@id=$id]" />

<xsl:value-of select="$entry/display-name" />, 

clinical research coordinator, at 

<xsl:element name="a">

    <xsl:attribute name="href">mailto:<xsl:value-of select="$entry/email" /></xsl:attribute>
    <xsl:attribute name="class">email</xsl:attribute>

    <xsl:value-of select="$entry/email" />

</xsl:element>

or 

<xsl:value-of select="$entry/phone" />  

</xsl:template>

我正在使用 Symphony CSM 生成数据。我只想删除所有空格,但我想保留缩进模式以提高可读性。

【问题讨论】:

标签: xml xslt symphony-cms removing-whitespace


【解决方案1】:

问题区域似乎是

<!-- clinical research coordinator -->
<xsl:template match="clinical-research-coordinators">
    <xsl:variable name="id" select="item/@id" />
    <xsl:variable name="entry" select="//people/entry[@id=$id]" />
    <xsl:value-of select="$entry/display-name" />,   clinical research coordinator, at   
    <xsl:element name="a">
        <xsl:attribute name="href">mailto:
            <xsl:value-of select="$entry/email" />
        </xsl:attribute>
        <xsl:attribute name="class">email</xsl:attribute>
        <xsl:value-of select="$entry/email" />
    </xsl:element>
      or   
    <xsl:value-of select="$entry/phone" />
</xsl:template>

解决办法是

<!-- clinical research coordinator -->
<xsl:template match="clinical-research-coordinators">
    <xsl:variable name="id" select="item/@id" />
    <xsl:variable name="entry" select="//people/entry[@id=$id]" />
    <xsl:value-of select="$entry/display-name" />,   clinical research coordinator, at <xsl:text/>  
    <xsl:element name="a">
        <xsl:attribute name="href">mailto:
            <xsl:value-of select="$entry/email" />
        </xsl:attribute>
        <xsl:attribute name="class">email</xsl:attribute>
        <xsl:value-of select="$entry/email" />
    </xsl:element>
      <xsl:text> or </xsl:text>   
    <xsl:value-of select="$entry/phone" />
</xsl:template>

注意:使用&lt;xsl:text&gt; 指令来消除现有的空白字符并明确指定应该输出的确切文本。

【讨论】:

  • 太棒了! +1 - - 现在我唯一的问题是将 MARKDOWN 格式读取为对象以删除其空格 see.weareinto.com/3kJf
  • @Kirk Srobeck:你还不能投票,但你可以接受答案。 :)
【解决方案2】:

来自http://www.w3.org/TR/xslt#strip

在源文档的树之后 或样式表文档已 构造,但在它之前 否则由 XSLT 处理,一些文本 节点被剥离。一个文本节点是 除非它只包含 空白字符。

这个

",

clinical research coordinator, at

"

"
or

"

不是只有空格的文本节点,那么它们不应该从样式表中删除。

这就是xsl:text 指令的用途。使用:

<xsl:text>, clinical research coordinator, at </xsl:text> 

<xsl:text> or </xsl:text> 

【讨论】:

  • +1 谢谢!我正在评论另一个答案,这是问题的最后一部分。
  • @Kirk Srobeck:我认为这是一个不同的问题。
  • @Kirk Srobeck:我不明白剩下的问题是什么?
  • @Alejandro 这是一个意外,意在将赏金放在另一个问题上:\
  • @Kirk Strobeck:然后你可以标记版主注意(Jeff Atwood quote
猜你喜欢
  • 2011-05-30
  • 2013-04-06
  • 2012-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-13
  • 2011-07-18
  • 1970-01-01
相关资源
最近更新 更多