0

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>
1
  • Please post the expected output. Commented Nov 26, 2015 at 20:46

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.