Reputation: 39
I'm struggling to create an excel with xslt that will link (join) the two nodes on the same level by common attribute value.
Here goes the input xml:
```
<Nodes>
<Item name="ABC" category_id="A"></Item>
<Item name="DEF" category_id="B"></Item>
<Category name="First category" cat_id="A"</Category>
<Category name="Second category" cat_id="B"</Category>
</Nodes>```
And part of an xsl:
<xsl:for-each select="//tc:Nodes/tc:Item">
<xsl:variable name="item_name" select="./@Name" />
<xsl:variable name="item_category_id" select="./@category_id" />
<xsl:for-each select="//tc:Nodes:tc:Category/@cat_id = $category_id">
<xsl:variable name="category_category_id" select="./@cat_id />
</xsl:for-each>
<xsl:call-template name="generateReportData">
<xsl:with-param name="item_name" select="$item_name"/>
<xsl:with-param name="item_category_id" select="$item_category_id" />
**<xsl:with-param name="category_category_id" select="$category_category_id"/>**
</xsl:call-template>
</xsl:for-each>
The problem is that I cannot access the variable $category_category_id as it says it is either not defined or out of scope.
The result should one row containing values like: ABC, A, First category (it will link the values from two nodes by common category ID). Please, help -i'm newbie to xsl - maybe there is another way to do so.
Regards, Luke
Upvotes: 0
Views: 528
Reputation: 117175
link (join) the two nodes on the same level by common attribute value.
This is best done using a key. For example:
XML
<Nodes>
<Item name="ABC" category_id="A"/>
<Item name="DEF" category_id="B"/>
<Category name="First category" cat_id="A"/>
<Category name="Second category" cat_id="B"/>
</Nodes>
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:key name="cat" match="Category" use="@cat_id" />
<xsl:template match="Nodes">
<Table>
<xsl:for-each select="Item">
<Row>
<Cell>
<xsl:value-of select="@name"/>
</Cell>
<Cell>
<xsl:value-of select="key('cat', @category_id)/@name"/>
</Cell>
</Row>
</xsl:for-each>
</Table>
</xsl:template>
</xsl:stylesheet>
Result
<?xml version="1.0" encoding="UTF-8"?>
<Table>
<Row>
<Cell>ABC</Cell>
<Cell>First category</Cell>
</Row>
<Row>
<Cell>DEF</Cell>
<Cell>Second category</Cell>
</Row>
</Table>
Upvotes: 1