转换时,XSLT不会创建正确的行数

问题描述 投票:0回答:1

我试图从XML创建一个XLST文件,但问题是当我将XSLT转换为HTML时,输出并不像它应该的那样。问题是,当我运行转换时,我只获得一行步骤,即只显示第一步,而其他步骤则不显示。输出可以看到HERE。可以找到所需的结果HERE

XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<Recipe>    
    <Title>Beef Parmesan With Garlic Angel Hair Pasta</Title>    
    <Ingredients>
        <Ingredient>
            <Name>Beef Cube Steak</Name>
            <Quantity>1 1/2 Pounds</Quantity>
            <Description>-</Description>
        </Ingredient>        
        <Ingredient>
            <Name>Spaghetti Sauce</Name>
            <Quantity>One 16 Ounce Jar</Quantity>
            <Description>-</Description>
        </Ingredient>
        <Ingredient>
            <Name>Onion</Name>
            <Quantity>1</Quantity>
            <Description>Sliced Into Thin Rings</Description>
        </Ingredient>        
        <Ingredient>
            <Name>Mozzarella Cheese</Name>
            <Quantity>1/2 Cup</Quantity>
            <Description>Shredded</Description>
        </Ingredient>
        <Ingredient>
            <Name>Green Bell Pepper</Name>
            <Quantity>1</Quantity>
            <Description>Sliced Into Rings</Description>
        </Ingredient>        
        <Ingredient>
            <Name>Green Bell Pepper</Name>
            <Quantity>1</Quantity>
            <Description>Sliced Into Rings</Description>
        </Ingredient>       
        <Ingredient>
            <Name>Angel Hair Pasta</Name>
            <Quantity>12 Ounces</Quantity>
            <Description>-</Description>
        </Ingredient>        
        <Ingredient>
            <Name>Bread Crumbs</Name>
            <Quantity>1 Cup</Quantity>
            <Description>Italian Seasoned</Description>
        </Ingredient>        
        <Ingredient>
            <Name>Garlic</Name>
            <Quantity>2 Teaspoons</Quantity>
            <Description>Minced</Description>
        </Ingredient>
        <Ingredient>
            <Name>Parmesan Cheese</Name>
            <Quantity>1/2 Cup</Quantity>
            <Description>Grated</Description>
        </Ingredient>
        <Ingredient>
            <Name>Butter</Name>
            <Quantity>1/4 Cup</Quantity>
            <Description>-</Description>
        </Ingredient>
        <Ingredient>
            <Name>Olive Oil</Name>
            <Quantity>2 Tablespoons</Quantity>
            <Description>-</Description>
        </Ingredient>        
    </Ingredients>

    <Directions>        
        <Steps>
            <Step>1. Preheat oven to 350 degress Fahrenheit (175 degrees Celsius)</Step>
            <Step>2. Cut cube steak into serving size pieces</Step>
            <Step>3. Coat meat with the bread crumbs and parmesan cheese</Step>
            <Step>4. Heat olive oil in a large frying pan, and saute 1 teaspoon of the garlic for 3 minutes</Step>
            <Step>5. Quick fry (brown quickly on both sides) meat</Step>
            <Step>6. Place meat in a casserole baking dish, slightly overlapping edges</Step>
            <Step>7. Place onion rings and peppers on top of meat, and pour marinara sauce over all</Step>
            <Step>8. Bake at 350 degrees FFahrenheit (175 degrees Celsius) for 30 to 45 minutes, depending on the thickness of the meat</Step>
            <Step>9. Sprinkle mozzarella over meat and leave in the oven till bubbly</Step>
            <Step>10. Boil pasta al dente</Step>
            <Step>11. Drain, and toss in butter and 1 teaspoon garlic</Step>
            <Step>12. For a stronger garlic taste, season with garlic powder</Step>
            <Step>13. Top with grated parmesan and parsley for color. Serve meat and sauce atop a mound of pasta</Step>
        </Steps>        
        <Hint>Make the meat ahead of time, and refrigerate overnight, the acid in the tomato sauce will tenderize the meat even more. If you do this, save the mozzarella till the last minute</Hint>        
    </Directions>

    <Nutritions>
                <Nutrient>
            <Name>Calories</Name>
            <Value>1167</Value>
        </Nutrient>        
        <Nutrient>
            <Name>Protein</Name>
            <Value>71g</Value>
        </Nutrient>        
        <Nutrient>
            <Name>Fat</Name>
            <Value>52g</Value>
        </Nutrient>        
        <Nutrient>
            <Name>Carbohydrates</Name>
            <Value>101g</Value>
        </Nutrient>
    </Nutritions>
</Recipe>      

XSL文件

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:template match="/">
        <html>
            <head>
                <style>
                    table {
                    border-collapse: collapse;
                    }
                    table, th, td {
                    border: 1px solid black;  border: 1px solid black;
                </style>
            </head>
            <body>
                <h1>
                    <xsl:value-of select="Recipe/Title"/>
                </h1>
                <h4>INGREDIENTS</h4>
                <table style="width:100%">
                    <tr>
                        <th>Name</th>
                        <th>Quality</th>
                        <th>Description</th>
                    </tr>
                    <xsl:for-each select="Recipe/Ingredients/Ingredient">
                        <tr>
                            <td>
                                 <xsl:value-of select="Name"/>
                            </td>
                            <td>
                                 <xsl:value-of select="Quantity"/>
                            </td>
                            <td>
                                 <xsl:value-of select="Description"/>
                            </td>
                        </tr>
                    </xsl:for-each>
                </table>
                <h4>DIRECTIONS</h4>
                <table style="width:100%">
                    <tr>
                        <th>Step</th>
                    </tr>
                    <xsl:for-each select="Recipe/Directions/Steps">
                        <tr>
                            <td>
                                 <xsl:value-of select="Step"/>
                            </td>
                        </tr>
                    </xsl:for-each>
                </table>
                <h4>NUTRIENTS</h4>
                <table style="width:100%">
                    <tr>
                        <th>Name</th>
                        <th>Value</th>
                    </tr>
                    <xsl:for-each select="Recipe/Nutritions/Nutrient">
                        <tr>
                            <td>
                                 <xsl:value-of select="Name"/>
                            </td>
                            <td>
                                 <xsl:value-of select="Value"/>
                            </td>
                        </tr>
                    </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>

</xsl:stylesheet>

任何和所有的帮助将不胜感激。

xml xslt transformation
1个回答
0
投票

您的代码中存在一些小错误:

  • ẀithRecipe/Ingredients/Ingredient你正在迭代所有Ingredient元素。那是对的。但是使用Recipe/Directions/Steps,你会迭代所有Steps元素 - 这正是一个元素。但是Step儿童本身就是Steps元素的孩子。所以你的XPath是错误的,应该是Recipe/Directions/Steps/Step而不是迭代所有13个Steps,然后在text()中选择他们的xsl:value-of内容。
  • 选择与text()相关的Step就像选择与Name相关的Ingredient - 它选择一个子节点。您也可以使用.而不是text(),但我选择了text(),因为它更具说明性。

因此,将Directions子节的XSLT更改为

<h4>DIRECTIONS</h4>
<table style="width:100%">
    <tr>
        <th>Step</th>
    </tr>
    <xsl:for-each select="Recipe/Directions/Steps/Step">
        <tr>
            <td>
                 <xsl:value-of select="text()"/>
            </td>
        </tr>
    </xsl:for-each>
    <tr>
        <td>
             <b>Hint: </b><xsl:value-of select="Recipe/Directions/Hint"/>
        </td>
    </tr>                
</table>

输出:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.