首页 > 解决方案 > 为什么我的 XSL 样式表不能与我的 XML 一起使用?

问题描述

我正在努力让数据显示在表格中,但它不起作用。我在代码中看不到任何错误(尽管必须有),并且我已经完成了教程但没有成功。

我真的希望我只是盲目的,答案会立即弹出给某人。

XML:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<?xml-stylesheet type="text/xsl" href="recipes.css" ?>
<recipes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="try.xsd">
    <recipe id="1">
        <recipe_name>Gooseberry crème brûlée tart</recipe_name>
        <prep_time>10</prep_time>
        <cook_time>80</cook_time>
        <difficulty>Moderate</difficulty>
        <ingredient name="gooseberries" amount="450g" />
        <ingredient name="white caster sugar" amount="200g" />
        <ingredient name="eggs" amount="4" />
        <ingredient name="double cream" amount="100ml" />
        <ingredient name="block sweet pastry" amount="500g" />
        <ingredient name="flour" amount="for dusting" />
        <servings>8</servings>
    </recipe>
    <recipe id="2">
        <recipe_name>One-pan jerk roast chicken</recipe_name>
        <prep_time>30</prep_time>
        <cook_time>130</cook_time>
        <difficulty>Easy</difficulty>
        <ingredient name="whole chicken" amount="1.5kg" />
        <ingredient name="red onions" amount="2" prep_info="then cut in to wedges leaving the root intact"/>
        <ingredient name="red peppers" amount="2" prep_info="deseeded and chopped in to chunks"/>
        <ingredient name="sweet potatoes" amount="4" prep_info="peeled and cut into chunks"/>
        <ingredient name="can black beans" amount="400 grams" prep_info="drained and rinsed"/>
        <ingredient name="coconut milk" amount="400ml" />
        <ingredient name="cooked rice" prep_info="to serve"/>
        <ingredient name="flatbreads" prep_info="to serve"/>
        <servings>4-6</servings>
    </recipe>
    <recipe id="3">
        <recipe_name>Roast sirloin of beef</recipe_name>
        <prep_time>10</prep_time>
        <cook_time>150</cook_time>
        <difficulty>Easy</difficulty>
        <ingredient name="vegetable oil or beef fat" amount="2 tbsp" />
        <ingredient name="sirloin of beef joint" amount="1-1.5 kg" />
        <ingredient name="red wine" amount="1" unit="glass" />
        <ingredient name="can beef consommé" amount="400g" />
        <servings>6</servings>
    </recipe>
    <recipe id="4">
        <recipe_name>Haddock &amp; leek au gratin with sweetcorn mash</recipe_name>
        <prep_time>10</prep_time>
        <cook_time>50</cook_time>
        <difficulty>Easy</difficulty>
        <ingredient name="potatoes" amount="350g" prep_info="quartered" />
        <ingredient name="can sweetcorn in water" amount="195g" />
        <ingredient name="bag ready washed spinach" amount="240g" />
        <ingredient name="leeks" amount="2" prep_info="thickly sliced" />
        <ingredient name="skimmed milk" amount="300ml" prep_info="plus 3 tbsp" />
        <ingredient name="unsalted butter" amount="15g" />
        <ingredient name="plain flour" amount="15g" />
        <ingredient name="English mustard" amount="0.5tsp" />
        <ingredient name="mature reduced-fat cheese" amount="75g" prep_info="grated" />
        <ingredient name="fillets of skinless haddock" amount="2* 125g" />
        <servings>4</servings>
    </recipe>
    <recipe id="5">
        <recipe_name>Cheesy mushroom omelette</recipe_name>
        <prep_time>5</prep_time>
        <cook_time>10</cook_time>
        <difficulty>Easy</difficulty>
        <ingredient name="olive oil" amount="1tbsp" />
        <ingredient name="button or chestnut mooshrooms" amount="handful" />
        <ingredient name="vegetarian cheddar" amount="25g" prep_info="grated" />
        <ingredient name="parsley leaves" amount="handful" prep_info="roughly chopped" />
        <ingredient name="eggs" amount="2" prep_info="beaten" />
        <servings>2</servings>
    </recipe>
    <recipe id="6">
        <recipe_name>Chorizo bean burgers</recipe_name>
        <prep_time>10</prep_time>
        <cook_time>16</cook_time>
        <difficulty>Easy</difficulty>
        <ingredient name="mini cook chorizo" amount="100g" prep_info="skins removed" />
        <ingredient name="port sausages" amount="3" prep_info="squeezed from their skins" />
        <ingredient name="smoked paprika" amount="0.25tsp" />
        <ingredient name="lime juice" amount="1tbsp" prep_info="plus wedges to serve" />
        <ingredient name="coriander" prep_info="small bunch" />
        <ingredient name="can mixed beans" amount="200g" prep_info="half a can" />
        <ingredient name="egg" amount="1" prep_info="lightly whisked" />
        <ingredient name="olive olive" amount="1.5tbsp" prep_info="plus extra to drizzle" />
        <ingredient name="ciabatta rolls" amount="2" prep_info="split and toasted" />
        <ingredient name="soured cream, avocado, small handful rocket and chips" prep_info="to serve (optional)" />
        <servings>2</servings>
    </recipe>
    <recipe id="7">
        <recipe_name>Cherry bruschetta</recipe_name>
        <prep_time>5</prep_time>
        <cook_time>4</cook_time>
        <difficulty>Easy</difficulty>
        <ingredient name="ciabatta" amount="1 loaf" />
        <ingredient name="extra virgin olive oil" />
        <ingredient name="ricotta" amount="100g" />
        <ingredient name="proscuitto" amount="80g" />
        <servings>12</servings>
    </recipe>
    <recipe id="8">
        <recipe_name>Hoisin mackerel pancakes</recipe_name>
        <prep_time>15</prep_time>
        <cook_time>5</cook_time>
        <difficulty>Easy</difficulty>
        <ingredient name="mackerel fillets" amount="3" prep_info="all bones removed, cut into finger-length strips" />
        <ingredient name="hoisin sauce" amount="2tbsp" prep_info="plus extra for dipping" />
        <ingredient name="spring onions" amount="4" />
        <ingredient name="cucumber" amount="0.5" />
        <ingredient name="little gem lettuces" amount="2" />
        <ingredient name="vegetable oil" amount="1tbsp" />
        <ingredient name="chinese pancakes" amount="10" prep_info="(available from Waitrose, Asian Supermarkets or Chinese Takeaways)" />
        <servings>4</servings>
    </recipe>
    <recipe id="9">
        <recipe_name>Fettuccine alfredo</recipe_name>
        <prep_time>15</prep_time>
        <cook_time>10</cook_time>
        <difficulty>Easy</difficulty>
        <ingredient name="clotted cream" amount="227g" />
        <ingredient name="butter" amount="25g" prep_info="(about 2 tbsp)" />
        <ingredient name="cornflour" amount="1tsp" />
        <ingredient name="parmesan" amount="100g" prep_info="grated" />
        <ingredient name="freshly grated nutmeg" />
        <ingredient name="fresh fettuccine or tagliatelle" amount="250" />
        <servings>
        </servings>
    </recipe>
    <recipe id="10">
        <recipe_name>Chicken &amp; mushroom puff pie</recipe_name>
        <prep_time>45</prep_time>
        <cook_time>30</cook_time>
        <difficulty>Easy</difficulty>
        <ingredient name="vegetable oil" amount="1 tbsp" />
        <ingredient name="skinless boneless chicken thighs" amount="8" />
        <ingredient name="smoked streaky bacon rashers" amount="8" prep_info="cut in to large pieces" />
        <ingredient name="onion" amount="0.5" prep_info="halved and sliced" />
        <ingredient name="baby button mushrooms" amount="250g" />
        <ingredient name="handful thyme sprigs" />
        <ingredient name="plain flour" amount="2 tbsp" />
        <ingredient name="chicken stock" amount="400ml" />
        <ingredient name="milk" amount="200ml" />
        <ingredient name="puff pastry" amount="500g" prep_info="fresh or frozen and defrosted" />
        <ingredient name="egg" amount="1" prep_info="beaten" />
        <servings>
        </servings>
    </recipe>
