XSLT allows you to create formatting structures which interpret and modify the existing XML elements. Learn about the syntax of XSLT elements, how the namespace attribute differs depending on the browser in use, and how to transform original XML elements.
This template looks for elements that match <headline> and then copies their content, makes them bold and adds a line-break. We then need another for-each template to match any <story> patterns:
This time, we don't want the contents made bold, but we can still include a line-break. Now we just close the template and the root element:
</xsl:template> </xsl:stylesheet>
That's it, as far as the stylesheet goes. Save the file as style.xls in the same folder as your XML file. For reference, the whole stylsheet should look like this:
Before this will actually work, you need to link the XLST file to the existing XML file, to do this, open the original XML file (news.xml if you're following on from the previous article) and add the following line of code directly below the XML declaration at the top:
Now save this file with the name transformedNews.xml and then open it in a browser. You should see a page with the Today's News heading at the top of the page, and the two news stories with their associated headings below this. If you open the XSL file, it will look very similar to a flat XML file, with the declaration and tags in blue and the elements in red. Additionally, XSLT elements will appear in a nice shade of purple.