user6313890
user6313890

Reputation:

XSL - Best practices for simple but verbose conditions

I have a XML file :

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Cars>
    <Car>
        <Color>Yellow</Color>
    </Car>
    <Car>
        <Color>Green</Color>
    </Car>
    <Car>
        <Color>Blue</Color>
    </Car>
    <Car>
        <Color>Black</Color>
    </Car>
    <Car>
        <Color>White</Color>
    </Car>
</Cars>

And one XSL file :

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" encoding="utf-8" media-type="text/xml"/>
    <xsl:template match="/">
        <Vehicles>
            <xsl:for-each select="Cars/Car">
                <Vehicle>
                    <VehicleColor>
                        <xsl:value-of select="Color"/>
                    </VehicleColor>
                    <xsl:choose>
                        <xsl:when test="Color = 'Yellow' or Color = 'Green' or Color ='Blue'">
                        </xsl:when>
                        <xsl:otherwise>
                            <VehicleStatus>ok</VehicleStatus>
                        </xsl:otherwise>
                    </xsl:choose>
                </Vehicle>
            </xsl:for-each>
        </Vehicles>
    </xsl:template>
</xsl:stylesheet>

which gives :

<?xml version="1.0" encoding="utf-8"?>
<Vehicles>
    <Vehicle>
        <VehicleColor>Yellow</VehicleColor>
    </Vehicle>
    <Vehicle>
        <VehicleColor>Green</VehicleColor>
    </Vehicle>
    <Vehicle>
        <VehicleColor>Blue</VehicleColor>
    </Vehicle>
    <Vehicle>
        <VehicleColor>Black</VehicleColor>
        <VehicleStatus>ok</VehicleStatus>
    </Vehicle>
    <Vehicle>
        <VehicleColor>White</VehicleColor>
        <VehicleStatus>ok</VehicleStatus>
    </Vehicle>
</Vehicles>  

I want to display the element if the color is different from Yellow, Green or Blue. It works but how to do this otherwise ? It's not pretty like that and I have a lot of colors

How to create a array with all the forbidden colors for example ?

I use only XSL 1.0

Thank's

Upvotes: 1

Views: 127

Answers (2)

michael.hor257k
michael.hor257k

Reputation: 116959

How to create a array with all the forbidden colors for example ?

Here's one way:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="http://www.example.com/my"
exclude-result-prefixes="my">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<my:forbidden-colors>
    <color>Yellow</color>
    <color>Green</color>
    <color>Blue</color>
</my:forbidden-colors>

<xsl:template match="/Cars">
    <Vehicles>
        <xsl:for-each select="Car">
            <Vehicle>
                <VehicleColor>
                    <xsl:value-of select="Color"/>
                </VehicleColor>
                <xsl:if test="not(Color = document('')/xsl:stylesheet/my:forbidden-colors/color)">
                    <VehicleStatus>ok</VehicleStatus>
                </xsl:if>
            </Vehicle>
        </xsl:for-each>
    </Vehicles>
</xsl:template>

</xsl:stylesheet>

Here's another (modeled after your original approach, actually):

<xsl:stylesheet version="1.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="/Cars">
    <Vehicles>
        <xsl:for-each select="Car">
            <Vehicle>
                <VehicleColor>
                    <xsl:value-of select="Color"/>
                </VehicleColor>
                <xsl:choose>
                    <xsl:when test="Color = 'Yellow'"/>
                    <xsl:when test="Color = 'Green'"/>
                    <xsl:when test="Color = 'Blue'"/>
                    <xsl:otherwise>
                        <VehicleStatus>ok</VehicleStatus>
                    </xsl:otherwise>
                </xsl:choose>
            </Vehicle>
        </xsl:for-each>
    </Vehicles>
</xsl:template>

</xsl:stylesheet>

Upvotes: 2

Dan Field
Dan Field

Reputation: 21641

@michael.hor257k's solution is neat, but here's another way to do/think of it: Create templates for each "forbidden" color that output your minimal version, and another template that matches all (i.e. all other) colors to output the full version:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" encoding="utf-8" media-type="text/xml"/>
    <xsl:template match="/">
        <Vehicles>
            <xsl:apply-templates />
        </Vehicles>
    </xsl:template>

    <xsl:template match="Car">
        <Vehicle>
            <xsl:apply-templates />
        </Vehicle>        
    </xsl:template>

    <xsl:template match="Color[. = 'Yellow']">
        <VehicleColor>
            <xsl:value-of select="."/>
        </VehicleColor>        
    </xsl:template>

    <xsl:template match="Color[. = 'Green']">
        <VehicleColor>
            <xsl:value-of select="."/>
        </VehicleColor>        
    </xsl:template>

    <xsl:template match="Color[. = 'Blue']">
        <VehicleColor>
            <xsl:value-of select="."/>
        </VehicleColor>        
    </xsl:template>

    <xsl:template match="Color">
        <VehicleColor>
            <xsl:value-of select="."/>
        </VehicleColor>
        <VehicleStatus>ok</VehicleStatus>
    </xsl:template>
</xsl:stylesheet>

If you're worried about repetitive code, you could use a call template as well like this:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" encoding="utf-8" media-type="text/xml"/>
    <xsl:template match="/">
        <Vehicles>
            <xsl:apply-templates />
        </Vehicles>
    </xsl:template>

    <xsl:template match="Car">
        <Vehicle>
            <xsl:apply-templates />
        </Vehicle>        
    </xsl:template>

    <xsl:template match="Color[. = 'Yellow']">
        <xsl:call-template name="vehicle-color" />
    </xsl:template>

    <xsl:template match="Color[. = 'Green']">
        <xsl:call-template name="vehicle-color" />
    </xsl:template>

    <xsl:template match="Color[. = 'Blue']">
        <xsl:call-template name="vehicle-color" />
    </xsl:template>

    <xsl:template match="Color">       
        <xsl:call-template name="vehicle-color" />
        <VehicleStatus>ok</VehicleStatus>
    </xsl:template>

    <xsl:template name="vehicle-color">
        <VehicleColor>
            <xsl:value-of select="."/>
        </VehicleColor>
    </xsl:template>
</xsl:stylesheet>

This isn't exactly the approach you've asked for, but it feels a little more like native XSLT than an attempt to get the processor to think using custom arrays/lists.

Upvotes: 2

Related Questions