2012-10-24 32 views
1

我花了大約6-7個小時試圖讓一個基本的xml頁面至少使用xsl顯示可讀性,並且無法讓我的頭轉向它。這很難嗎?或者我正在掙扎大時間。在xslt中顯示xml簡單

我的XML代碼是基本

<?xml version="1.0" encoding="utf-8"?> 
<?xml-stylesheet type="text/xsl" href="recipepages.xsl"?><!DOCTYPE chefmaster [ 
<!ELEMENT chefmaster (recipe+)> 
    <!ELEMENT recipe (title, dishtype?, dishcategory?, chef, countryshown, episodeshown?, preparationtime, cookingtime, serves, caloriesperserve?, allergenslist?, ingredients, method, recommendedbeverage?, author*, mainingredientlink+)> 
    <!ATTLIST recipe cuisine (chinese|french|greek|indian|italian|japanese|mexican|notspecified|thai|western) #REQUIRED> 
    <!ELEMENT title (#PCDATA)> 
    <!ELEMENT dishtype (#PCDATA)> 
    <!ELEMENT dishcategory (#PCDATA)> 
    <!ELEMENT chef (#PCDATA)> 
    <!ELEMENT countryshown (#PCDATA)> 
    <!ELEMENT episodeshown (#PCDATA)> 
    <!ELEMENT preparationtime (#PCDATA)> 
    <!ELEMENT cookingtime (#PCDATA)> 
    <!ELEMENT serves (#PCDATA)> 
    <!ELEMENT caloriesperserve (#PCDATA)> 
    <!ELEMENT allergenslist (#PCDATA)> 
    <!ELEMENT ingredients (main, ingredient+)> 
    <!ELEMENT main (#PCDATA)> 
    <!ATTLIST main image CDATA #REQUIRED> 
    <!ELEMENT ingredient (#PCDATA)> 
    <!ATTLIST ingredient amount CDATA #IMPLIED> 
    <!ATTLIST ingredient units CDATA #IMPLIED> 
    <!ELEMENT method (step+)> 
    <!ELEMENT step (#PCDATA)> 
    <!ELEMENT recommendedbeverage (#PCDATA)> 
    <!ELEMENT author (givenname, surname)> 
    <!ELEMENT givenname (#PCDATA)> 
    <!ELEMENT surname (#PCDATA)> 
    <!ELEMENT mainingredientlink (#PCDATA)> 
]> 
<chefmaster> 
    <recipe cuisine="notspecified"> 
    <title>Quick carrot and coriander soup</title> 
    <dishtype>Entree</dishtype> 
    <dishcategory>Vegetarian</dishcategory> 
    <chef>Alison Good</chef> 
    <countryshown>Australia</countryshown> 
    <episodeshown>Season 2, Episode 1</episodeshown> 
    <preparationtime>5 mins</preparationtime> 
    <cookingtime>15 mins</cookingtime> 
    <serves>6</serves> 
    <caloriesperserve>85</caloriesperserve> 
    <allergenslist>None</allergenslist> 
    <ingredients> 
     <main image="http://1.bp.blogspot.com/-XaAgu2H_RDk/TmxXBKiOJ8I/AAAAAAAABQg/DrMxd41-b8s/s1600/carrot.jpg">Carrot</main> 
     <ingredient amount="4">Large carrots, peeled and cut into large chunks</ingredient> 
     <ingredient amount="1" units="tablespoon">Olive oil</ingredient> 
     <ingredient amount="1/4">Large onion, diced</ingredient> 
     <ingredient amount="4" units="cups">Vegetable stock</ingredient> 
     <ingredient>Large bunch of fresh coriander, de-stemmed and roughly chopped</ingredient> 
    </ingredients> 
    <method> 
     <step>1. Heat the oil in a large saucepan over medium heat and saute the carrots and onion for a few minutes until the onion has softened a little.</step> 
     <step>2. Pour in the vegetable stock and add the coriander. Bring to a boil and cook until the carrots are tender; about 10 minutes. Remove from the heat and allow to cool slightly.</step> 
     <step>3. Puree the soup until smooth using a hand-blender or food processor. Reheat before serving if necessary.</step> 
    </method> 
    <recommendedbeverage>Water</recommendedbeverage> 
    <author> 
     <givenname>Sarah</givenname> 
     <surname>May</surname> 
    </author> 
    <mainingredientlink>http://en.wikipedia.org/wiki/Carrot</mainingredientlink> 
    </recipe> 
    <recipe cuisine="greek"> 
    <title>Prawns (garides) saganaki</title> 
    <dishtype>Entree</dishtype> 
    <chef>Alison Good</chef> 
    <countryshown>Australia</countryshown> 
    <episodeshown>Season 2, Episode 3</episodeshown> 
    <preparationtime>5 mins</preparationtime> 
    <cookingtime>35 mins</cookingtime> 
    <serves>4</serves> 
    <caloriesperserve>287</caloriesperserve> 
    <allergenslist>Seafood</allergenslist> 
    <ingredients> 
     <main image="http://images.wikia.com/prawnhub/images/d/df/Going_to_be_a_Fairy_Prawn.jpg">Prawns</main> 
     <ingredient amount="500" units="grams">Raw king prawns</ingredient> 
     <ingredient amount="3" units="tablespoons">Olive oil</ingredient> 
     <ingredient amount="1">Onion, chopped</ingredient> 
     <ingredient amount="1" units="teaspoon">Freshly chopped parsley</ingredient> 
     <ingredient>Large bunch of fresh coriander, de-stemmed and roughly chopped</ingredient> 
     <ingredient amount="1" units="cup">White wine</ingredient> 
     <ingredient amount="400" units="gram">Tin chopped tomatoes, drained</ingredient> 
     <ingredient amount="1" units="clove">Garlic, finely chopped</ingredient> 
     <ingredient amount="200" units="grams">Feta cheese, cubed</ingredient> 
    </ingredients> 
    <method> 
     <step>1. Place the prawns in a pot and add enough water to cover them. Boil for 5 minutes, then drain, reserving the liquid and set aside.</step> 
     <step>2. Heat about 2 tablespoons of oil in a saucepan. Add the onion and cook and stir until the onions are soft. Mix in the parsley, wine, tomatoes, garlic and remaining olive oil.</step> 
     <step>3. Simmer, stirring occasionally, for about 30 minutes, or until the sauce is thickened.</step> 
     <step>4. While the sauce is simmering, the prawns should have become cool enough to handle. First remove the legs by pinching them, and then pull off the shells, leaving the head and tail on.</step> 
     <step>5. When the sauce has thickened, stir in the prawns. Bring to a simmer, and cook for about 5 minutes.</step> 
     <step>6. Add the feta cheese and remove from the heat. Let stand until the cheese starts to melt. Serve warm.</step> 
    </method> 
    <author> 
     <givenname>George</givenname> 
     <surname>Adams</surname> 
    </author> 
    <author> 
     <givenname>Alex</givenname> 
     <surname>Antonovich</surname> 
    </author> 
    <mainingredientlink>http://en.wikipedia.org/wiki/Shrimp_meat</mainingredientlink> 
    </recipe> 
    <recipe cuisine="greek"> 
    <title>Spicy Baked Feta</title> 
    <dishtype>Entree</dishtype> 
    <dishcategory>Vegetarian</dishcategory> 
    <chef>Neil Perry</chef> 
    <countryshown>Australia</countryshown> 
    <episodeshown>Season 2, Episode 3</episodeshown> 
    <preparationtime>3 mins</preparationtime> 
    <cookingtime>10 mins</cookingtime> 
    <serves>4</serves> 
    <caloriesperserve>267</caloriesperserve> 
    <allergenslist>Chilli</allergenslist> 
    <ingredients> 
     <main image="http://www.gaganisbros.com.au/images/Greek_Feta_Texture.jpg">Feta Cheese</main> 
     <ingredient amount="250" units="gram">Slab of feta cheese</ingredient> 
     <ingredient amount="3 to 4" units="tablespoons">Chilli flakes</ingredient> 
     <ingredient units="pinch">Oregano</ingredient> 
     <ingredient>Olive oil</ingredient> 
    </ingredients> 
    <method> 
     <step>1. Preheat oven 150 degrees C.</step> 
     <step>2. Place feta in small baking dish. Drizzle with olive oil. Cover liberally with chili flakes, and a sprinkle of oregano.</step> 
     <step>3. Bake uncovered for 10 minutes until feta is soft.</step> 
     <step>4. Serve with crusty bread or toast.</step> 
    </method> 
    <recommendedbeverage>Glass of shiraz</recommendedbeverage> 
    <author> 
     <givenname>Christina</givenname> 
     <surname>Motso</surname> 
    </author> 
    <mainingredientlink>http://en.wikipedia.org/wiki/Feta</mainingredientlink> 
    <mainingredientlink>http://culinaryarts.about.com/od/glossary/g/Feta.htm</mainingredientlink> 
    </recipe> 
</chefmaster> 

是否有可能以顯示它,這樣的食譜可以很容易地在一個xsl文檔的一些基本線讀?

+1

你需要包括你真正想要它顯示。這很容易,儘管如果你不習慣聲明式或(種類)函數式編程,起初它似乎有點外國人。 –

+0

請提供確切需要的輸出以及您希望轉換實施(服從)的任何規則。 –

回答

0

您可以使用一些通用規則將一個快速又髒的格式轉換爲HTML格式,然後通過添加更多規則使其更漂亮來對其進行自定義。這是一個初學者,它根據文檔的深度顯示元素和屬性;它不是美麗的,但它是一個起點:

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

<xsl:template match="/"> 
    <html><body><xsl:apply-templates/></body></html> 
</xsl:template> 

<xsl:template match="*/*"> 
    <h1> 
    <xsl:value-of select="name()"/> 
    <xsl:apply-templates select="@*"/> 
    </h1> 
    <table> 
    <xsl:apply-templates select="*"/> 
    </table> 
</xsl:template> 

<xsl:template match="*/*/*"> 
    <tr> 
    <td><xsl:value-of select="name()"/></td> 
    <td><xsl:apply-templates/></td> 
    </tr> 
</xsl:template> 

<xsl:template match="*/*/*/*"> 
    <dt> 
    <xsl:value-of select="name()"/> 
    <xsl:apply-templates select="@*"/> 
    </dt> 
    <dd><xsl:value-of select="."/></dd> 
</xsl:template> 

<xsl:template match="@*"> 
    <xsl:value-of select="concat(' ', name(), ': ', .)"/> 
</xsl:template> 
</xsl:stylesheet>