Reputation: 17
I want to display the parts that are red and where supplier id is 1. how does one achieve this in xslt?
i have tried using this using xpath but i can only get values like if sid=1 and if color = red but not both!
<table>
<suppliers>
<supply>
<sid>1</sid>
<sname> vishal</sname>
<address> wtf lane</address>
</supply>
<supply>
<sid>2</sid>
<sname> vishal</sname>
<address> wtf lane</address>
</supply>
</suppliers>
<parts>
<part>
<pid>1</pid>
<pname>kacha</pname>
<color>red</color>
</part>
</parts>
<catalogs>
<catalog>
<sid>1</sid>
<pid>1</pid>
<cost> 5 rs</cost>
</catalog>
</catalogs>
</table>
Upvotes: 0
Views: 533
Reputation: 116982
I want to display the parts that are red and where supplier id is 1.
Your question is not entirely clear. The following stylesheet selects catalog
entries where the sid
is 1
and the corresponding part
's color
is "red".
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="part" match="part" use="pid" />
<xsl:key name="supplier" match="supply" use="sid" />
<xsl:template match="/table">
<items>
<xsl:for-each select="catalogs/catalog[sid=1 and key('part', pid)/color='red']">
<item>
<xsl:copy-of select="key('part', pid)/pname"/>
<xsl:copy-of select="key('supplier', sid)/sname"/>
<xsl:copy-of select="cost"/>
</item>
</xsl:for-each>
</items>
</xsl:template>
</xsl:stylesheet>
Test input
<table>
<suppliers>
<supply>
<sid>1</sid>
<sname>supplier A</sname>
<address>address A</address>
</supply>
<supply>
<sid>2</sid>
<sname>supplier B</sname>
<address>address B</address>
</supply>
</suppliers>
<parts>
<part>
<pid>1</pid>
<pname>part A</pname>
<color>red</color>
</part>
<part>
<pid>2</pid>
<pname>part B</pname>
<color>blue</color>
</part>
</parts>
<catalogs>
<catalog>
<sid>1</sid>
<pid>1</pid>
<cost>101</cost>
</catalog>
<catalog>
<sid>1</sid>
<pid>2</pid>
<cost>102</cost>
</catalog>
<catalog>
<sid>2</sid>
<pid>1</pid>
<cost>103</cost>
</catalog>
<catalog>
<sid>2</sid>
<pid>2</pid>
<cost>104</cost>
</catalog>
</catalogs>
</table>
Result
<?xml version="1.0" encoding="UTF-8"?>
<items>
<item>
<pname>part A</pname>
<sname>supplier A</sname>
<cost>101</cost>
</item>
</items>
Upvotes: 1