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

时间:2015-11-26 14:53:16

标签: html xml xslt xslt-1.0

来自这个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>

我哪里错了?

非常感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

我没有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>

答案 1 :(得分:0)

据我所知,你想:

  1. <found><notfound>标记更改为字符串,并在之前添加<p>标记,在
  2. 之后添加<pre>标记
  3. <color bgcolor="red">更改为<span style="color:red;"></color>更改为</span>
  4. 我要做的是使用Beautiful Soup,迭代标签,然后根据1和2进行更改。