Wednesday, October 10, 2018

Transforming XML

Suppose you have an XML file with your favorite data, such as your CD collection
<catalog>
  <cd>
    <title>Greatest Hits</title>
    <artist>John Doe</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
  </cd>
  <cd>
    <title>Favorite Songs</title>
    <artist>Mary Doe</artist>
    <country>UK</country>
    <company>CBS Records</company>
    <price>9.90</price>
    <year>1988</year>
  </cd>

</catalog>
How do you change it to the following in HTML?

My CD Collection

Title Artist
Greatest Hits John Doe
Favorite Songs Mary Doe

Sure you can write a little program,in java, for example, to use one of the many fairly-difficult-to-use XML library to turn it into an object then rewrite it in html. But there is an perhaps easier way. You can just transform the XML into something else, like HTML. This is actually a very old problem, and it is called XSLT... That actually is tricky to get right, and hard to come up with entirely by hand. Gotta give credit for those who put good tutorials. https://www.w3schools.com/xml/xsl_intro.asp.

No comments: