【问题标题】:HTML Formatting Partial Content Within XML NodeHTML 格式化 XML 节点中的部分内容
【发布时间】:2010-08-28 06:10:22
【问题描述】:

我有一个 XML 文档,其中有一个名为“描述”的 XML 节点

描述节点包含诸如“这是一个长描述”之类的文本。

当 XML 文件被加载并显示在浏览器中时,我只需要将“long”这个词加成斜体。我知道我可以使用 XSLT 来设置整个 Description 节点的样式,但是如何仅在节点内容的一小部分上使用 CSS 样式?

【问题讨论】:

    标签: html css xml xslt


    【解决方案1】:

    这个 XSLT 1.0 样式表:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:template match="node()|@*">
            <xsl:copy>
                <xsl:apply-templates select="node()|@*"/>
            </xsl:copy>
        </xsl:template>
        <xsl:template match="text()[ancestor::Description]" name="replace">
            <xsl:param name="pString" select="."/>
            <xsl:choose>
                <xsl:when test="contains($pString,' long ')">
                    <xsl:value-of select="substring-before($pString,' long ')"/>
                    <i> long </i>
                    <xsl:call-template name="replace">
                        <xsl:with-param name="pString" 
                        select="substring-after($pString,' long ')"/>
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$pString"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:template>
    </xsl:stylesheet>
    

    有了这个输入:

    <Description>This is a long description</Description>
    

    输出:

    <Description>This is a<i> long </i>description</Description>
    

    还有这个 XSLT 2.0 样式表:

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:template match="node()|@*">
            <xsl:copy>
                <xsl:apply-templates select="node()|@*"/>
            </xsl:copy>
        </xsl:template>
        <xsl:template match="text()[ancestor::Description]">
            <xsl:analyze-string select="." regex="([\p{{P}}\p{{Z}}])long([\p{{P}}\p{{Z}}])">
                <xsl:matching-substring>
                    <xsl:value-of select="regex-group(1)"/>
                    <i>long</i>
                    <xsl:value-of select="regex-group(2)"/>
                </xsl:matching-substring>
                <xsl:non-matching-substring>
                    <xsl:value-of select="."/>
                </xsl:non-matching-substring>
            </xsl:analyze-string>
        </xsl:template>
    </xsl:stylesheet>
    

    输出:

    <Description>This is a <i>long</i> description</Description>
    

    【讨论】:

    • 谢谢 - 这正是我需要的!
    【解决方案2】:

    你没有。正如 CSS3 规范所说,

    选择器定义了以下功能:

    表达式 * 元素 → 布尔值

    也就是说,除了元素之外没有选择任何东西的概念。

    如果“long”要斜体,则需要将其放置在单独的内联元素中。 &lt;em&gt; 可能是一个合适的选择。

    您可以在文档发送到浏览器之前在服务器端执行此操作,或者您可以使用 JavaScript 以及可能像正则表达式匹配这样简单的方法在客户端执行此操作。

    【讨论】:

    • 感谢您的回答 - 我如何在我的 XML 节点的内容中放置一个 标记并保留 字符?非常感谢您的帮助!
    • 为什么你甚至没有提到 XSLT 解决方案,尽管这个问题被标记了?
    猜你喜欢
    • 2020-02-12
    • 1970-01-01
    • 1970-01-01
    • 2017-03-25
    • 1970-01-01
    • 1970-01-01
    • 2015-02-16
    • 1970-01-01
    • 2020-09-02
    相关资源
    最近更新 更多