</recipes>

XSL:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output
        method="html"
        doctype-system="about:legacy-compat"
        encoding="UTF-8"
        indent="yes"
        omit-xml-declaration="yes" />
<xsl:template match="/">

<html>
    <head>
        <title>Good Food</title>
        <meta charset="utf-8" />
        <link rel="stylesheet" type="text/css" href="recipes.css" />
    </head>
    <body>
        <h1>Good Food</h1>
        <table border="1">
            <tr>
                <th>Name</th>
                <th>Preperation Time</th>
                <th>Cooking Time</th>
                <th>Difficulty</th>
                <th>Ingredients</th>
                <th>Serves</th>
            </tr>

            <xsl:for-each select="//recipes">
            <xsl:sort select="recipe_name" />
            <tr>
                <td><xsl:value-of select="recipe_name" /></td>
                <td><xsl:value-of select="prep_time" /></td>
                <td><xsl:value-of select="cook_time" /></td>
                <td><xsl:value-of select="difficulty" /></td>
                <td><xsl:value-of select="ingredients" /></td>
                <td><xsl:value-of select="servings" /></td>
            </tr>
            </xsl:for-each>
        </table>
    </body>
</html>
</xsl:template>
</xsl:stylesheet>

作为旁注,如果有人可以为 MAC 推荐一个可靠的代码编辑器,那也将不胜感激。

感谢所有的回复。

标签: xmlxsltdatatable

解决方案


如果要处理所有recipe元素,请使用

        <xsl:for-each select="//recipe">

并不是

        <xsl:for-each select="//recipes">

推荐阅读