Search code examples
xmlxsltbrowserxquerydtd

What do I need to manage XML files?


I believe I need a DTD to define the schema and an XSLT if I want to display it in a browser and have it look "pretty". But I'm not sure what else I would need to have a well-defined XML document that can be queried using XQuery and displayed in a web browser.


Solution

  • For a XML document to be queryable using XQquery you do not have to define a DTD or XSD. The purpose of DTD or XSD is to define the strict structure of a XML document and to allow validation before usage.

    Modern browsers interpret XML files very nicely and show a DOM tree. If enhanced formatting of XML for browser display is necessary you have to create a XSLT transformation file and then add a directive to the original XML document pointing to the XSLT file. The browser picks that directive and uses the built-in XSLT processor to obtain the output that is then interpreted by the browser.

    info.xml

    <?xml version="1.0" encoding="iso-8859-1"?>
    <?xml-stylesheet type="text/xsl" href="info.xslt"?>
    <info>
        <appName>My App</appName>
        <version>1.0.129</version>
        <buildTime>10-09-2008 12:44:03</buildTime>
    </info>
    

    info.xslt

    <?xml version="1.0" encoding="iso-8859-1"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:template match="/">
            <html>
                <head>
                    <title>Application</title>
                    <style type="text/css">
                        body { font-family: Lucida Console; }
                        #outer { text-align: left; }
                        #name {
                            font-weight: bold;
                            font-size: 1.2em;
                        }
                        #logo {
                            float: left;
                            padding-right: 20px;
                            padding-bottom: 200px;
                        }
                    </style>
                </head>
                <body>
                    <xsl:apply-templates select="info" />
                </body>
            </html>
        </xsl:template>
    
        <xsl:template match="info">
            <img id="logo" src="image.png" />
            <div id="outer">
                <div id="name">
                    <xsl:value-of select="appName"/>
                </div>
                <div id="version">
                    <xsl:value-of select="version"/>
                </div>
                <div id="date">
                    <xsl:value-of select="buildTime"/>
                </div>
            </div>
        </xsl:template>
    </xsl:stylesheet>