使用XSLT以每行三列显示XML数据

时间:2017-02-15 07:38:18

标签: xml xslt-1.0

我有以下XML数据,其中我想在每行三列中显示配方。我想在每行3 td的td中显示配方标签的标题,服务和图像节点,并在下一行中跟踪搜索的配方。

<recipeList>
    <recipe id="ch02_recipe020">
        <title>Spinach and zucchini frittata</title>
        <serves>Serves 4</serves>
        <ingredientList>
            <ingredientEntry>
                <ingredientQty>1 tablespoon</ingredientQty>
                <keyIngredient>olive oil</keyIngredient>
            </ingredientEntry>
            <ingredientEntry>
                <ingredientQty>1</ingredientQty>
                <keyIngredient>red onion</keyIngredient>, thinly sliced

            </ingredientEntry>

        </ingredientList>
        <prepSteps>
            <prepStep>
                <para>Heat the oil in a medium non-stick frying pan and fry the onion and zucchini over medium heat until they are a pale golden brown. Add the garlic and cook it for a minute. Add the spinach and cook until the spinach has wilted and any excess moisture has evaporated off — if you don't do this, your frittata will end up soggy in the middle, as the liquid will continue to come out as it cooks. Shake the pan so you get an even layer of mixture. Turn the heat down to low.</para>
            </prepStep>

        </prepSteps>
        <figure>
            <img src="images/01075/091.jpg" />
        </figure>

    </recipe>
    <recipe id="ch02_recipe026">
        <title>Bacon and avocado salad</title>
        <serves>Serves 4</serves>
        <ingredientList>
            <ingredientEntry>
                <ingredientQty>8</ingredientQty>
                <keyIngredient>bacon rashers</keyIngredient>, rinds cut off

            </ingredientEntry>

        </ingredientList>
        <prepSteps>
            <prepStep>
                <para>Turn on the grill (broiler). Put the bacon on a tray and grill on both sides until it is nice and crisp. Leave it to cool and then break into pieces.</para>
            </prepStep>
            <prepStep>
                <para>Bring a saucepan of water to the boil and cook the beans for 4 minutes. Drain and then hold them under cold running water for a few seconds to stop them cooking any further.</para>
            </prepStep>

        </prepSteps>
        <figure>
            <img src="images/01075/103.jpg" />
        </figure>

    </recipe>
    <recipe id="ch02_recipe028">
        <title>Spinach salad with chicken and sesame dressing</title>
        <serves>Serves 4</serves>
        <ingredientList>
            <ingredientEntry>
                <ingredientQty>450 g (1 lb)</ingredientQty>
                <keyIngredient>baby English spinach leaves</keyIngredient>
            </ingredientEntry>
            <ingredientEntry>
                <ingredientQty>1</ingredientQty>
                <keyIngredient>Lebanese (short) cucumber</keyIngredient>, peeled and diced

            </ingredientEntry>

        </ingredientList>
        <prepSteps>
            <prepStep>
                <para>Put the spinach in a large bowl. Scatter the cucumber, spring onion and carrot over the top. Shred the chicken breast into long pieces and scatter it over the vegetables.</para>
            </prepStep>

        </prepSteps>
        <figure>
            <img src="images/01075/107.jpg" />
        </figure>
        <file src="MB Pages/Lunch.qxd" />
    </recipe>
    <recipe id="ch03_recipe025">
        <title>Tandoori chicken with cardamom rice</title>
        <serves>Serves 4</serves>
        <ingredientList>
            <ingredientEntry>
                <ingredientQty>250 ml (1 cup)</ingredientQty>
                <keyIngredient>natural yoghurt</keyIngredient>, plus extra for serving

            </ingredientEntry>

        </ingredientList>
        <prepSteps>
            <prepStep>
                <para>Soak eight wooden skewers in water for 30 minutes to prevent them burning during cooking. Combine the yoghurt, tandoori paste and lemon juice in a non-metallic dish. Add the chicken and coat well, then cover and marinate for at least 10 minutes.</para>
            </prepStep>

        </prepSteps>
        <figure>
            <img src="images/01075/174.jpg" />
        </figure>
        <file src="MB Pages/Casual 124-185.qxd" />
    </recipe>

</recipeList>

我在XSLT下使用,但每行不显示3条记录。

<?xml version="1.0" encoding="UTF-8" ?>
<!-- Designed by SoftServ Solutions, February 13, 2017 -->
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:m="http://www.w3.org/1998/Math/MathML"
                xmlns:xlink="http://www.w3.org/1999/xlink">
    <xsl:output method="html" indent="yes" encoding="windows-1252" />
    <xsl:preserve-space elements="*" />
    <xsl:template match="/">
        <html>
        <head>
            <title>Murdoch Books DIY</title>
        </head>
        <body>
            <xsl:apply-templates select="recipeList" />
        </body>
    </html>
</xsl:template>
<xsl:output indent="yes" />
<xsl:strip-space elements="*" />
<xsl:param name="cols">3</xsl:param> <!-- set the number of rows here -->
<xsl:template match="recipeList">
    <table width="30%" align="center" border="1">
        <xsl:apply-templates select="recipe[position() mod $cols = 1 or position() = 1]" mode="row" />
    </table>
</xsl:template>

<xsl:template match="recipe" mode="row">
    <tr>

        <xsl:apply-templates select=". | following-sibling::title[position() &lt; $cols]" mode="cell" />

    </tr>
</xsl:template>

<xsl:template match="recipe" mode="cell">
    <td>
        <xsl:value-of select="title" />
        <xsl:value-of select="serves" />
        <br />
        <xsl:if test="figure">
            <xsl:apply-templates select="figure" />
        </xsl:if>
    </td>
</xsl:template>
<xsl:template match="figure">
    <p align="left">
        <img width="25%" height="25%">
        <xsl:attribute name="src">
            <xsl:value-of select="img/@src" />
        </xsl:attribute>
        </img>
    </p>
    <a>
        <xsl:attribute name="name">
            <xsl:value-of select="@id" />
        </xsl:attribute>
    </a>
    <p>
        <xsl:apply-templates select="caption" />
    </p>
    <p>
        <xsl:apply-templates select="source" />
    </p>
</xsl:template>
</xsl:stylesheet>

有人可以帮忙吗?我是XSLT的新手。

1 个答案:

答案 0 :(得分:0)

您需要更改此部分:

<xsl:template match="recipe" mode="row">
    <tr>

        <xsl:apply-templates select=". | following-sibling::title[position() &lt; $cols]" mode="cell" />

    </tr>
</xsl:template>

为:

<xsl:template match="recipe" mode="row">
    <tr>

        <xsl:apply-templates select=". | following-sibling::recipe[position() &lt; $cols]" mode="cell" />

    </tr>
</xsl:template>

当你在这里时,你可以在这里删除冗余:

<xsl:apply-templates select="recipe[position() mod $cols = 1 or position() = 1]" mode="row" />

简单地说:

<xsl:apply-templates select="recipe[position() mod $cols = 1]" mode="row" />

您还有两个xsl:output个元素,<xsl:preserve-space elements="*" />xsl:strip-space elements="*" />这两个元素都没有多大意义。这些只是我注意到的一些事情,可能还有更多。