Xslt XSL:有没有一种简单的方法来预防寡妇?

Xslt XSL:有没有一种简单的方法来预防寡妇?,xslt,symphony-cms,Xslt,Symphony Cms,我想打电话给 <xsl:call-template name="widow-fix"> <with-param name="text" select="text"></with-param> </xsl:call-template> 及 及 Lorem ipsum door sit amet,concetetur adipising#elit 您需要“select=text()”,您可以使用其中一个xs:string函数进行替换,但我不会将结

我想打电话给

<xsl:call-template name="widow-fix">
  <with-param name="text" select="text"></with-param>
</xsl:call-template>

Lorem ipsum door sit amet,concetetur adipising#elit


您需要“select=text()”,您可以使用其中一个xs:string函数进行替换,但我不会将结果描述为防止寡妇。

您只需要替换每个文本节点中的最后一个空格:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">


<xsl:template match="p | text()">
    <xsl:apply-templates select="." mode="widow-fix"/>
</xsl:template>


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


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


<xsl:template match="text()[contains(., ' ')]" mode="widow-fix">
    <xsl:call-template name="text">
        <xsl:with-param name="text" select="."/>
    </xsl:call-template>
</xsl:template>


<xsl:template name="text">
    <xsl:param name="text"/>

    <xsl:variable name="substring-before" select="substring-before($text, ' ')"/>
    <xsl:variable name="substring-after"  select="substring-after($text, ' ')"/>

    <xsl:choose>
        <xsl:when test="contains($substring-after, ' ')">
            <xsl:value-of select="$substring-before"/>
            <xsl:text> </xsl:text>

            <xsl:call-template name="text">
                <xsl:with-param name="text" select="$substring-after"/>
            </xsl:call-template>
        </xsl:when>

        <xsl:otherwise>
            <xsl:value-of select="$substring-before"/>
            <!--<xsl:text>&#160;</xsl:text>-->
            <xsl:text>#</xsl:text>
            <xsl:value-of select="$substring-after"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>


</xsl:stylesheet>

#
mode=“widow fix”可以处理保留封闭标记的文本节点和段落

我使用这样的文档作为测试源

<book>
<p>Highly random content</p>
in this book
</book>

高度随机内容

在这本书中
它转换为以下内容

<book>
<p>Highly random#content</p>
in this#book
</book>

高度随机的内容

在这本书中
你能举一个输入字符串的例子吗?
Lorem ipsum dolor sit amet,Concertetur Adipising Elite。
Thx-你是在使用XSLT 1.0还是2.0?我目前使用的是1.0(附带的),很高兴知道这正是你所需要的!这是我在这里的第一个答案:)P.s.我已经稍微简化了mode=“widow fix”中text()的模板。有没有办法让它不插入名称的元素?当前,如果我运行
它将插入
Lorem ipsum
我想要
Lorem ipsum
您可以忽略它,选择此元素中的所有节点:
<p>Lorem ipsum dolor sit amet, consectetur adipiscing#elit.</p>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">


<xsl:template match="p | text()">
    <xsl:apply-templates select="." mode="widow-fix"/>
</xsl:template>


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


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


<xsl:template match="text()[contains(., ' ')]" mode="widow-fix">
    <xsl:call-template name="text">
        <xsl:with-param name="text" select="."/>
    </xsl:call-template>
</xsl:template>


<xsl:template name="text">
    <xsl:param name="text"/>

    <xsl:variable name="substring-before" select="substring-before($text, ' ')"/>
    <xsl:variable name="substring-after"  select="substring-after($text, ' ')"/>

    <xsl:choose>
        <xsl:when test="contains($substring-after, ' ')">
            <xsl:value-of select="$substring-before"/>
            <xsl:text> </xsl:text>

            <xsl:call-template name="text">
                <xsl:with-param name="text" select="$substring-after"/>
            </xsl:call-template>
        </xsl:when>

        <xsl:otherwise>
            <xsl:value-of select="$substring-before"/>
            <!--<xsl:text>&#160;</xsl:text>-->
            <xsl:text>#</xsl:text>
            <xsl:value-of select="$substring-after"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>


</xsl:stylesheet>
<book>
<p>Highly random content</p>
in this book
</book>
<book>
<p>Highly random#content</p>
in this#book
</book>