XSLT - 用新节点替换 text() 节点的特定内容

XSLT - replace specific content of the text() node with a new node

我有一个这样的xml,

 <doc>
    <p>Biological<sub>89</sub> bases<sub>4456</sub> for<sub>8910</sub> sexual<sub>4456</sub>
            differences<sub>8910</sub> in<sub>4456</sub> the brain exist in a wide range of
        vertebrate species, including chickens<sub>8910</sub> Recently<sub>8910</sub> the
            dogma<sub>8910</sub> of<sub>4456</sub> hormonal dependence for the sexual
        differentiation of the brain has been challenged.</p>
</doc>

如您所见,<sub> 个节点和 text() 个节点包含在 <p> 个节点中。每个 <sub> 节点末尾,都有一个文本节点,以 space 开头。 (例如:<sub>89</sub> bases:在 'bases' 文本出现之前存在 space。)我需要用节点替换那些特定的 spaces。

所以预期的输出应该是这样的,

<doc>
    <p>Biological<sub>89</sub><s/>bases<sub>4456</sub><s/>for<sub>8910</sub><s/>sexual<sub>4456</sub>
        <s/>differences<sub>8910</sub><s/>in<sub>4456</sub><s/>the brain exist in a wide range of
        vertebrate species, including chickens<sub>8910</sub><s/>Recently<sub>8910</sub><s/>the
        dogma<sub>8910</sub><s/>of<sub>4456</sub><s/>hormonal dependence for the sexual
        differentiation of the brain has been challenged.</p>
</doc>

为此,我可以使用这样的正则表达式,

<xsl:template match="p/text()">
        <xsl:analyze-string select="." regex="(&#x20;)">
            <xsl:matching-substring>
                <xsl:choose>
                    <xsl:when test="regex-group(1)">
                        <s/>
                    </xsl:when>                
                </xsl:choose>
            </xsl:matching-substring>
            <xsl:non-matching-substring>
                <xsl:value-of select="."/>
            </xsl:non-matching-substring>
        </xsl:analyze-string>
    </xsl:template>

但这会向 text() 节点中的每个 space 添加 <s/> 个节点。但我只需要将节点添加到特定的 spaces.

任何人都可以建议我一个方法我该怎么做..

只需像这样更改正则表达式^(&#x20;):它将只匹配文本部分开头的空格。

剪下此 XSL:

<xsl:analyze-string select="." regex="^(&#x20;)">

这是我得到的结果:

<p>Biological<sub>89</sub><s></s>bases<sub>4456</sub><s></s>for<sub>8910</sub><s></s>sexual<sub>4456</sub>
         differences<sub>8910</sub><s></s>in<sub>4456</sub><s></s>the brain exist in a wide range of
         vertebrate species, including chickens<sub>8910</sub><s></s>Recently<sub>8910</sub><s></s>the
         dogma<sub>8910</sub><s></s>of<sub>4456</sub><s></s>hormonal dependence for the sexual
         differentiation of the brain has been challenged.
      </p>

如果您只想匹配以 space 开头且前面有 sub 元素的文本节点,您可以将条件放在模板 match

<xsl:template match="p/text()[substring(., 1, 1) = ' '][preceding-sibling::node()[1][self::sub]]">

如果您只想删除字符串开头的 space,只需简单替换即可。

<xsl:value-of select="replace(., '^\s+', '')" />

试试这个 XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="xml" indent="no" />

    <xsl:template match="p/text()[substring(., 1, 1) = ' '][preceding-sibling::node()[1][self::sub]]">
      <s />
      <xsl:value-of select="replace(., '^\s+', '')" />
    </xsl:template>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>