XML 中指定的格式化 colors/bgcolors/bold/underline

Formatting colors/bgcolors/bold/underline specified in XML

来自这个 XML :

<log>
        <found>
            <color bgcolor="red">This is a silly <u>dummy</u> text with <color color="green">colored</colored> words and some <b>emphasized</b> ones</color>
        </found>
        <notfound>
            This is a silly <color color="red">stupid</color> text
        </notfound>
</log>

我必须制作这个 HTML :

<p>Found : <pre><span style="background-color:red;">This is a silly <u>dummy</u> text with <span style="color:green;">colored</span> words and some <b>emphasized</b> ones</span></pre></p>
<p>Not Found : <pre>This is a silly <span style="color:red;">stupid</span> text</pre></p>

我试图通过以下方式实现这一目标:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" encoding="UTF-8"/>

    <xsl:apply-templates select="/log"/>

    <xsl:template match="log">
        <xsl:apply-templates select="found"/>
        <xsl:apply-templates select="notfound"/>
    </xsl:template>

    <xsl:template match="found">
        <xsl:if test=". != ''">
            <p>Found : <pre><xsl:apply-templates name="./color"/></pre></p>
        </xsl:if>
    </xsl:template>

    <xsl:template match="notfound">
        <xsl:if test=". != ''">
            <p>Not found : <pre><xsl:apply-templates name="./color"/></pre></p>
        </xsl:if>
    </xsl:template>

    <xsl:template match="*/color">
        <span>
            <xsl:attribute name="style">
                <xsl:if test="./@color != ''">
                    color:<xsl:value-of select="./@color"/>;
                </xsl:if>
                <xsl:if test="./@bgcolor != ''">
                    background-color:<xsl:value-of select="./@bgcolor"/>; 
                </xsl:attribute>
            <xsl:apply-templates select="./color"/>
            <xsl:copy-of select="text()"/>
        </span>
    </xsl:template>

</xsl:stylesheet>

但是它失败了,因为它会删除它可能找到的 <u><b><i> 内的所有内容(参见 <found>)或将所有颜色的内容放在所有内容之前其他内容(见<notfound>):

<p>Found : <pre><span style="background-color:red;">This is a silly  text with <span style="color:green;">colored</span> words and some  ones</span></pre></p>
<p>Not Found : <pre><span style="color:red;">stupid</span>This is a silly  text</pre></p>

我哪里错了?

非常感谢您的帮助!

据我所知你想要:

  1. <found><notfound>标签改成字符串,在前面加一个<p>标签,在
  2. 后面加一个<pre>
  3. <color bgcolor="red">改为<span style="color:red;">,将</color>改为</span>

我会做的是使用 Beautiful Soup,迭代标签,并根据 1 和 2 更改它们。

我没有可用的 2.0 版,但我可以将其更改为可用的 1.0 版。评论中的重要变化:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" encoding="UTF-8"/>

    <xsl:template match="log">
        <xsl:apply-templates select="found"/>
        <xsl:apply-templates select="notfound"/>
    </xsl:template>

    <xsl:template match="found">
        <xsl:if test=". != ''">
            <p>Found : <pre><xsl:apply-templates/></pre></p>
        </xsl:if>
    </xsl:template>

    <xsl:template match="notfound">
        <xsl:if test=". != ''">
            <p>Not found : <pre><xsl:apply-templates/></pre></p>
        </xsl:if>
    </xsl:template>

    <xsl:template match="color">
        <span>
            <xsl:attribute name="style">
                <xsl:if test="./@color != ''">color:<xsl:value-of select="./@color"/>;</xsl:if>
                <xsl:if test="./@bgcolor != ''">background-color:<xsl:value-of select="./@bgcolor"/>;</xsl:if>
            </xsl:attribute>
            <!-- this is needed to process the inner tags -->
            <xsl:apply-templates/>
        </span>
    </xsl:template>

    <!-- This prints underlined and bold text. -->
    <xsl:template match="*">
      <xsl:copy-of select="."/>
    </xsl:template>

</xsl:stylesheet>