我有以下模板如何檢查標記是否存在於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