2012-02-26 32 views
0

我有一個使用多個變量計算的表。在計算過程中,我將這些計算記錄在xml文件中。使用參數加載節點兄弟的xsl模板

的事情是我需要在不同的佈局

  • 一個從發票
  • 另外從列名起開始格式化這個XML。

在每個「發票」前面都有一個指定的「成本」,它可以是發票價值的一部分,每個「成本」都放在表中的一列中。

我的XML是這樣的:

<table> 
    <invoices> 
     <invoice id="1" value="230" supplier="First supplier"/> 
    </invoices> 
    <costs> 
     <cost id="1" invoice="1" column="2" value="100"> 
      <calculation> 
       <tenant name="Tenant1" cost="30" /> 
       <tenant name="Tenant2" cost="70" /> 
      </calculation> 
     </cost> 
     <cost id="2" invoice="1" column="1" value="130"> 
      <calculation> 
       <tenant name="Tenant1" cost="50" /> 
       <tenant name="Tenant2" cost="50" /> 
      </calculation> 
      <calculation> 
       <tenant name="Tenant1" cost="10" /> 
       <tenant name="Tenant2" cost="20" /> 
      </calculation> 
     </cost> 
    </costs> 
    <columns> 
     <column id="1" name="Column name 1"/> 
     <column id="2" name="Column name 2"/> 
    </columns> 
</table> 

所以我的預期輸出會是這樣的

Column name 
    Costs from which invoice 
     Calculations 

或者

Invoice 
    Spread over a column based on cost 
     Calculations 

我的XSL看起來是這樣的:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/"> 
      <xsl:for-each select="table/costs/cost"> 
       <xsl:call-template name="cost"></xsl:call-template> 
       <h1>Mere</h1> 
      </xsl:for-each> 
     </html> 
    </xsl:template> 
    <xsl:template match="cost" name="cost"> 
     <xsl:apply-templates select="invoice"></xsl:apply-templates> 
     <h1>Cheltuiala <xsl:value-of select="@id"></xsl:value-of></h1> 
     <ul> 
      <li> 
      <xsl:call-template name="invoice" > 
       <xsl:with-param name="idInvoice" select="@invoice"></xsl:with-param> 
      </xsl:call-template> 
      </li> 
      <li>Si ceva avcolo</li> 
     </ul> 
    </xsl:template> 
    <xsl:template name="invoice" match="table/invioces/invoice[@[email protected]]"> 
     <xsl:param name="idInvoice"></xsl:param> 
     <p>Found something</p> 
     <h2><xsl:value-of select="@value" /></h2> 
    </xsl:template> 
</xsl:stylesheet> 

所以我想要做的是根據節點的屬性調用模板,顯示數據並繼續到下一個節點。

這可能嗎?

+0

你忘了提供確切想從轉換輸出。請編輯問題並提供這些必要的信息。 – 2012-02-26 16:23:26

回答

1

您可以使用current()函數來引用當前上下文節點。但要做到這一點,你需要調用模板或之前時選擇的節點:

<xsl:template name="cost"> 
    ... 
    <xsl:for-each match="/table/invoices/invoice[@id=current()/@invoice]"> 
     <xsl:call-template name="invoice"/> 
    </xsl:for-each> 
    ... 
</xsl:template> 
  • @id指選擇<invoice>id=屬性。
  • current()/@invoice指上下文<cost>invoice=屬性。

它往往是更容易使用<xsl:apply-templates>而不是<xsl:for-each> + <xsl:call-template>,但你必須重寫使用match=代替name=的模板。

<xsl:template match="cost"> 
    ... 
    <xsl:apply-templates match="/table/invoices/invoice[@id=current()/@invoice]"/> 
    ... 
</xsl:template> 

<xsl:template match="invoice"> 
    ... 
</xsl:template> 

如果你需要使用一個標籤名稱多次,您可以使用模式:

<xsl:template match="cost"> 
    ... 
    <xsl:apply-templates match="/table/invoices/invoice[@id=current()/@invoice]" 
         mode="cost-invoice"/> 
    ... 
</xsl:template> 

<xsl:template mode="cost-invoice" match="invoice"> 
    ... 
</xsl:template>