I have a table that is calculated using many variables. During the calculation, i keep record of these calculations in a xml file.
The thing is I need to format this xml in different layouts
- one starting from the invoices
- the other starting from the column names.
Fore each ‘invoice’ there is a ‘cost’ specified which can be a part from the invoice’s value and each ‘cost’ is being placed in a column from the table.
My XML is like this:
<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>
So my expected output would be something like
Column name
Costs from which invoice
Calculations
Or
Invoice
Spread over a column based on cost
Calculations
My XSL looks something like this:
<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[@id=@idInvoice]">
<xsl:param name="idInvoice"></xsl:param>
<p>Found something</p>
<h2><xsl:value-of select="@value" /></h2>
</xsl:template>
</xsl:stylesheet>
So what I want to do is call a template based on an attribute from a node, display the data and continue to the next node.
IS this possible?
You can use the
current()function to refer to the current context-node. But to do this, you need to select the nodes when calling the template or before:@idrefers to theid=attribute of the selected<invoice>.current()/@invoicerefers to theinvoice=attribute of the context<cost>.It is often easier to use
<xsl:apply-templates>instead of<xsl:for-each>+<xsl:call-template>, but you would have to rewrite the template to usematch=instead ofname=.If you need to use a tag-name multiple times, you can use modes: