Xslt 如何使用xsl更改元素中的文本

Xslt 如何使用xsl更改元素中的文本,xslt,Xslt,如何将更改应用于元素中的文本而不丢失其子元素 例如: 我有一个xml,我想对“p”元素中的文本进行更改 <section> <p >Awesome LO</p> <p > Begin with an interesting fact, thought-provoking <keyword>question</keyword> <context>

如何将更改应用于元素中的文本而不丢失其子元素

例如:

我有一个xml,我想对“p”元素中的文本进行更改

<section>
    <p >Awesome LO</p>
    <p >
        Begin with an interesting fact, thought-provoking
        <keyword>question</keyword>
        <context>
            <p type="Key Words Head">Banana</p>
            <p type="Key Words">A tasty treat to eat any time, and good with ice cream – a banana split.</p>
        </context>, or a one sentence scenario to illustrate why the learning object (content) is important.
    </p>
    <p >
        Begin with a definition, if required. Then, provide an example by example view.
    </p>
</section>

太棒了

从一个有趣的事实开始,发人深省 问题 香蕉

随时都可以吃到的美味佳肴,搭配冰激凌很好——香蕉片

,或用一句话的情景来说明学习对象(内容)的重要性。

如果需要,从定义开始。然后,提供一个逐个示例的视图。

所以我的xsl看起来像这样

<xsl:template match="p">
    <xsl:copy>
        <xsl:call-template name="widont-title">
            <xsl:with-param name="text" select="text()" />
        </xsl:call-template>
    </xsl:copy>
</xsl:template>

问题是,当我这样做时,我丢失了“关键字”、“上下文”和“p”中的其他元素。有人能给我指点线索吗?谢谢

<!-- this method puts a non breaking space in the last word of a 'p' if its less than 5 characters-->
<xsl:template name="widont-title">
    <xsl:param name="temp"/>
    <xsl:param name="text"/>
    <xsl:param name="minWidowLength" select="5"/>

    <xsl:choose>
        <xsl:when test="contains($text, ' ')">
            <xsl:variable name="prev" select="substring-before($text,' ')"/>
            <xsl:variable name="before" select="concat($temp,' ',$prev)"/>
            <xsl:variable name="after" select="substring-after($text, ' ')"/>

            <xsl:choose>
                <xsl:when test="contains($after, ' ')">
                    <xsl:call-template name="widont-title">
                        <xsl:with-param name="temp" select="$before"/>
                        <xsl:with-param name="text" select="$after"/>
                    </xsl:call-template>
                </xsl:when>
                <xsl:when test="not(contains($after, ' ')) and string-length(translate($after,'`~!@#$%^\*()-_=+\\|]}[{;:,./?&lt;&gt;','')) &lt; $minWidowLength">

                    <xsl:value-of select="concat($before, '&#160;', $after)" />
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="concat($before, ' ', $after)" />
                </xsl:otherwise>
            </xsl:choose>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$text"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>


目前尚不清楚widont title
模板的作用,以及是否正确执行了该模板(看起来有点太复杂),但问题是该模板应用得太快,无法处理
p
的子元素。

解决方案(使用并覆盖
p/text()
节点)非常简单

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes"/>
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="p/text()">
        <xsl:call-template name="widont-title">
            <xsl:with-param name="text" select="." />
        </xsl:call-template>
    </xsl:template>

         <!-- "widont-title" template omitted for brevity -->

</xsl:stylesheet>

当对提供的XML文档应用上述转换时,任何
p
元素的子元素都正确地出现在输出中