0

Hello! I'm having trouble with my college assignment. The assignment wants me to display an XML file using XSLT and add some filtering function.

I have no problem displaying the XML with XSLT in my web page using javascript. I used the tutorial from w3school (http://www.w3schools.com/xsl/xsl_client.asp).

However i have no idea how to filter my results. For example I have this following XML:

<fruitsList>
    <fruit>
        <name>Apple</name>
        <color>Red</color>
    </fruit>
    <fruit>
        <name>Banana</name>
        <color>Yellow</color>
    </fruit>
    <fruit>
        <name>Tomato</name>
        <color>Red</color>
    </fruit>
    <fruit>
        <name>Grape</name>
        <color>Purple</color>
    </fruit>
</fruitsList>

I only want the page to display fruits that has red color. How do I do this?

In the tutorial, they use this line of code to transform the XML and XSLT resultDocument = xsltProcessor.transformToFragment(xml, document); and then append it to the page using document.getElementById("example").appendChild(resultDocument); so I thought I could use and process the resultDocument variable to filter it but I have no idea how. The resultDocument appears to be a DocumentFragment node. I thought I could just simply use resultDocument.getElementsByTagName("fruit") and then loop each result to find element with red color then only display that. However I couldn't.

Can anyone help me with this? I'm new to javascript so kind of really confused :(. Oh, and please do forgive me if my english is confusing, i'm not a native speaker.

Thank you.

2
Contributors
3
Replies
9
Views
2 Years
Discussion Span
Last Post by pritaeas
0

Did you change the XSLT? It should do the filtering of the XML, and afterwards the entire output is added to the example div. See this link for example on how to use if.

Edited by pritaeas

0

Forgot to mention, i want to apply the filtering from the webpage dynamically. So that i can change whether i want to display red fruits or other color from the webpage. hope it makes sense though.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.