2013-04-25 83 views
1

我正在將XML文檔轉換爲HTML文件以供在線顯示(作爲電子書)。使用模式忽略XSLT模板中的子節點

XML文件中的每一章都包含在<div>之內,並有一個標題(<head>)。我需要顯示每個標題兩次 - 一次是開始時的內容列表的一部分,第二次是每章的頂部。我在<xsl:template>之內使用了mode="toc"來執行此操作。

我的問題是,我的一些<head>標題有一個子元素<note>,其中包含編輯腳註。我需要當標題出現在章節的頂部這些<note>標籤進行處理,但我不希望他們在目錄中顯示(即當mode="toc"

我的問題是如何告訴樣式表?來處理表的內容<head>元素,但忽略任何子元素(他們應該發生)

下面是一個例子沒有注意到,它在內容模式表顯示細膩標題:

<div xml:id="d1.c1" type="chapter"> 
    <head>Pursuit of pleasure. Limits set to it by Virtue— 
    Asceticism is Vice</head> 
    <p>Contents of chapter 1 go here</p> 
</div> 

而且這裏有一張紙條,我想將其刪除母雞生成表的內容:

<div xml:id="d1.c6" type="chapter"> 
    <head>Happiness and Virtue, how diminished by Asceticism in an indirect 
    way.—Useful and genuine obligations elbowed out by spurious ones<note 
    xml:id="d1.c6fn1" type="editor">In the text, the author has noted at this 
    point: 'These topics must have been handled elsewhere: perhaps gone through 
    with. Yet what follows may serve for an Introduction.'</note></head> 
    <p>Contents of chapter 6 go here</p> 
</div> 

我的XSL目前看起來是這樣的:

<xsl:template match="tei:head" mode="toc"> 
    <xsl:if test="../@type = 'chapter'"> 
     <h3><a href="#{../@xml:id}"><xsl:apply-templates/></a></h3> 
    </xsl:if> 
</xsl:template> 

我試過在toc模式注意添加一個新的空白模板匹配,但無濟於事。例如:

<xsl:template match="tei:note" mode="toc"/> 

我也試過tei:head/tei:note\\tei:head\tei:note

在我的模板,整個文件(/),我用下面顯示的內容與下表匹配:

<xsl:apply-templates select="//tei:head" mode="toc"/> 

我試過添加以下內容,但無濟於事:

<xsl:apply-templates select="//tei:head/tei:note[@type = 'editorial']" 
mode="toc"/> 

任何幫助,將不勝感激!

p.s.這是我在SE的第一篇文章,所以如果我錯過了重要的細節,請讓我知道,我會澄清。謝謝。

+1

你能告訴我們你的全XSLT(如果它不是太大)和一個樣本輸入和輸出XML? – JLRishe 2013-04-25 15:36:32

回答

0

您通常需要通過做特定的TOC處理時沿模式:

<xsl:template match="tei:head" mode="toc" /> 
<xsl:template match="tei:head[../@type = 'chapter']" mode="toc"> 
    <h3><a href="#{../@xml:id}"><xsl:apply-templates mode="toc" /></a></h3> 
</xsl:template> 

注意在xsl:apply-templatesmode屬性。如果你這樣做,那麼你的模板tei:note應該服從。

如果您使用的是標識模板,這可能意味着您需要一個用於toc模式。

或者,如果你真的不需要特定的方式處理,一旦你已經達到tei:head,你可以這樣做:

<xsl:template match="tei:head" mode="toc" /> 
<xsl:template match="tei:head[../@type = 'chapter']" mode="toc"> 
    <h3><a href="#{../@xml:id}"> 
    <xsl:apply-templates select="node()[not(self::tei:note)]" /> 
    </a></h3> 
</xsl:template> 
+0

很好,謝謝。一旦我將'mode =「toc」'添加到'xsl:apply-templates'中作爲你的例子描述後面的空''使元素靜音。 太棒了! – 2013-04-25 15:54:25