XSL - 简单但冗长的条件的最佳实践

时间:2016-07-05 19:25:33

标签: xml xslt xslt-1.0

我有XML个文件:

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

一个XSL文件:

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

给出:

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

如果颜色与黄色,绿色或蓝色不同,我想显示元素。 它有效,但如何做到这一点呢?它不是那样的,我有很多颜色

如何创建一个包含所有禁用颜色的数组?

我只使用XSL 1.0

感谢&#39; S

2 个答案:

答案 0 :(得分:2)

  

如何创建一个包含所有禁用颜色的数组?

以这种方式:

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>

这是另一个(实际上以原始方法为模型):

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

答案 1 :(得分:2)

@ michael.hor257k的解决方案很简洁,但这是另一种方法/想到它:为输出最小版本的每个“禁止”颜色创建模板,为输出匹配所有(即所有其他)颜色的另一个模板完整版:

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

如果您担心重复代码,可以使用以下呼叫模板:

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

这不是完全你所要求的方法,但它感觉有点像本机XSLT,而不是尝试让处理器使用自定义数组/列表进行思考。