2012-04-26 66 views
1

我有一個食譜的XML文件。每個配方都使用一個元素定義,My XSL我想用<xsl:for-each select="//recipe">填充一個表格。唯一的問題是我沒有得到理想的結果。除了for-each之外的所有東西都可以正常工作,並且可以顯示,但for-each中的所有內容都沒有出現。XML和XSL沒有得到期望的結果

我沒有看到我的XSL中有任何錯誤,但後來我不是最好的創建它們。任何幫助,將不勝感激。

輸出:

Testing HTML 
Testing Body 
Testing Table 

XML

<?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet type="text/xsl" href="recipe.xsl"?> 
<recipesXML xmlns="http://www.w3schools.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3schools.com recipe.xsd "> 
    <recipe> 
     <name> Burnt Cookies </name> 
     <image>images/burntcookie.jpg</image> 
     <prepTime> 120 min </prepTime> 
     <cookTime> 300 min </cookTime> 
     <ingredients>Animal Crackers</ingredients> 
     <instructions> 
      <step num="1">Place Animal Crackers on a pan</step> 
      <step num="2">Place Pan in Oven</step> 
      <step num="3"> ... </step> 
      <step num="4">Profit</step> 
     </instructions> 
    </recipe> 
    <recipe> 
     <name>Delicious Black Bean Burritos</name> 
     <image>images/</image> 
     <prepTime>10 min</prepTime> 
     <cookTime>15 min</cookTime> 
     <ingredients>2 (10 inch) flour tortillas</ingredients> 
     <ingredients>2 tablespoons vegetable oil</ingredients> 
     <ingredients>1 small onion, chopped</ingredients> 
     <ingredients>1/2 red bell pepper, chopped</ingredients> 
     <ingredients>1 teaspoon minced garlic</ingredients> 
     <ingredients>1 (15 ounce) can black beans, rinsed and drained</ingredients> 
     <ingredients>1 teaspoon minced jalapeno peppers</ingredients> 
     <ingredients>3 ounces cream cheese</ingredients> 
     <ingredients>1/2 teaspoon salt</ingredients> 
     <ingredients>2 tablespoons chopped fresh cilantro</ingredients> 
     <instructions> 
      <step num="1">Wrap tortillas in foil and place in oven heated to 350 degrees F (175 degrees C). Bake for 15 minutes or until heated through.</step> 
      <step num="2">Heat oil in a 10-inch skillet over medium heat. Place onion, bell pepper, garlic and jalapenos in skillet, cook for 2 minutes stirring occasionally. Pour beans into skillet, cook 3 minutes stirring.</step> 
      <step num="3">Cut cream cheese into cubes and add to skillet with salt. Cook for 2 minutes stirring occasionally. Stir cilantro into mixture.</step> 
      <step num="4">Spoon mixture evenly down center of warmed tortilla and roll tortillas up. Serve immediately.</step> 
     </instructions> 
    </recipe> 
    <recipe> 
     <name>Cream of Asparagus Soup</name> 
     <image>images/aspSoup.jpg</image> 
     <prepTime>30 min</prepTime> 
     <cookTime>30 min</cookTime> 
     <ingredients>1/4 cup margarine</ingredients> 
     <ingredients>1 onion, chopped</ingredients> 
     <ingredients>3 stalks celery, chopped</ingredients> 
     <ingredients>3 tablespoons all-purpose flour</ingredients> 
     <ingredients>4 cups water</ingredients> 
     <ingredients>1 (10.5 ounce) can condensed chicken broth</ingredients> 
     <ingredients>4 tablespoons chicken bouillon powder</ingredients> 
     <ingredients>1 potato, peeled and diced</ingredients> 
     <ingredients>1 pound fresh asparagus, trimmed and coarsely chopped</ingredients> 
     <ingredients>3/4 cup half-and-half</ingredients> 
     <ingredients>1 tablespoon soy sauce</ingredients> 
     <ingredients>1/4 teaspoon ground black pepper</ingredients> 
     <ingredients>1/4 teaspoon ground white pepper</ingredients> 
     <instructions> 
      <step num="1">Melt butter or margarine in a heavy cooking pot. Add onions and chopped celery; saute until tender, about 4 minutes. Stir in flour, mixing well. Cook for about 1 minute, stirring constantly. Do not burn, or let it go lumpy. Add water, chicken broth, and chicken soup base; stir until smooth. Bring to a boil. Add diced potatoes and chopped asparagus. Reduce heat, and simmer for about 20 minutes.</step> 
      <step num="2">Puree soup in a food processor or blender in batches. Return to pot.</step> 
      <step num="3">Stir in half and half cream, soy sauce, and black and white pepper. Bring soup just to boil. Adjust seasonings to taste. Serve hot.</step> 
     </instructions> 
    </recipe> 
    <recipe> 
     <name>Carrot Cake IV</name> 
     <image>images/carrotcake.jpg</image> 
     <prepTime>30 min</prepTime> 
     <cookTime>30 min</cookTime> 
     <ingredients>1 (18.5 ounce) package carrot cake mix</ingredients> 
     <ingredients>1/2 cup water</ingredients> 
     <ingredients>1/2 cup vegetable oil</ingredients> 
     <ingredients>3 eggs</ingredients> 
     <ingredients>1/2 cup finely chopped walnuts</ingredients> 
     <ingredients>1 (8 ounce) can crushed pineapple with juice</ingredients> 
     <instructions> 
      <step num="1">Preheat oven to 350 degrees F (175 degrees C). Grease and flour two 8 inch round pans.</step> 
      <step num="2">In large bowl, blend cake mix, oil, water, undrained pineapple, eggs, and nuts. Beat for 2 minutes with an electric mixer at medium speed. Spread batter into prepared pans.</step> 
      <step num="3">Bake for 35 to 40 minutes, or until a toothpick inserted in the center comes out clean. Cool. Frost with cream cheese frosting.</step> 
     </instructions> 
    </recipe> 
    <recipe> 
     <name>Eggy Veggie Bake</name> 
     <image>images/eggveggie.jpg</image> 
     <prepTime>10 min</prepTime> 
     <cookTime>35 min</cookTime> 
     <ingredients>1 tablespoon olive oil</ingredients> 
     <ingredients>1 cup sliced halved zucchini</ingredients> 
     <ingredients>3 green onions, chopped</ingredients> 
     <ingredients>1/2 sweet onion, thinly sliced</ingredients> 
     <ingredients>2 roma (plum) tomatoes, chopped</ingredients> 
     <ingredients>1/2 cup chopped fresh mushrooms</ingredients> 
     <ingredients>3 cups chopped baby spinach</ingredients> 
     <ingredients>1/2 lemon, juiced</ingredients> 
     <ingredients>Worcestershire sauce to taste</ingredients> 
     <ingredients>hot sauce to taste</ingredients> 
     <ingredients>garlic powder to taste</ingredients> 
     <ingredients>salt and ground black pepper to taste</ingredients> 
     <ingredients>1 1/2 cups liquid egg substitute</ingredients> 
     <ingredients>1/4 cup shredded Cheddar cheese</ingredients> 
     <instructions> 
      <step num="1">Preheat oven to 350 degrees F (175 degrees C). Spray an 8x8 inch baking dish with cooking spray.</step> 
      <step num="2">Heat the olive oil in a skillet over medium-high heat. Place zucchini, green onions, onion, tomatoes, mushrooms, and spinach in the skillet. Sprinkle with lemon juice, Worcestershire sauce, and hot sauce. Season with garlic powder, salt, and pepper. Cook until tender. Transfer to the prepared baking dish. Pour egg substitute over the vegetables in the dish. Top with cheese.</step> 
      <step num="3">Bake 20 minutes in the preheated oven, or until egg substitute is set and cheese is melted. Cut with a spatula and enjoy!</step> 
     </instructions> 
    </recipe> 
    <recipe> 
     <name>Chicken and Bacon Shish Kabobs</name> 
     <image>images/ChickenBaconShish.jpg</image> 
     <prepTime>25 min</prepTime> 
     <cookTime>20 min</cookTime> 
     <ingredients>1/4 cup soy sauce</ingredients> 
     <ingredients>1/4 cup cider vinegar</ingredients> 
     <ingredients>2 tablespoons honey</ingredients> 
     <ingredients>2 tablespoons canola oil</ingredients> 
     <ingredients>10 large mushrooms, cut in half</ingredients> 
     <ingredients>2 green onions, minced</ingredients> 
     <ingredients>3 skinless, boneless chicken breast halves - cut into chunks</ingredients> 
     <ingredients>1/2 pound sliced thick cut bacon, cut in half</ingredients> 
     <ingredients>1 (8 ounce) can pineapple chunks, drained</ingredients> 
     <ingredients>skewers</ingredients> 
     <instructions> 
      <step num="1">In a large bowl, mix the soy sauce, cider vinegar, honey, canola oil, and green onions. Place the mushrooms and chicken into the mixture, and stir to coat. Cover, and marinate in the refrigerator at least 1 hour.</step> 
      <step num="2">Preheat grill for high heat.</step> 
      <step num="3">Remove the mushrooms and chicken from the marinade and shake off excess. Pour the marinade into a small saucepan and bring to a boil over high heat. Reduce heat to medium-low and simmer for 10 minutes; set aside.</step> 
      <step num="4">Wrap the chicken chunks with bacon, and thread onto skewers so that the bacon is secured. Alternate with mushroom halves and pineapple chunks.</step> 
      <step num="5">Lightly oil the grill grate. Arrange skewers on the prepared grill. Cook 15 to 20 minutes, brushing occasionally with remaining soy sauce mixture, until bacon is crisp and chicken juices run clear.</step> 
     </instructions> 
    </recipe> 
    <recipe> 
     <name>Amish Bread</name> 
     <image>images/amishbread.jpg</image> 
     <prepTime>5 min</prepTime> 
     <cookTime>180 min</cookTime> 
     <ingredients>2 3/4 cups bread flour</ingredients> 
     <ingredients>1/4 cup canola oil</ingredients> 
     <ingredients>1 teaspoon active dry yeast</ingredients> 
     <ingredients>1/4 cup white sugar</ingredients> 
     <ingredients>1/2 teaspoon salt</ingredients> 
     <ingredients>18 tablespoons warm water</ingredients> 
     <instructions> 
      <step num="1">Place ingredients in the pan of the bread machine in the order recommended by the manufacturer. Select White Bread cycle; press Start.</step> 
      <step num="2">When the dough has raised once and second cycle of kneading begins, turn machine off. Reset by pressing Start once again. This gives the dough two full raising cycles before the final raising cycle prior to baking.</step> 
     </instructions> 
    </recipe> 
    <recipe> 
     <name>Pizza Crust Fantastico</name> 
     <image>images/pizzaCrustFantastico.jpg</image> 
     <prepTime>20 min</prepTime> 
     <cookTime>15 min</cookTime> 
     <ingredients>1 1/2 cups warm water (110 degrees F/45 degrees C)</ingredients> 
     <ingredients>1 tablespoon honey</ingredients> 
     <ingredients>1 3/4 teaspoons active dry yeast</ingredients> 
     <ingredients>2 1/2 cups all-purpose flour</ingredients> 
     <ingredients>3/4 cup whole wheat flour</ingredients> 
     <ingredients>1/4 cup corn flour</ingredients> 
     <ingredients>1 teaspoon salt</ingredients> 
     <ingredients>1 teaspoon ground black pepper</ingredients> 
     <ingredients>1 teaspoon dried oregano</ingredients> 
     <ingredients>1 teaspoon dried basil leaves</ingredients> 
     <ingredients>1 teaspoon dried rosemary, chopped</ingredients> 
     <ingredients>1/2 cup tomato sauce</ingredients> 
     <ingredients>2 tablespoons olive oil</ingredients> 
     <instructions> 
      <step num="1">Pour warm water into a small bowl. Dissolve honey and yeast in water. Let stand until creamy, about 10 minutes.</step> 
      <step num="2">In a large bowl, combine the all-purpose flour, whole wheat flour, corn flour, salt, black pepper, oregano, basil and rosemary. Add the yeast mixture, tomato sauce and olive oil; stir well to combine.</step> 
      <step num="3">When the dough has pulled together, turn it out onto a lightly floured surface and knead until smooth and elastic, about 8 minutes. Lightly oil a large bowl, place the dough in the bowl and turn to coat with oil. Cover with a damp cloth and let rise in a warm place until doubled in volume, about 1 hour. Meanwhile, preheat oven to 450 degrees F (230 degrees C) place pizza stone in oven if one is to be used.</step> 
      <step num="4">Deflate the dough and turn it out onto a lightly floured surface. Divide the dough into two equal pieces and form into rounds, cover and let rest for 10 minutes.</step> 
      <step num="5">Roll the rounds out to crust size. Spread crusts with desired pizza sauce from the center outward and cover with desired toppings. Bake on preheated pizza stone or on lightly oiled pizza pans until cheese is bubbly and golden, about 12 minutes</step> 
     </instructions> 
    </recipe> 
</recipesXML> 

XSL

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/"> 
     <html> 
      Testing HTML 
      <body> 
       Testing Body 
       <table border="1"> 
       Testing Table 
        <xsl:for-each select="//recipe"> 
         Testing Each Recipe 
         <tr> 
          <td> 
           Test 
           <xsl:element name="img"> 
            <xsl:attribute name="src"><xsl:value-of select="image"/></xsl:attribute> 
            <xsl:attribute name="alt">recipe</xsl:attribute> 
           </xsl:element> 
          </td> 
          <td> 
           <xsl:value-of select="name"></xsl:value-of> 
          </td> 
         </tr> 
        </xsl:for-each> 
       </table> 
      </body> 
     </html> 
    </xsl:template> 
</xsl:stylesheet> 

回答

4

這是因爲在XML的默認命名空間xmlns="http://www.w3schools.com"的。

您可以:

  1. 從XML刪除它。

  2. 使用前綴添加名稱空間,並在select中使用前綴。 (select="//x:recipe"

  3. 如果您使用的是XSLT 2.0,請使用*作爲select中的前綴。 (select="//*:recipe"

  4. 在select中使用local-name()。 (select="//*[local-name() = 'recipe']"

+0

Eclipse中悄悄過去一個我看來它寫。這就是爲什麼我通常在記事本++中編寫XML的東西。謝謝,我放鬆了它,以方便。 – meriley 2012-04-26 17:05:03

+0

@meriley - 你很受歡迎。我使用oXygen作爲我的XML編輯器;既可以作爲獨立的插件,也可以作爲Eclipse插件使用。試一試。我敢打賭你會比記事本++更喜歡它:-) – 2012-04-26 18:43:27

1

順便說一句,這img元素可以使用

<img src="{image}" alt="recipe"/>