在xslt中显示简单的xml

时间:2012-10-24 01:28:36

标签: xml xslt

我花了大约6-7个小时试图获得一个基本的xml页面,至少使用xsl显示可读性,并且无法理解它。这难吗?或者我只是在苦苦挣扎。

我的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 doc中的几个基本行中轻松阅读配方?

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>