我有以下模板如何检查标记是否存在于XSLT中?
<h2>one</h2>
<xsl:apply-templates select="one"/>
<h2>two</h2>
<xsl:apply-templates select="two"/>
<h2>three</h2>
<xsl:apply-templates select="three"/>
我想,如果有相应的模板中的至少一个构件,以只显示标题(一个,两个,三个)。我如何检查这个?
我有以下模板如何检查标记是否存在于XSLT中?
<h2>one</h2>
<xsl:apply-templates select="one"/>
<h2>two</h2>
<xsl:apply-templates select="two"/>
<h2>three</h2>
<xsl:apply-templates select="three"/>
我想,如果有相应的模板中的至少一个构件,以只显示标题(一个,两个,三个)。我如何检查这个?
<xsl:if test="one">
<h2>one</h2>
<xsl:apply-templates select="one"/>
</xsl:if>
<!-- etc -->
或者,你可以创建一个命名模板,
<xsl:template name="WriteWithHeader">
<xsl:param name="header"/>
<xsl:param name="data"/>
<xsl:if test="$data">
<h2><xsl:value-of select="$header"/></h2>
<xsl:apply-templates select="$data"/>
</xsl:if>
</xsl:template>
,然后调用为:
<xsl:call-template name="WriteWithHeader">
<xsl:with-param name="header" select="'one'"/>
<xsl:with-param name="data" select="one"/>
</xsl:call-template>
不过说实话,看起来像更多的工作对我来说...只有在绘制标题时非常有用...对于一个简单的<h2>...</h2>
,我很想让它内联。
如果标题标题总是节点名称,你可以通过删除“$头” ARG simplifiy模板,并使用来代替:
<xsl:value-of select="name($header[1])"/>
我喜欢运动XSL的功能方面这使我实现如下:
<?xml version="1.0" encoding="UTF-8"?>
<!-- test data inlined -->
<test>
<one>Content 1</one>
<two>Content 2</two>
<three>Content 3</three>
<four/>
<special>I'm special!</special>
</test>
<!-- any root since take test content from stylesheet -->
<xsl:template match="/">
<html>
<head>
<title>Header/Content Widget</title>
</head>
<body>
<xsl:apply-templates select="document('')//test/*" mode="header-content-widget"/>
</body>
</html>
</xsl:template>
<!-- default action for header-content -widget is apply header then content views -->
<xsl:template match="*" mode="header-content-widget">
<xsl:apply-templates select="." mode="header-view"/>
<xsl:apply-templates select="." mode="content-view"/>
</xsl:template>
<!-- default header-view places element name in <h2> tag -->
<xsl:template match="*" mode="header-view">
<h2><xsl:value-of select="name()"/></h2>
</xsl:template>
<!-- default header-view when no text content is no-op -->
<xsl:template match="*[not(text())]" mode="header-view"/>
<!-- default content-view is to apply-templates -->
<xsl:template match="*" mode="content-view">
<xsl:apply-templates/>
</xsl:template>
<!-- special content handling -->
<xsl:template match="special" mode="content-view">
<strong><xsl:apply-templates/></strong>
</xsl:template>
一旦容纳在测试元件体所有元素具有首标内容的插件施加(按文档顺序)。
默认首标内容的插件模板(匹配“*”)的第一施加头视点然后施加一个内容视图当前元素。
默认头视图模板在H2标签放置当前元素的名。默认的content-view应用通用处理规则。
如果不存在由[not(text())]谓词判断的内容,则不会出现该元素的输出。
一关特价个案很容易处理。
更精确:)你的XML文件就像你想使用这个模板? – kender 2008-10-12 08:53:42