-2

I'm new to XSL and I'm having some issues with fully understanding the mode attribute. I assumed it was an optional feature, however, a project I've recently got involved with uses it to generate email campaigns to discriminate between top-stories and normal stories in a kind of bulk fashion.

The mode seems to have a profound effect on what these objects look like. For example, a particular mode appears to create background colours and fixed table widths. Still, I cannot locate where or how the mode decides on this. As soon as I change or remove the mode all hell seems to break loose and the layout is quickly destroyed.

I appreciate this sounds brief but any help with how the mode is created and where the associated styling for this would be appreciated.

Mathias Müller
  • 22,203
  • 13
  • 58
  • 75

1 Answers1

3

Because you did not give any input XML or example XSLT, this example may not apply to your solution/request, but it gives an idea how the mode works.

Input XML

<?xml version="1.0" encoding="UTF-8"?>
<data>
    <set>
        <big>no</big>
        <field1>Test</field1>
        <field2>Value</field2>
    </set>
    <set>
        <big>yes</big>
        <field1>Test 2</field1>
        <field2>Value 2</field2>
    </set>
    <set>
        <big>no</big>
        <field1>Test 3</field1>
        <field2>Value 3</field2>
    </set>
</data>

XSLT

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

    <xsl:template match="@*|node()">
        <xsl:apply-templates select="@*|node()" />
    </xsl:template>

    <xsl:template match="data">
        <html>
            <head>
                <title>TEST</title>
            </head>
            <body>
                <table>
                    <xsl:apply-templates select="set[big='yes']" mode="upper" />
                    <xsl:apply-templates select="set[big='no']" mode="lower" />
                </table>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="set" mode="upper">
        <tr>
            <td><xsl:value-of select="upper-case(field1)" /></td>
            <td><xsl:value-of select="upper-case(field2)" /></td>
        </tr>
    </xsl:template>

    <xsl:template match="set" mode="lower">
        <tr>
            <td><xsl:value-of select="lower-case(field1)" /></td>
            <td><xsl:value-of select="lower-case(field2)" /></td>
        </tr>
    </xsl:template>
</xsl:stylesheet>

Output

<?xml version="1.0" encoding="UTF-8"?>
<html>
    <head>
        <title>TEST</title>
    </head>
    <body>
        <table>
            <tr>
                <td>TEST 2</td>
                <td>VALUE 2</td>
            </tr>
            <tr>
                <td>test</td>
                <td>value</td>
            </tr>
            <tr>
                <td>test 3</td>
                <td>value 3</td>
            </tr>
        </table>
    </body>
</html>

Briefly explained

With the following code:

<xsl:apply-templates select="set[big='yes']" mode="upper" />
<xsl:apply-templates select="set[big='no']" mode="lower" />

You decide which mode should be applied on the selected nodes. Within the template match you defined these modes:

<xsl:template match="set" mode="upper">
<xsl:template match="set" mode="lower">
Mark Veenstra
  • 4,691
  • 6
  • 35
  • 66