0
我有一個XSLT樣式表,其中包含多個單獨工作的模板,但是我很難拼湊在一起。看來我可以調用PAR模板或TABLE模板,但不能同時使用這兩種模板。我怎麼把它放在一起?XSLT調用多個模板
這裏是XML:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="AllTogether.xslt"?>
<document>
<item name="Some bullets">
<richtext>
<pardef/>
<par def="20">
<run>This is the </run>
<run><font style="underline"/>preamble.</run>
</par>
<pardef id="21" list="bullet"/>
<par def="21">
<run>This is the </run>
<run>first bullet.</run>
</par>
<par def="20">
<run/>
</par>
<par def="21">
<run>This is the second </run>
<run>bullet.</run>
</par>
<par def="20">
<run>This is the </run>
<run>conclusion.</run>
</par>
</richtext>
</item>
<item name="A table">
<richtext>
<table>
<tablerow>
<tablecell>
<par def="43"><run>Total Savings ($M)</run></par></tablecell>
<tablecell>
<pardef/>
<par def="50"><run></run></par></tablecell>
<tablecell>
<par def="44"><run>0.9360</run></par></tablecell>
<tablecell>
<par def="45"><run>5.0047</run></par></tablecell>
<tablecell>
<par def="46"><run>8.8080</run></par></tablecell></tablerow></table>
</richtext>
</item>
</document>
下面是所需的輸出:
<html>
<head/>
<body>
<table border="1">
<tbody>
<tr>
<td>Some bullets</td>
<td>
<p>This is the <span style="text-decoration: underline;">preamble.</span></p><ul>
<li>This is the first bullet.</li></ul>
<p></p><ul>
<li>This is the second bullet.</li></ul>
<p>This is the conclusion.</p>
</td>
</tr>
<tr>
<td>A table</td>
<td>
<table border="1">
<tr>
<td>Total Savings ($M)</td>
<td></td>
<td>0.9360</td>
<td>5.0047</td>
<td>8.8080</td>
</tr>
</table>
</td>
</tr>
</tbody>
</table>
</body>
,這裏是樣式表:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output indent="yes" method="html"/>
<xsl:template match="/*">
<html>
<body>
<table border="1">
<tbody>
<xsl:apply-templates/>
</tbody>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="item">
<tr>
<td><xsl:value-of select="@name"/></td>
<td>
<xsl:apply-templates/>
</td>
</tr>
</xsl:template>
<xsl:template match="table">
<table border="1">
<xsl:for-each select="tablerow">
<tr>
<xsl:for-each select="tablecell">
<td>
<xsl:apply-templates />
</td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</xsl:template>
<xsl:template match="richtext/par">
<xsl:for-each-group select="par[run[normalize-space()]]" group-adjacent="if (@def) then @def else preceding-sibling::par[run[normalize-space()]][@def][1]/@def">
<xsl:variable name="listType" select="preceding-sibling::*[1][self::pardef]/@list" />
<xsl:choose>
<xsl:when test="$listType = 'unordered'">
<ul>
<xsl:apply-templates select="current-group()" mode="list"/>
</ul>
</xsl:when>
<xsl:when test="$listType = 'ordered'">
<ol>
<xsl:apply-templates select="current-group()" mode="list"/>
</ol>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="current-group()" mode="para" />
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:template>
<xsl:template match="par" mode="list">
<li>
<xsl:value-of select="run" separator=""/>
</li>
</xsl:template>
<xsl:template match="par" mode="para">
<p>
<xsl:value-of select="run" separator=""/>
</p>
</xsl:template>
<xsl:template match="run">
<xsl:choose>
<xsl:when test="font[@style = 'underline']">
<span style="text-decoration: underline;">
<xsl:value-of select="." separator=""/>
</span>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="text()" separator=""/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>