2010-07-16 49 views
3

我想基本上用XSLT模板重新創建ASP.NET母版頁的功能。我可以將xslt模板的結果作爲參數傳遞給另一個模板嗎?

我有一個「母版頁」模板,其中包含存儲在.xslt文件中的大部分頁面html。我有另一個特定於單個頁面的.xslt文件,它需要用XML來表示頁面數據。我想從我的新模板中調用母版頁模板,並且仍然可以插入我自己的將應用的xml。如果我可以傳遞一個允許我以參數作爲名稱來調用模板的參數,那就可以做到這一點,但這似乎不被允許。

基本上我有這樣的:

<xsl:template name="MainMasterPage"> 
    <xsl:with-param name="Content1"/> 
    <html> 
    <!-- bunch of stuff here --> 
    <xsl:value-of select="$Content1"/> 
    </html> 
</xsl:template> 

而且這樣的:

<xsl:template match="/"> 
    <xsl:call-template name="MainMasterPage"> 
    <xsl:with-param name="Content1"> 
     <h1>Title</h1> 
     <p>More Content</p> 
     <xsl:call-template name="SomeOtherTemplate"/> 
    </xsl:with-param> 
    </xsl-call-template> 
</xsl:template> 

什麼情況是,嵌套的XML基本上剝離和所有插入的 「TitleMore內容」

+0

好問題(+ 1)。請參閱我的回答以解釋問題並尋求正確的解決方案。 – 2010-07-17 03:31:57

回答

5

提供的代碼的問題在這裏:

<xsl:value-of select="$Content1"/> 

這將輸出任一的$Content1頂部節點(如果它包含一個文件)或它的第一個元素或文本子的字符串值的所有文本節點的後代的級聯(如果它是一個XML片段)。

您需要使用的

<xsl:copy-of select='$pContent1'>

,而不是

<xsl:value-of select='$pContent1'>

這正確的副本$pContent1

下的所有子節點是一個修正後的變換

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

<xsl:template match="/"> 
    <xsl:call-template name="MainMasterPage"> 
    <xsl:with-param name="pContent1"> 
     <h1>Title</h1> 
     <p>More Content</p> 
     <xsl:call-template name="SomeOtherTemplate"/> 
    </xsl:with-param> 
    </xsl:call-template> 
</xsl:template> 

<xsl:template name="MainMasterPage"> 
    <xsl:param name="pContent1"/> 
    <html> 
    <!-- bunch of stuff here --> 
    <xsl:copy-of select="$pContent1"/> 
    </html> 
</xsl:template> 

<xsl:template name="SomeOtherTemplate"> 
    <h2>Hello, World!</h2> 
</xsl:template> 
</xsl:stylesheet> 

當這種轉變是在任何XML文檔(未使用),想要的,正確的應用結果產生

<html> 
    <h1>Title</h1> 
    <p>More Content</p> 
    <h2>Hello, World!</h2> 
</html> 
+0

+1,很好的答案。是否可以使用'apply-templates'(使用相同的'select')代替'copy-of',將模板應用於中間結果?我一直試圖讓這個工作一段時間,並沒有抱怨,但結果只是空的。 – falstro 2011-02-03 09:55:08

+0

@roe:是的,但只有當要應用的模板等同於標識轉換時,apply-templates才相當於複製。 – 2011-02-03 13:40:09

+0

我明白了,我的問題是我試圖執行轉換,然後在該結果上進行另一個轉換(一個轉換修改文本內容,插入零寬度空格字符,第二個轉換完成標記例如fo-inline塊),但由於某種原因,結果是空的。 – falstro 2011-02-03 13:42:13

相關問題