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

使用XSLT以每行三列的形式显示XML数据,xml,xslt-1.0,Xml,Xslt 1.0,我有以下XML数据,在这些数据中,我希望以每行三列的形式显示配方。我想在td中显示菜谱标签的标题、服务和图像节点,每行3个td,下一行搜索菜谱 <recipeList> <recipe id="ch02_recipe020"> <title>Spinach and zucchini frittata</title> <serves>Serves 4</serves> &

我有以下XML数据,在这些数据中,我希望以每行三列的形式显示配方。我想在td中显示菜谱标签的标题、服务和图像节点,每行3个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>

菠菜和西葫芦煎饼
服务4
1汤匙
橄榄油
1.
红洋葱,切成薄片
在中火不粘煎锅中加热油,用中火将洋葱和西葫芦炒至浅金黄色。加入大蒜,煮一分钟。加入菠菜,煮到菠菜萎蔫,多余的水分蒸发掉。如果你不这样做,你的FruttaA会在中间湿透,因为液体会继续煮出来。摇动平底锅,使混合物均匀。把暖气调低。
培根鳄梨沙拉
服务4
8.
熏肉锉、去皮
打开烤架(烤箱)。把培根放在托盘上,两面烤制,直到它变得又香又脆。让它冷却,然后切成碎片。
用平底锅把水烧开,把豆子煮4分钟。沥干水份,然后把它们放在冰冷的自来水下几秒钟,以阻止它们继续烹饪。
菠菜沙拉配鸡肉和芝麻酱
服务4
450克(1磅)
小英国菠菜叶
1.
黎巴嫩(短)黄瓜,去皮切丁
把菠菜放在一个大碗里。把黄瓜、葱和胡萝卜撒在上面。把鸡胸肉切成长条,撒在蔬菜上。
豆蔻米饭鸡
服务4
250毫升(1杯)
天然酸奶,加上额外的服务
将八根木串在水中浸泡30分钟,以防止它们在烹饪过程中燃烧。将酸奶、tandoori酱和柠檬汁混合在一个非金属盘中。加入鸡肉,裹好外衣,然后盖上盖子,腌至少10分钟。
我正在使用下面的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>

默多克图书DIY
3.


有人能帮忙吗?我是XSLT新手。

您需要更改此部分:

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

        <xsl:apply-templates select=". | following-sibling::title[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:strip-space元素=“*”/>
,它们都没有什么意义。这些只是我注意到的一些事情,可能还有更多。

您的要求不明确。您想要一个有3列的表(每行一个配方,每列3个单元格),还是一个有9列的表(每行3个配方,每行3个单元格)?对不起,我不清楚。我需要一个表,每行3列,每列将有配方标题标签和缩略图图像。如果我的搜索结果中有7条记录,那么我的表将显示3行,前2行有6个配方(3列),最后一行有一个配方(一列)。先谢谢你。
<xsl:apply-templates select="recipe[position() mod $cols = 1]" mode="row" />