ChPortos
ChPortos

Reputation: 75

Formatting colors/bgcolors/bold/underline specified in XML

Coming from this 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>

I must produce this 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>

I tried to achieve this with :

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

But it fails by stripping everything that may be within <u> or <b> or <i> it may found (see <found>) or putting every colored stuff before all other content (see <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>

Where am I wrong ?

Many thanks for any help !

Upvotes: 1

Views: 535

Answers (2)

G&#225;bor Erdős
G&#225;bor Erdős

Reputation: 3689

As far as I see you want to:

  1. change the <found> and <notfound> tags into string, plus add a <p> tag before, and a <pre> after
  2. Change the <color bgcolor="red"> into <span style="color:red;"> and </color> into </span>

What I would do is use Beautiful Soup, iterate over the tags, and change them according to 1 and 2.

Upvotes: 0

choroba
choroba

Reputation: 241858

I don't have version 2.0 available, but I was able to change it into a working 1.0 version. The important changes in the comments:

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

Upvotes: 1

Related Questions