XSLT - 将模板标记为斜体和粗体 XML 元素文本

XSLT - Tokenizing template to italicize and bold XML element text

我在我的 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 元素文本中将某些词加粗或斜体化的能力,在单词的每一侧都有一个定界符表示粗体或斜体。

XML 当前系统示例:

        <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>

"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."

是的,这是可能的。在这一点上,你应该能够自己实现它,使用与标记化模板(你称之为 "split")所使用的相同的原则。创建标记为 <li> 的令牌与创建标记为 <b><i>.

的令牌没有区别

编辑:

回应:

"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."

所需的更改是微不足道的。考虑以下示例:

<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>

请注意,最后一个奇数定界符按原样传递给输出。

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

--
P.S. 这个解决方案自豪地使用了 xsl:choosexsl:choose 是 XSLT 语言不可分割的一部分,充分利用它绝对没有错。它增加了代码的清晰度,而避免使用它的人为尝试最终只会不必要地混淆代码。