XSLT - 标记模板以斜体和粗体 XML 元素文本 [英] XSLT - Tokenizing template to italicize and bold XML element text

查看:27
本文介绍了XSLT - 标记模板以斜体和粗体 XML 元素文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 XSLT 中实现了以下标记化模板.

I have the following tokenizing template implemented in my XSLT.

<xsl:template match="sporting_arena/text()[normalize-space()]" name="split">
 <xsl:param name="pText" select="."/>
 <xsl:if test="normalize-space($pText)">
   <li>
     <xsl:call-template name="replace">
       <xsl:with-param name="pText" select="substring-before(concat($pText, ';'), ';')"/>
     </xsl:call-template>
   </li>
   <xsl:call-template name="split">
     <xsl:with-param name="pText" select="substring-after($pText, ';')"/>
   </xsl:call-template>
 </xsl:if>

<xsl:template name="replace">
   <xsl:param name="pText"/>
   <xsl:if test="normalize-space($pText)">
       <xsl:value-of select="substring-before(concat($pText, '*'),'*')"/>
       <xsl:if test="contains($pText, '*')">
           <br/>
         <xsl:call-template name="replace">
           <xsl:with-param name="pText" select="substring-after($pText, '*')"/>
         </xsl:call-template>
       </xsl:if>
    </xsl:if>
</xsl:template> 

我想知道是否有可能向这个标记系统添加粗体或斜体的能力,使用相同的分隔符方法在我的 XML 元素文本中使用相同的分隔符方法,在单词的每一侧都有一个分隔符来指示粗体或斜体.

I'm wondering if it is possible to add to this tokenizing system the ability to bold or italicize certain words in my XML element text using the same delimiter approach with a delimiter on each side of a word to indicate either bold or italicized.

当前系统的 XML 示例:

XML Example with current system:

        <sporting_arena>
    Adelaide Oval: An awesome new bike and truck that drove up* a hill and never came back.; 
The delimiter I choose here * places this text on a new line and now I'm;
On a new dot point. 
        </sporting_arena>

推荐答案

我想知道是否有可能在这个标记化系统中添加能够将我的 XML 元素文本中的某些单词加粗或斜体使用相同的分隔符方法,在 a 的每一侧都有一个分隔符表示粗体或斜体的单词."

"I'm wondering if it is possible to add to this tokenizing system the ability to bold or italicize certain words in my XML element text using the same delimiter approach with a delimiter on each side of a word to indicate either bold or italicized."

是的,这是可能的.在这一点上,您应该能够自己实现这一点,使用与标记化模板(您称之为拆分")所使用的原理相同的原理.创建一个标记为

  • 的令牌与创建一个标记为 的令牌没有什么不同.

    Yes, it is possible. And at this point you should be able to implement this yourself, using the same principle as the one utilized by the tokenizing template (which you call "split"). Creating a token tagged as <li> is no different from creating one tagged <b> or <i>.

    回应:

    "我认为有区别:同时将文本分割成 li元素,每个文本片段最终都会在一个列表中结束item 和分隔符 ; 标记列表项应该在哪里结束和另一个立即开始.相反,粗体或斜体标记会仅适用于开头和结尾之间的文本部分分隔符."

    "I think there is a difference: while partitioning the text into li elements, every fragment of text eventually ends up inside a list item, and the delimiter ; marks where a list item should end and another immediately start. Conversely, the bold or italic markup would apply only to a text portion between a starting and an ending delimiter."

    所需的更改相当简单.考虑以下示例:

    The required change is rather trivial. Consider the following example:

    <xsl:template name="italic">
        <xsl:param name="text"/>
        <xsl:param name="delimiter" select="'*'"/>
        <xsl:choose>
            <xsl:when test="contains($text, $delimiter) and contains(substring-after($text, $delimiter), $delimiter)">
                <xsl:value-of select="substring-before($text, $delimiter)"/>
                <i>
                    <xsl:value-of select="substring-before(substring-after($text, $delimiter), $delimiter)"/>
                </i>
                <!-- recursive call -->
                <xsl:call-template name="italic">
                    <xsl:with-param name="text" select="substring-after(substring-after($text, $delimiter), $delimiter)"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$text"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    

    当这个模板被调用为:

    <p>
        <xsl:call-template name="italic">
            <xsl:with-param name="text" select="input"/>
        </xsl:call-template>
    </p>
    

    输入为:

    <input>Lorem ipsum *dolor* sit amet, *consectetuer* adipiscing * elit.</input>
    

    结果将是:

    <p>Lorem ipsum <i>dolor</i> sit amet, <i>consectetuer</i> adipiscing * elit.</p>
    

    请注意,最后一个奇数分隔符按原样传递给输出.

    Note that the last, odd delimiter is passed to the output as is.

    您可以通过参数化要创建的标记元素的名称来概括此模板以处理其他类型的标记(例如粗体).

    You could generalize this template to handle other types of markup (e.g. bold) by parametrizing the name of the token element to create.

    --
    P.S. 该解决方案自豪地使用 xsl:choose.xsl:choose 是 XSLT 语言的一个组成部分,充分利用它绝对没有错.它增加了代码的清晰度,而人为地试图避免使用它只会导致不必要的混淆代码.

    --
    P.S. This solution proudly uses xsl:choose. xsl:choose is an integral part of the XSLT language, and there is absolutely nothing wrong with using it to its full advantage. It adds clarity to the code, while artificial attempts to avoid using it only end up obfuscating the code unnecessarily.

    这篇关于XSLT - 标记模板以斜体和粗体 XML 元素文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