How to import style sheets into xslt conditionally?

Is there a way to import style sheets after checking some conditions?

For example, if the value of the variable $ a = "1", then import 1.xsl or import 2.xsl.

+5
source share
2 answers

Hi everyone, Is there a way to import style sheets after checking some conditions?

For example, if the value of the variable is $ a = "1" then import 1.xsl or import 2.xsl.

No, the directive <xsl:import>is only compilation time .

In XSLT 2.0, you can use the attribute for limited conditional compilation. use-when

For example :

<xsl:import href="module-A.xsl" 
     use-when="system-property('xsl:vendor')='vendor-A'"/>

use-when , , , , .

-XSLT- href <xsl:import> , :

  • xsl XML

  • , , .

  • href <xsl:import> URI .

  • xsl .

+14

, , .

. VB.

breakfast_menu.xml:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="conditionDisplay.xsl" ?>
<data>
    <breakfast_menu>
        <food>
            <name>Belgian Waffles</name>
            <price>$5.95</price>
            <description>Two of our famous Belgian Waffles with plenty of real maple syrup</description>
            <calories>650</calories>
        </food>

        <food>
            <name>Strawberry Belgian Waffles</name>
            <price>$7.95</price>
            <description>Light Belgian waffles covered with strawberries and whipped cream</description>
            <calories>900</calories>
        </food>


        <food>
            <name>Homestyle Breakfast</name>
            <price>$6.95</price>
            <description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
            <calories>950</calories>
        </food>
    </breakfast_menu> 
    <display>1</display>
</data>

, , .

conditionDisplay.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
    <xsl:import href="display1.xsl"/>
    <xsl:import href="display2.xsl"/>
    <xsl:template match="/">
        <xsl:variable name="display"><xsl:value-of select= "data/display"/></xsl:variable>
        <xsl:choose>
            <xsl:when test="$display='1'">
                <xsl:call-template name="display1" />
            </xsl:when>
            <xsl:otherwise>
                <xsl:call-template name="display2 />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

display1.xsl:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template name="display1">
        <html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
            <body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
                <xsl:for-each select="data/breakfast_menu/food">
                    <div style="background-color:teal;color:white;padding:4px">
                        <span style="font-weight:bold"><xsl:value-of select="name"/> - </span>
                        <xsl:value-of select="price"/>
                    </div>
                    <div style="margin-left:20px;margin-bottom:1em;font-size:10pt">
                        <p>
                            <xsl:value-of select="description"/>
                            <span style="font-style:italic"> (<xsl:value-of select="calories"/> calories per serving)</span>
                        </p>
                    </div>
                </xsl:for-each>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

display2.xsl:

<?xml version="1.0" encoding="UTF-8"?>futur
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template name="display2">
        <html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
            <body style="font-family:Arial;font-size:12pt;background-color:#222222">
                <xsl:for-each select="data/breakfast_menu/food">
                    <div style="background-color:teal;color:white;padding:4px">
                        <span style="font-weight:bold"><xsl:value-of select="name"/> - </span>
                        <xsl:value-of select="price"/>
                    </div>
                    <div style="margin-left:20px;margin-bottom:1em;font-size:10pt">
                        <p>
                            <xsl:value-of select="description"/>
                            <span style="font-style:italic"> (<xsl:value-of select="calories"/> calories per serving)</span>
                        </p>
                    </div>
                </xsl:for-each>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

. , , -, , .

+2

All Articles