Xml/Xslt - Filtering the XML Nodes bound with dropdownlist

Asked By sachtech boy on 07-May-12 05:37 PM
Earn up to 20 extra points for answering this tough question.
Hello Friends, I have two DropDownList: 1) CollegeDDL which contains Four static names of the colleges. : Homeopathic Medical College,Physiotherapy College,Institute of Ayurveda,Institute of Business Administration




2) CourseDDL which is bound to CourseDetail.XML.
<?xml version="1.0" encoding="utf-8" ?>
<Institutes>  
 <Institute>
           <InstituteName>  Homoeopathic Medical College  </InstituteName>
  <Course> <CourseName>  BHMS                    </CourseName> </Course>
  <Course> <CourseName>  MD(Repertary)           </CourseName> </Course>
  <Course> <CourseName>  MD(Organon of Medicine) </CourseName> </Course>
  <Course> <CourseName>  MD(Materia Medica)      </CourseName> </Course>
</Institute>


<Institute>
<InstituteName>  Physiotherapy College  </InstituteName>
<Course> <CourseName>  BPT          </CourseName> </Course>
                <Course> <CourseName>  BPT(Sports)  </CourseName> </Course>
                <Course> <CourseName>  BPT(Cardio)  </CourseName> </Course>
</Institute>


<Institute>
<InstituteName>  Institute of Ayurved  </InstituteName>
<Course> <CourseName>  BAMS          </CourseName> </Course>
       <Course> <CourseName>  BAMS(Nature)  </CourseName> </Course>
                <Course> <CourseName>  BAMS(Plants)  </CourseName> </Course>
</Institute>


<Institute>
<InstituteName> Institute of Business Administration </InstituteName>
<Course> <CourseName>BBA</CourseName> </Course>
                <Course> <CourseName>DBM</CourseName> </Course>
                <Course> <CourseName>BBA(Distance)</CourseName> </Course>
</Institute>


---------------------------------
  CourseDetail.XSL
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
  <xsl:output method="xml" indent="yes"/>


  <xsl:template match ="/">


    <Institute>
      <xsl:apply-templates select ="Institutes/Institute/Course"/>
    </Institute>


  </xsl:template>




  <xsl:template match ="Institutes/Institute/Course">
    <Institute>
      <xsl:attribute name="CourseName">
        <xsl:value-of select="CourseName"/>
      </xsl:attribute>
    </Institute>


  </xsl:template>


</xsl:stylesheet>
------------------------
I have bind Coursedetail.XML to CourseDDL and CourseDetail.XSL (No XPATH Expression)


After doing this, My CourseDDL is correctly filled with all the CourseName value of my XML file.
But now I want to filter the data bound to CourseDDL , based on College Name selected in CollegeDDL.


For Example:Currently my CourseDDL contains all the CourseName : BHMS,MD(Repertary),MD(Organon of Medicine),......,DBM,BBA(Distance).


But when I select "Homoeopathic Medical College" in CollegeDDL , The data bound to CourseDDL should filter accordingly.
It means when I select "Homoeopathic Medical College" , my CourseDDL should Display only that course: BHMS,MD(Repertary),MD(Organon of Medicine),MD(Materia Medica)


------------------------------------
I have tried the following , but could not achieve it.


1) XmlDataSourceCourse.XPath = "/Institutes/Institute[InstituteName='" + CollegeDDL.SelectedItem.Value + "']"


2) XmlDataSourceCourse.XPath = "/Institutes/Institute[InstituteName='" + CollegeDDL.SelectedItem.Value + "']/Course"


3)XmlDataSourceCourse.XPath = "/Institutes/Institute[InstituteName='" + CollegeDDL.SelectedItem.Value + "']/Course/CourseName"




Anybody please help to format correct XPATH Expression to achive this.
Somesh Yadav replied to sachtech boy on 08-May-12 01:58 AM
hello,

in the below link some sample examples are explained similar to your requirement , so once you go through the below one,

http://forums.asp.net/t/999571.aspx/1

Hope it might help you.
Anil Kumar replied to sachtech boy on 08-May-12 02:28 AM
You can refer to the discussion over here to get into a bit deeper into the issue....just try grilling with the XSL:
http://stackoverflow.com/questions/6046002/how-do-i-pass-a-xslt-parameter-to-a-xmldatasource-correctly 

Update again.
Thank you
Anil
Chintan Vaghela replied to sachtech boy on 08-May-12 03:50 AM

Hello,

 

You need to pass filter parameter to XSLT (for filter only selected Institute Course)

 

<xsl:param name="varInstituteName"></xsl:param>

 

 

After then assign this xslt param value using C# XslArgumentList

 

XsltArgumentList argsList = new XsltArgumentList();

argsList.AddParam("varInstituteName", "", strInstituteName);

 

Now this Xsl Argument list pass to XML datasource

 

xmlDS.TransformFile = Server.MapPath("XMLFileCollage.xslt");

xmlDS.TransformArgumentList = argsList;

 

In xsl file filter parameter

 

<xsl:if test="InstituteName[node()=$varInstituteName]">

 

 

your xslt changes as following way

 

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">

  <xsl:output method="xml" indent="yes"/>

  <xsl:param name="varInstituteName"></xsl:param>

  <xsl:template match ="/">

    <Institutes>

    <xsl:for-each select="Institutes/Institute">

      <xsl:if test="InstituteName[node()=$varInstituteName]">

      <xsl:for-each select="Course">

        <Institute>

        <xsl:attribute name="CourseName">

          <xsl:value-of select="CourseName/node()"/>

        </xsl:attribute>

        </Institute>

      </xsl:for-each>

      </xsl:if>

    </xsl:for-each>

    </Institutes>

  </xsl:template>

</xsl:stylesheet>

 

 

Bind dropdown logic change as following way

 

string InstitueName = CollegeDDL.SelectedItem.Value;// "Institute of Business Administration";

      XsltArgumentList argsList = new XsltArgumentList();

      argsList.AddParam("varInstituteName", "", strInstituteName);

      XmlDataSource xmlDS = new XmlDataSource();

      xmlDS.EnableCaching = false;

      xmlDS.DataFile = Server.MapPath("XMLFileCollage.xml");

      xmlDS.TransformFile = Server.MapPath("XMLFileCollage.xslt");

      xmlDS.TransformArgumentList = argsList;

      xmlDS.XPath = "/Institutes/Institute";

      DropDownList1.DataSource = xmlDS;

      DropDownList1.DataTextField = "CourseName";

      DropDownList1.DataValueField = "CourseName";

      DropDownList1.DataBind();

 

 

Check it and let me know your feedback

 

 

 

 

 

Hope this helpful!

Thanks

 

 

 

sachtech boy replied to Somesh Yadav on 08-May-12 07:53 AM
Thanks Somesh.....but I have already went through that page but could not find my solution.........
anyway thanks for early reply :-)
sachtech boy replied to Anil Kumar on 08-May-12 07:55 AM
Thank You Anil Kumar....... I have read that page and find it really helpful .........
and thank you for replying early .... 
Anil Kumar replied to sachtech boy on 08-May-12 07:59 AM
Sachtech boy,
Glad to help you out.

Thank you
Anil
sachtech boy replied to Chintan Vaghela on 08-May-12 08:01 AM
Oh R B.......thanks a lot for such a deep explanation  and working solution......
Yes It works as I wished .......
I have never thought passing Parameter to XSLT and was stuck  in XPATH .....
This will definatly help me lot in my future projects also........

Once again my Heartily thanks to You R B..........!!!
Enjoy....Take Care......
Chintan Vaghela replied to sachtech boy on 08-May-12 08:04 AM
you most welcome :)