首页 > 解决方案 > 在 XSLT 中的节点之间添加空格

问题描述

XML文件 :

     <string-name>
      <surname>Husebo</surname><given-names>BS</given-names>
    </string-name>, <string-name>
                     <surname>Ballard</surname> <given-names>C</given-names></string-name>

XSL : 

    <xsl:template match="surname">
        <xsl:choose>
            <xsl:when test = "following-sibling::node()[not(./*) and normalize-space(.)='']">
            <xsl:text> </xsl:text>
            </xsl:when>
            <xsl:otherwise>
                <xsl:apply-templates/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

输出 : Husebo BS, Ballard C

检查空格后,我想在< surname>< given-name>标记之间添加空格。ex - 首先,在检查是否应该添加空格之后,等等< string-name>之间没有空格。但是在标签中已经存在一个空格,因此不会在那里添加空格。请帮忙 !!!< surname>< given-name>< /surname>2nd < /string-name>

标签: xslt-2.0

解决方案


使用此代码希望它对您有所帮助:

<xsl:template match="surname[not(following-sibling::node()[1][self::text()[. = '&#x0020;']])]">
    <xsl:apply-templates/>
    <xsl:text> </xsl:text>
</xsl:template>

如果您想更具体,请检查紧随其后的兄弟姐妹是否给定名称,然后使用以下代码:

<xsl:template match="surname[not(following-sibling::node()[1][self::text()[. = '&#x0020;']]) and following-sibling::node()[1][self::given-names]]">
    <xsl:apply-templates/>
    <xsl:text> </xsl:text>
</xsl:template>

推荐阅读