2011-06-10 34 views
2

XML:渲染不同的模板相同的XML元素,在同級別

<Root> 
    <Elements> 
    <Element>el1</Element> 
    <Element>el2</Element> 
    </Elements> 

    <Elements> 
    <Element>el1</Element> 
    <Element>el2</Element> 
    </Elements> 
</Root> 

試圖生成適用兩種不同的模板相同的元素。

主要模板:

<xsl:stylesheet version="1.0"> 
     <xsl:template match="/Root"> 
      At root level 
      <xsl:apply-templates select="Elements"> 

      <h1>Render something more</h1> 

      <xsl:apply-templates select="Elements" mode="1:Custom"> 
     </xsl:template> 


    <!-- This doesn't render though it is called above--> 
     <xsl:template match="Elements"> 
     render something here 
     </xsl:template> 

    <!-- This renders twice --> 
     <xsl:template match="Elements" mode="1:Custom"> 
     render something else here 
     </xsl:template> 
</xsl:stylesheet> 

如果我添加模式的第一個模板,既不要渲染。

也試過:

<xsl:apply-templates select="Elements" mode="1:Custom" /> 

與不同的模板以應用爲:

<xsl:apply-templates select="Elements" mode="Different" /> 

只有兩個(其具有指定在呈現模式的第一個)中的一個。 即

<xsl:template match="Elements"> 
</xsl:template> 

不會呈現

<xsl:template match="Elements" mode="Different" />渲染兩次。

我該如何解決這個問題?我研究的每個地方都建議優先考慮這種模式。因爲有那麼多程序員使用它,所以必須是簡單的東西?

+2

這不可能,沒有看到你的XSLT程序的其餘部分來回答。某處有一個非常簡單的錯誤,但是上下文不足以指出它。嘗試生成仍然失敗的最小可能的XSLT/XML對,並將其添加到您的問題中。 – Tomalak 2011-06-10 15:50:32

+0

你所描述的是lecit,應該工作。您應該向我們展示更多涉及的XSLT。 – 2011-06-10 15:56:30

+0

我現在編輯了我的問題。希望它更清楚一點? – 2011-06-10 16:23:39

回答

5
<?xml version="1.0"?> 

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

     <xsl:template match="/Root"> 
      At root level 
      <xsl:apply-templates select="Elements"/> 

      <h1>After first template</h1> 

      <xsl:apply-templates select="Elements" mode="Custom"/> 
     </xsl:template> 

     <xsl:template match="Elements"> 
     <p>First template</p> 
      <xsl:apply-templates select="Element"/> 
     </xsl:template> 

     <xsl:template match="Elements" mode="Custom"> 
     <p>Second template  </p> 
     </xsl:template> 
     </xsl:stylesheet> 
4
<xsl:template match="Elements" mode="1:Custom"> 

您正在使用syntactically illegal mode name這裏(必須是QName)和任何符合XSLT處理器必須發出一個錯誤。

解決方案:只要改變

mode="1:Custom" 

mode="Custom" 

因此,這種轉變是正確的

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output omit-xml-declaration="yes" indent="yes"/> 
    <xsl:strip-space elements="*"/> 

    <xsl:template match="/Root"> 
     At root level 
    <xsl:apply-templates select="Elements"/> 
    <h1>Render something more</h1> 

    <xsl:apply-templates select="Elements" mode="Custom"/> 
    </xsl:template> 

    <xsl:template match="Elements"> 
     render something here 

    </xsl:template> 

    <xsl:template match="Elements" mode="Custom"> 

    render something else here 
    </xsl:template> 

    <xsl:template match="text()"/> 
</xsl:stylesheet> 

WH恩應用所提供的XML文檔

<Root> 
    <Elements> 
     <Element>el1</Element> 
     <Element>el2</Element> 
    </Elements> 
    <Elements> 
     <Element>el1</Element> 
     <Element>el2</Element> 
    </Elements> 
</Root> 

想要的,正確的結果產生:

 At root level 

    render something here 


    render something here 

<h1>Render something more</h1> 

render something else here 


render something else here 
相關問題