XML上的条件XSL循环。

时间:2014-04-02 16:15:17

标签: xml loops xslt

我想通过我的XML文件使用XSL创建一个循环,该文件只处理评级大于5的游戏:

<game_id id="101">
        <game_name>Minecraft</game_name>
        <game_price currency="€">23.99</game_price>
        <game_type type="Sandbox" />
        <game_art>minecraft.jpg</game_art>
        <game_platform>
            <platform platform="XboxOne">XboxOne</platform>
            <platform platform="Ps4">Ps4</platform>
            <platform platform="PC">PC</platform>
            <platform platform="Mac">Mac</platform>
        </game_platform>
        <game_desc>Simple block based game</game_desc>
        <game_dev>Mojang</game_dev>
        <game_rating rating="9">5/10 </game_rating>
        <game_review>
            <para para="1">The game involves players creating and destroying various types of blocks in a three dimensional environment. 
                        The player takes an avatar that can destroy or create blocks, forming fantastic structures, 
                        creations and artwork across the various multiplayer servers in multiple game modes
                        </para>
            <para para="x"></para>
            <para para="x"></para>
        </game_review>
    </game_id>

我一直在尝试创建循环,但我的尝试失败了:

<xsl:choose>
        <xsl:when test="game_id/game_rating =  '5'">
            <h1><xsl:value-of select="game_name"></xsl:value-of></h1>
            <hr />
                <img> <xsl:attribute name="src"> <xsl:value-of select="game_art"/></xsl:attribute></img>

                    <p><strong>Price:</strong> <xsl:value-of select="game_price"></xsl:value-of></p>

                    <xsl:for-each select="game_platform/platform">
                    <p><strong>Platform:</strong> <xsl:value-of select="node()"></xsl:value-of></p>
                    </xsl:for-each>

                    <p><strong>Game Desc: </strong> <xsl:value-of select="game_desc"></xsl:value-of></p>

                    <p><strong>Game Dev: </strong> <xsl:value-of select="game_dev"></xsl:value-of></p>

                    <p><strong>Game Rating: </strong> <xsl:value-of select="game_rating"></xsl:value-of></p>

                    <h4>Game Reveiw</h4>
                    <xsl:for-each select="game_review/para"> 
                        <p><xsl:value-of select="node()"></xsl:value-of></p>
                    </xsl:for-each>
                </xsl:when>

            </xsl:choose>

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

理想情况下,您不需要循环,但使用模板匹配。假设您有多个 game_id 元素,并且位于它们的父元素上,您可以这样做...

<xsl:apply-templates select="game_id[number(game_rating/@rating) > 5]" />

然后,您将拥有一个与 game_id

匹配的模板
<xsl:template match="game_id">
    <h1><xsl:value-of select="game_name"></xsl:value-of></h1>

例如,给定此样式表

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="html" indent="yes"/>

    <xsl:template match="/*">
        <xsl:apply-templates select="game_id[number(game_rating/@rating) > 5]" />
    </xsl:template>

    <xsl:template match="game_id">
        <h1><xsl:value-of select="game_name" /></h1>
    </xsl:template>
</xsl:stylesheet>

应用于以下XML

<games>
    <game_id id="101">
        <game_name>Minecraft</game_name>
        <game_rating rating="9">9/10</game_rating>
    </game_id>
    <game_id id="102">
        <game_name>Goat Simulator</game_name>
        <game_rating rating="4">4/10</game_rating>
    </game_id>
</games>

然后输出以下内容

 <h1>Minecraft</h1>

或者,根据XML的实际结构,您可以允许XSLT的内置模板选择节点,并有一个额外的模板来排除低评级的游戏。在这种情况下,以下内容也会提供相同的输出。

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="html" indent="yes"/>

    <xsl:template match="game_id[number(game_rating/@rating) &lt;= 5]" />

    <xsl:template match="game_id">
        <h1><xsl:value-of select="game_name" /></h1>
    </xsl:template>
</xsl:stylesheet>