What Makes Up XML?

I l @ ve RuBoard

XML is made up of several different things. In its most basic form, it can just be an XML document. However, it can also include a DTD document and an XSL document.

Document Type Definition (DTD) Document

When you create your own language using XML, you need to make sure that all the XML documents you create using your XML language follow the rules of your language; thus, you create a Document Type Definition (DTD) document.

To create an XML document, you don't need a DTD document, but if you want to enforce that all your XML documents follow the same language schema, you need a DTD document. So what does a DTD document look like? Let's look at a sample DTD for our sample XML document:

 <?xml version="1.0" encoding="UTF-8"?>  <!ELEMENT NAME (#PCDATA)>  <!ELEMENT PEOPLE (PERSON+)>  <!ELEMENT PERSON (NAME)> 

The language for creating DTD documents is set by the XML standard. Thus, you use the same language to create all DTD documents. The third line of the example defines the PEOPLE node as containing multiple instances of the PERSON node. In line four, the PERSON node is defined as containing the NAME node. The NAME node is defined in the first line as containing data (such as Andrew). This sounds like the XML example. In fact, with this DTD document, your XML documents must have this structure, or they will be invalid (and validating XML parsers will report them as such). Save your DTD to a file called people.dtd, ready for use in your XML file.

Now that you have a DTD document, how do you make use of it in your XML file? You embed it as follows :

 <?xml version="1.0" encoding="UTF-8"?>  <!DOCTYPE PEOPLE SYSTEM  "http://localhost/phpbook/Chapter8_XML/XML/people.dtd">  <PEOPLE>       <PERSON>            <NAME>Andrew</NAME>       </PERSON>       <PERSON>            <NAME>Emma</NAME>       </PERSON>       <PERSON>            <NAME>Terry</NAME>       </PERSON>       <PERSON>            <NAME>Mary</NAME>       </PERSON>       <PERSON>            <NAME>Thomas</NAME>       </PERSON>  </PEOPLE> 

Specifically, you use the DOCTYPE , a special XML node for embedding the DTD document:

 <!DOCTYPE PEOPLE SYSTEM  "http://localhost/phpbook/Chapter8_XML/XML/people.dtd"> 

Here I have used an HTTP URL, but you can also use a file path .

Extensible Style Sheet Language (XSL) Document

As you saw in Figure 8.1, XML displayed in its raw form is not easy to read. Thankfully, the W3C developed a standard to allow us to display XML files in a much easier-to-read manner. They called it Extensible Style Sheet Language (XSL).

If you think XSL sounds like the HTML Cascading Style Sheet (CSS) language, you are correct. XSL and CSS serve a similar purpose ”to apply styles and formatting. CSS and XSL also serve different purposes, but they complement each other. For example, XSL can transform documents, but CSS can only style them. XSL can produce HTML/CSS from XML, but CSS can't.

The XSL language, like the DTD language, is fixed. The XSL language is an XML language, and it's a standard, so all XSL documents you create must follow that standard. What does an XSL document look like? Let's look at a sample XSL document for our sample XML document:

 <?xml version="1.0" encoding="UTF-8"?>  <xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">       <xsl:template match="/">            <html>                 <head/>                 <body>                      <xsl:apply-templates/>                 </body>            </html>       </xsl:template>       <xsl:template match="NAME">            <span style="display:list-item; font-family:Arial">                 <span style="display:list-item; font-family:Arial">                      <xsl:apply-templates/>                 </span>            </span>       </xsl:template>  </xsl:stylesheet> 

First, you must include the required XSL header:

 <xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

This is so that the parser knows which version of XSL you are using. In the XSL document, you will format your XML document into an HTML document. To ensure that your XML document is displayed correctly within HTML, you place your XML content within the <BODY></BODY> HTML tags:

 <xsl:template match="/">       <html>            <head/>            <body>                 <xsl:apply-templates/>            </body>       </html>  </xsl:template> 

The only node within your XML document that holds character data is the NAME node. To ensure that this is what is displayed within the HTML, you target that node:

 <xsl:template match="NAME"> 

You then specify all formatting to apply to the NAME node:

 <span style="display:list-item; font-family:Arial">  <xsl:apply-templates/>  </span> 

In this case, you list the items and display in the Arial font. You save your XSL file to a file called people.xstl. You can then embed it as follows:

 <?xml version="1.0" encoding="UTF-8"?>  <!DOCTYPE PEOPLE SYSTEM  "http://localhost/phpbook/Chapter8_XML/XML/people.dtd">  <?xml-stylesheet type="text/xsl"  href="http://localhost/phpbook/Chapter8_XML/XML/people.xslt"?>  <PEOPLE>       <PERSON>            <NAME>Andrew</NAME>       </PERSON>       <PERSON>            <NAME>Emma</NAME>       </PERSON>       <PERSON>            <NAME>Terry</NAME>       </PERSON>       <PERSON>            <NAME>Mary</NAME>       </PERSON>       <PERSON>            <NAME>Thomas</NAME>       </PERSON>  </PEOPLE> 

Specifically, you use the xml-stylesheet tag, a special XML node for embedding the XSL document:

 <?xml-stylesheet type="text/xsl"  href="http://localhost/phpbook/Chapter8_XML/XML/people.xslt"?> 

If you now display the XML document, you will see it in a formatted form, as shown in Figure 8.2.

Figure 8.2. The XML file formatted with XSL.

graphics/08fig02.gif

I l @ ve RuBoard


PHP Programming for Windows
PHP Programming for Windows (Landmark (New Riders))
ISBN: 0735711690
EAN: 2147483647
Year: 2002
Pages: 99

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net