views:

56

answers:

1

I would like to better understand the basic steps needed to a take an XML document like this Breakfast Menu...

<?xml version="1.0" encoding="ISO-8859-1"?>
<breakfast_menu>
    <food>
        <name>Belgian Waffles</name>
        <price>$5.95</price>
        <description>two of our famous Belgian Waffles with plenty of real maple syrup</description>
        <calories>650</calories>
    </food>
    <food>
        <name>Strawberry Belgian Waffles</name>
        <price>$7.95</price>
        <description>light Belgian waffles covered with strawberries and whipped cream</description>
        <calories>900</calories>
    </food>
    <food>
        <name>Berry-Berry Belgian Waffles</name>
        <price>$8.95</price>
        <description>light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>
        <calories>900</calories>
    </food>
    <food>
        <name>French Toast</name>
        <price>$4.50</price>
        <description>thick slices made from our homemade sourdough bread</description>
        <calories>600</calories>
    </food>
    <food>
        <name>Homestyle Breakfast</name>
        <price>$6.95</price>
        <description>two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
        <calories>950</calories>
    </food>
</breakfast_menu>

And "export" it to say, an Access or MySQL database using XSLT, creating two joined tables:

  • Table: breakfast_menu
    • Field: menu_item_id
    • Field: food_id
  • Table: food
    • Field: food_id
    • Field: name
    • Field: price
    • Field: description
    • Field: calories

If there are online tutorials on this that you know of, I'd be interesting in learning more, as well.

Thanks.

+2  A: 

XSLT transforms a document tree to another document tree. As I know there are no direct connectors to MySQL that could insert transformed data into MYSQL.

Another option might be to generate an XML that could be imported by MySQL.

mysqldump utility accepts --xml option that allows to dump data in XML format (so you can transform your data to the format). Unfortunatelly, loading the data from the file back is a trick that is available only from MySQL 5.1. In MySQL 6.0.3 though there appears a new LOAD XML statement that can be used for the purpose.

I believe that the task can easily be solved by reading the XML with an XML-reader and inserting the data row by row using standard database connectors in any modern programming language.

UPD:

An xsl might look as following:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <xsl:output method="text"/>

    <xsl:template match="breakfast_menu">
        <xsl:apply-templates mode="insert-to-food" select="food" />
        <xsl:apply-templates mode="insert-to-breakfast_menu" select="food" />
    </xsl:template>

    <xsl:template mode="insert-to-food" match="food">
        <xsl:text>INSERT INTO food(</xsl:text>
        <xsl:number />
        <xsl:text>, '</xsl:text>
        <xsl:value-of select="name" />
        <xsl:text>', '</xsl:text>
        <xsl:value-of select="price" />
        <xsl:text>', '</xsl:text>
        <xsl:value-of select="description" />
        <xsl:text>', </xsl:text>
        <xsl:value-of select="calories" />
        <xsl:text>);&#xA;</xsl:text>
    </xsl:template>

    <xsl:template mode="insert-to-breakfast_menu" match="food">
        <xsl:text>INSERT INTO breakfast_menu(1, </xsl:text>
        <xsl:number />
        <xsl:text>);&#xA;</xsl:text>
    </xsl:template>
</xsl:stylesheet>
newtover
Any more-specific recommendations on actually transforming the data *from* XML *into* SQL?
Yaaqov
Very clear- thanks for the guidance.
Yaaqov
Side note: The XSL will obviously break if any of the XML text fields contains a single quote. I'd as well recommend doing all of this in a regular programming language with some concept of both XML and databases.
Tomalak