XSLT:在文本元素中,如何用空白 space 替换换行符 (<br/>)?
XSLT: in text element, how to replace line break (<br/>) with blank space?
注意:我在 OS X Yosemite.
上使用 xsltproc
XSLT 转换的源内容是 HTML。一些
文本节点包含换行符 (
)。在改造后
内容(XML 文件),我希望将换行符转换为 spaces.
比如我有:
<div class="location">London<br />Hyde Park<br /></div>
我想像这样转换这个元素:
<xsl:element name="location">
<xsl:variable name="location" select="div[@class='location']"/>
<xsl:value-of select="$location"/>
</xsl:element>
发生的情况是
被简单地删除了输出:
<location>LondonHyde Park</location>
我还有其他涉及的模板:
<xsl:template match="node()|script"/>
<xsl:template match="*">
<xsl:apply-templates/>
</xsl:template>
转换此处的
需要什么XSLT操作
到单个 space?
我会使用 xsl:apply-templates
而不是 xsl:value-of
并添加一个模板来处理 <br/>
.
您还需要修改 <xsl:template match="node()|script"/>
,因为 node()
也会选择文本节点。如果需要,您可以将 node()
替换为 processing-instruction()|comment()
,但默认情况下它们不会输出。
这是一个工作示例:
输入
<div class="location">London<br />Hyde Park<br /></div>
XSLT 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="script"/>
<xsl:template match="*">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="div[@class='location']">
<location><xsl:apply-templates/></location>
</xsl:template>
<xsl:template match="br">
<xsl:text> </xsl:text>
</xsl:template>
</xsl:stylesheet>
输出
<location>London Hyde Park </location>
如果您不想要结尾的 space,您可以...
- 将
xsl:apply-templates
放入变量 ($var
) 并在 xsl:value-of
中使用 normalize-space()
。喜欢:<xsl:value-of select="normalize-space($var)"/>
- 更新
br
元素的匹配。喜欢:br[not(position()=last())]
注意:我在 OS X Yosemite.
上使用xsltproc
XSLT 转换的源内容是 HTML。一些
文本节点包含换行符 (
)。在改造后
内容(XML 文件),我希望将换行符转换为 spaces.
比如我有:
<div class="location">London<br />Hyde Park<br /></div>
我想像这样转换这个元素:
<xsl:element name="location">
<xsl:variable name="location" select="div[@class='location']"/>
<xsl:value-of select="$location"/>
</xsl:element>
发生的情况是
被简单地删除了输出:
<location>LondonHyde Park</location>
我还有其他涉及的模板:
<xsl:template match="node()|script"/>
<xsl:template match="*">
<xsl:apply-templates/>
</xsl:template>
转换此处的
需要什么XSLT操作
到单个 space?
我会使用 xsl:apply-templates
而不是 xsl:value-of
并添加一个模板来处理 <br/>
.
您还需要修改 <xsl:template match="node()|script"/>
,因为 node()
也会选择文本节点。如果需要,您可以将 node()
替换为 processing-instruction()|comment()
,但默认情况下它们不会输出。
这是一个工作示例:
输入
<div class="location">London<br />Hyde Park<br /></div>
XSLT 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="script"/>
<xsl:template match="*">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="div[@class='location']">
<location><xsl:apply-templates/></location>
</xsl:template>
<xsl:template match="br">
<xsl:text> </xsl:text>
</xsl:template>
</xsl:stylesheet>
输出
<location>London Hyde Park </location>
如果您不想要结尾的 space,您可以...
- 将
xsl:apply-templates
放入变量 ($var
) 并在xsl:value-of
中使用normalize-space()
。喜欢:<xsl:value-of select="normalize-space($var)"/>
- 更新
br
元素的匹配。喜欢:br[not(position()=last())]