Html 使用xslt添加逗号

Html 使用xslt添加逗号,html,xml,xslt,Html,Xml,Xslt,这是我的XML结构: <bibliography> <element> <name>name</name> <author><bold>author</bold></author> <title>some <italic>title</italic></title> <page

这是我的XML结构:

<bibliography>
    <element>
        <name>name</name>
        <author><bold>author</bold></author>
        <title>some <italic>title</italic></title>
        <pages>123</pages>
    </element>
    ...
</bibliography>
这是我的全部信息,它们可能有助于找到问题。

更新(最终解决:)


基本上对原始xsl进行了以下更改:

<xsl:template match="bibliography/*/*" priority="0">
        <xsl:if test="position()>2">
            <xsl:text>, </xsl:text>
        </xsl:if>
        <xsl:apply-templates/>
    </xsl:template>
使用
,它将消除问题

有关修剪空间,请参阅:

也可以使用:fn:normalize-space()进行修剪。以下是链接:

和说明:

fn:normalize-space(string)

fn:normalize-space()    
从指定字符串中删除前导空格和尾随空格,并用一个空格替换所有内部空格序列,然后返回结果。如果没有字符串参数,则在当前节点上执行相同操作

Example: normalize-space(' The   XML ')

Result: 'The XML'
将代码更改为:

 <xsl:template match="/*/*/*">
  <xsl:if test="position()>2">, </xsl:if>
  <xsl:value-of select="normalize-space()"/>
 </xsl:template>

, 
结果是:


您的基本错误是使用了
xsl:value of
而不是
xsl:apply-templates
。我也不清楚为什么要使用一个笨拙且低效的结构,比如:

<xsl:template match="/*/*/*">

编辑: 如果您想为除author之外的元素的所有子元素创建一个通用模板,您可以使用以下内容:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="element">
    <xsl:apply-templates/>
</xsl:template>

<xsl:template match="element/*">
    <xsl:apply-templates/>
    <xsl:text> </xsl:text>
</xsl:template>

<xsl:template match="author" priority="1">
    <xsl:apply-templates/>
    <xsl:text>, </xsl:text>
</xsl:template>

<xsl:template match="bold">
    <span style="font-weight:bold;">
        <xsl:apply-templates/>
    </span>
</xsl:template>

<xsl:template match="italic">
    <span style="font-style:italic;">
        <xsl:apply-templates/>
    </span>
</xsl:template>

</xsl:stylesheet>

, 

谢谢,但这会导致:
[1]B.Abadie,关于非交换海森堡流形的K理论,
但我想让它反过来:
[1]B.Abadie,关于非交换海森堡流形的K理论,
。在
中的逗号后添加空格,
此XSLT可以工作,但
标记现在无法工作。我怎样才能解决这个问题?是的,他们成功了。正如您看到的粗体和斜体文本。我更新了我的问题,并添加了处理
标记的部分。我还更新了链接的XSLT文件。也许这有助于你找到问题所在。我不知道为什么它会忽略这些标签。谢谢你迄今为止的帮助@rei0d您可以使用一个模板匹配多个元素:
好的,我回家后再试。谢谢你的帮助。照你说的做,结果是,正如你所看到的,每个元素和逗号(第一条红线)之间仍然有空格,但对于粗体元素似乎是正确的。此外,斜体元素和后面的粗体元素(第二条红线)之间没有逗号。我做错什么了吗?下面是我在这些编辑之后的个人信息。
fn:normalize-space(string)

fn:normalize-space()    
Example: normalize-space(' The   XML ')

Result: 'The XML'
 <xsl:template match="/*/*/*">
  <xsl:if test="position()>2">, </xsl:if>
  <xsl:value-of select="normalize-space()"/>
 </xsl:template>
<xsl:template match="/*/*/*">
<xsl:template match="element">
    <xsl:apply-templates/>
</xsl:template>
<xsl:template match="author">
    <xsl:apply-templates/>
    <xsl:text>, </xsl:text>
</xsl:template>

<xsl:template match="bold">
    <span style="font-weight:bold;">
        <xsl:apply-templates/>
    </span>
</xsl:template>

<xsl:template match="italic">
    <span style="font-style:italic;">
        <xsl:apply-templates/>
    </span>
</xsl:template>
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="element">
    <xsl:apply-templates/>
</xsl:template>

<xsl:template match="element/*">
    <xsl:apply-templates/>
    <xsl:text> </xsl:text>
</xsl:template>

<xsl:template match="author" priority="1">
    <xsl:apply-templates/>
    <xsl:text>, </xsl:text>
</xsl:template>

<xsl:template match="bold">
    <span style="font-weight:bold;">
        <xsl:apply-templates/>
    </span>
</xsl:template>

<xsl:template match="italic">
    <span style="font-style:italic;">
        <xsl:apply-templates/>
    </span>
</xsl:template>

</xsl:stylesheet>