Now comes the action. An XSLT processor is probably readily available to you on your computer in a browser such as Microsoft Internet Explorer (IE) Version 6 or later, Netscape Navigator (Netscape) Version 7.1 or later, or Mozilla Version 1.4 or later. All three of these browsers have client-side XSLT processing ability already built-in. A common way to apply an XSLT stylesheet like msg.xsl to the document msg.xml in a browser is by using a processing instruction. You can see a processing instruction in a slightly altered version of msg.xml called msg-pi.xml. Open the file msg-pi.xml from examples/ch01 with one of the browsers mentioned. The result tree (a result twig, really) is displayed. Figure 1-1 shows you what the result looks like in IE Version 6, with service pack 1 (SP1). I explain how msg-pi.xml works in the section "The XML Stylesheet Processing Instruction" which follows. Figure 1-1. Transforming msg-pi.xml with Internet ExplorerWhen the XSLT processor in the browser found the pattern identified by the template in msg.xsl, it wrote the string Found it! onto the browser's canvas or rendering space.
1.2.1 The XML Stylesheet Processing InstructionTo apply an XSLT stylesheet to an XML document with a browser, you must first add an XML stylesheet processing instruction to the document. This is the case with msg-pi.xml, which is why you can display it in an XSLT-aware browser. A processing instruction, or PI, allows you to include instructions for an application in an XML document. The document msg-pi.xml, which you displayed earlier in a browser, contains an XML stylesheet PI: <?xml-stylesheet href="msg.xsl" type="text/xsl"?> <msg/> The XML stylesheet PI should always come before the document element (msg in this case), and is part of what is called the prolog of an XML document. The purpose of this PI is similar to one of the purposes of the link tag in HTML, that is, to associate a stylesheet with the document. Usually, there is only one XML stylesheet PI in a document, but under certain circumstances, you can have more than one.
In the XML stylesheet PI, the term xml-stylesheet is the target of the PI. The target identifies the name, purpose, or intent of the PI. This assumes that the application understands what the PI target is. Home-grown PIs are usually application-specific, but the XML stylesheet PI is widely supported and understood. If you invent a new, unique PI target, you also have to write the code to process your PI. 1.2.1.1 Attributes and pseudoattributesIn XML, attributes may only appear in element start tags or empty element tags, as shown in this element start tag (from message.xml): <message priority="low"> This message element contains an attribute, with priority as the attribute name and low as the attribute value. The attribute name and value are separated by an equals sign (=). In well-formed XML, attribute values must always be surrounded by either single (') or double (") quotes. The quotes must not be mixed together. You can read more about attributes in Section 3.1 of the XML specification. The constructs that follow the target in the XML stylesheet PI, href and type, are not attributes but are pseudoattributes. PIs can contain any legal XML characters between the target and the closing ?>, not just text that looks like attributes. For example, the following PI is perfectly legal: <?do not go gentle into that good night?> The first word following <? is do. This is the target of the PI, and there must be no space between <? and the target. The target is followed by the data not go gentle into that good night. This may not be complete nonsense to a Dylan Thomas fan, but a PI will be nonsense to an application unless the PI contains a target and other data that the application understands and knows what to do with it. If an XML processor does not understand the content of a PI, the consequences are not dire. The processor will simply ignore the PI and move on. Pseudoattributes structure the data so processors may have an easier time interpreting it. The href pseudoattribute contains a value that is a URI reference. This URI specifies the relative location of the stylesheet msg.xsl. An XSLT processor knows where to find resources relative to its base URI. The base URI is usually the directory that holds the source document. The other pseudoattribute, type, identifies the content type of the stylesheet, text/xsl. The content type identifies the content of the stylesheet as XSL or XSLT text.
|