初学xml vs xslt

xml file:

 1<manual id="model-rocket" type="assembly">
 2<parts-list>
 3<part count="1" label="A">fuselage, left half</part>
 4<part count="1" label="B">fuselage, right half</part>
 5<part count="4" label="F">steering fin</part>
 6<part count="3" label="N">rocket nozzle</part>
 7<part count="1" label="C">crew capsule</part>
 8</parts-list>
 9<instructions>
10<step>   
11Glue <part ref="A"></part> and <part ref="B"></part> together to form the   
12fuselage.   
13</step>
14<step>   
15For each <part ref="F"></part>, apply glue and insert it into slots in the   
16fuselage.   
17</step>
18<step>   
19Affix <part ref="N"></part> to the fuselage bottom with a small amount of   
20glue.   
21</step>
22<step>   
23Connect <part ref="C"></part> to the top of the fuselage. Do not use   
24any glue, as it is spring-loaded to detach from the fuselage.   
25</step>
26</instructions>
27</manual>

xslt file:

 1<xsl:stylesheet version="1.0" xmlns:xsl=" http://www.w3.org/1999/XSL/Transform ">
 2<xsl:output encoding="ISO-8859-1" method="html"></xsl:output>
 3<xsl:template match="manual">
 4<html>
 5<head><title>Instrutions Guide</title></head>
 6<body>
 7<h1>Instructions Guide</h1>
 8<xsl:apply-templates></xsl:apply-templates>
 9</body>
10</html>
11</xsl:template>
12<xsl:template match="parts-list">
13<h2>Parts</h2>
14<dl>
15<xsl:apply-templates></xsl:apply-templates>
16</dl>
17</xsl:template>
18<xsl:template match="part[@label]">
19<dt>
20<xsl:value-of select="@label"></xsl:value-of>
21</dt>
22<dd>
23<xsl:apply-templates></xsl:apply-templates>
24</dd>
25</xsl:template>
26<xsl:template match="part[@ref]">
27<xsl:variable name="label" select="@ref"></xsl:variable>
28<xsl:value-of select="//part[@label=$label]"></xsl:value-of>
29<xsl:text> (Part </xsl:text>
30<xsl:value-of select="@ref"></xsl:value-of>
31<xsl:text> ) </xsl:text>
32</xsl:template>
33</xsl:stylesheet>

属性的设置可以覆盖的,所以如果第一个template是对manaul的设置,并不
意味着将整个xml文件按照设置输出,还要看后面的对各个element的样式设置.

Published At
Categories with Web编程
Tagged with
comments powered by Disqus