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.
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.