【问题标题】:XSLT won't create correct number of rows upon transformationXSLT 在转换时不会创建正确的行数
【发布时间】:2018-08-10 01:57:52
【问题描述】:

我正在尝试从 XML 创建一个 XLST 文件,但问题是当我将 XSLT 转换为 HTML 时,输出与预期不同。问题是,当我运行转换时,我只得到一行 Steps,即只显示第一步,而其他所有步骤都没有。输出可见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】:

    您的代码中有一些小错误:

    • Ẁith Recipe/Ingredients/Ingredient 您正在遍历所有 Ingredient 元素。这是正确的。 但是使用Recipe/Directions/Steps,您将遍历所有Steps 元素——这正是一个元素。但是Step 子元素本身就是Steps 元素的子元素。所以你的 XPath 是错误的,应该是 Recipe/Directions/Steps/Step,而不是遍历所有 13 个 Steps,然后在 xsl:value-of 中选择它们的 text() 内容。
    • 相对于Step 选择text() 就像相对于Ingredient 选择Name - 它选择一个子节点。您也可以使用. 代替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>
    

    输出:

    【讨论】:

    • 这就像一个魅力。但为什么要使用 select="text()"?为什么它不像我编写的那样工作?
    • 你也可以使用select="."。那也行。但我的版本更明确。 --- 它没有按照您的编码方式工作,因为您迭代(for-each 循环)错误的元素 Directionsselected 错误的孩子 Hint 而不是 Step。所以你的输出几乎是空的。
    • 谢谢。但这实际上是一个错字。我最初打算使用: 但这似乎也不起作用
    • 我们只能在您发布代码时告诉您代码中的错误。我们无法判断代码中的错误是由于输入错误还是由于对语言概念的根本误解造成的,我们通常不会尝试。
    • @MichaelKay 是的,我知道这只是在编辑帖子时有时会令人困惑。无论如何,我更新了 XSLT 文件以显示我在说什么,但它仍然只显示 1 个步骤。任何关于为什么现在发生的帮助将不胜感激
    猜你喜欢
    • 2011-07-10
    • 1970-01-01
    • 1970-01-01
    • 2011-08-21
    • 1970-01-01
    • 1970-01-01
    • 2017-06-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多