Intro to XML: Formatting (output)

Exercises

You can find all the files for this in-class exercise set here.

Starting with Transformations

Transformations are rule sets that create a new result tree from a source tree based on templates and filters. As a declarative language, XSLT is both powerful and effective in changing XML into other forms.

Part 1: Searching and Sorting

Use the customers.xml file and build the necessary XML stylesheets to:

  1. Create a mailing list of all customers and addresses
  2. Retrieve only the customer mailing list from a country of your choice

Part 2: Generating Attributes and Elements

Both elements and attributes can be added to the result document using XSLT. Using the file called coffee.xml, write an xsl stylesheet so that the resulting output looks like a very straightforward list of information.

Now tweak the stylesheet so that you can add an element you’ve forgotten (i.e. add a child element called InStock to coffee). You can add elements by using literal text but the <xsl:element> instruction is just as effective. Try both and compare them.

Attributes are generated in the same way as elements … use <xsl:attribute> instead of <xsl:element>. Add the attribute “currently” to the InStock element to reflect whether something is available now.

Part 3: Converting Elements to Attributes

Create a stylesheet that converts the price element into an attribute of coffee (rather than a child). And, let’s also convert the taste and bestwith elements into attributes, as well.

<xsl:template match=”coffee”>
    <coffee name=”{@name}” origin={@origin}” price=”{price}”>
    <availability<xsl:value-of select=”availability” /></availability>
    <descriptions taste=”{taste}” bestwith=”{bestwith}” />
    </coffee>
</xsl:template>

Try this style sheet against your coffee.xml file and look at the results … did you successfully convert the elements to attributes? Try checking your file in Oxygen and compare the results.

Remember that it is also possible to delete elements and attributes using an empty template (a match pattern is defined but there is no matching template).


revalidate the HTML revalidate the CSS Pastafarian Flag

Last modified: 2 Sep 2007 12:28:17 PM