我有这个XML文件:
<mixed-citation >
<collab>American Indian Research and Policy Institute.</collab>
(<year>2000</year>).
<source>
<bold>To build a bridge: An introduction to working with American Indian communities</bold>
</source>
</mixed-citation>
我有这个XSL:
<xsl:template match="mixed-citation">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="year">
<span class="references__{name()}">
<xsl:apply-templates/>
</span>
</xsl:template>
<xsl:template match="source">
<span class="references__{name()}">
<strong>
<xsl:value-of select="."/>
</strong>
</span>
</xsl:template>
问题是,当应用此XSLT规则时,结果不断生成,而我不知道它来自哪里,这是结果:
美洲印第安人研究与政策研究所(2000)。搭建桥梁:与美洲印第安人社区合作的简介
年份和左括号之间的多余空间,有人可以告诉我它来自哪里,请问如何删除?
答案 0 :(得分:0)
原因是在</collab>
之后,您的源XML实际上
包含一个文本节点,该文本节点包含一个换行符,4个空格和(
(及其后的year
元素)。
这些“不可见”字符在HTML中呈现为单个空格。
一种可能的解决方案是添加一个与text()
匹配的模板,然后
生成带有剥离的初始/终端“白色”字符的输出:
<xsl:template match="text()">
<xsl:value-of select="normalize-space()"/>
</xsl:template>
实际上,normalize-space()
还会更改每个“白色”字符的序列
在文本节点中间插入单个空间。
也许您还应该添加<xsl:strip-space elements="*"/>
在XSLT脚本的开头(实际上是在xsl:output
之后)。
但是请注意,此更改也会删除空格 在 bridge:和简介... 之间。
要保留此空间,必须显式添加。
只需在<xsl:text> </xsl:text>
之前添加<strong>
这样您的输出就可以了。
答案 1 :(得分:0)
如@Valdi_Bo的帖子所述,空格来自换行符。要摆脱换行符,请将此代码添加到xsl文档的顶部:
<xsl:strip-space elements="*"/>
或者如果您要定位特定元素:
<xsl:strip-space elements="mixed-citation"/>
您可以在此处添加节点名称,只需在每个节点名称之后放置一个空格即可。