The ?xsl:stylesheet? Processing Instruction

The <?xsl:stylesheet?> Processing Instruction

When you have an XSL stylesheet you want to apply to an XML document, you need some way of connecting that stylesheet to the document, and thats often done with the <?xsl:stylesheet?> processing instruction. This instruction has several possible attributes:

  • href (mandatory). The URI of the stylesheet. May be a full URI, or may be of the fragment form #new_style . Set to a URI.

  • type (mandatory). The MIME type of the stylesheet. Typically text/xml or application/xml. For the Internet Explorer, use text/xsl. Set to a valid MIME type.

  • title (optional). Use a title to distinguish between several <?xsl:stylesheet?> elements. Some XML parsers enable you to specify which one to use. Set to a string value.

  • media (optional). Description of the output medium, such as print or aural. Set to one of the values listed in the W3C HTML 4.0 specification.

  • charset (optional). Sets the character encoding. Note that XSLT stylesheets set their own encoding, because they are XML documents, so this attribute has no real use. Set to a character encoding such as UTF-8.

  • alternate (optional). Set to yes to indicate this is an alternate stylesheet, or no to indicate it is the preferred stylesheet.

The <?xsl:stylesheet?> processing instruction is added in the XML document, not the XSL stylesheet, and shows XSLT processors which stylesheet to use with this document.

In practice, <?xsl:stylesheet?> is used mostly with browsers, because with standalone processors, you usually give the name of the stylesheet document directly, as when you use the Oracle XSLT processor this way:

 C:\planets>java oracle.xml.parser.v2.oraxsl planets.xml planets.xsl planets.html 

In fact, it may surprise you to learn that <?xsl:stylesheet?> is not part of the XSLT recommendation. This processing instruction has its own recommendation just for itself, which you can find at www.w3c.org/TR/xml-stylesheet. Among other things, this means that XSLT processors are not obliged to support this processing instruction, and most standalone processors do not.

Heres an example. In Chapter 1, you saw planets.xml, a well- formed XML document that holds data about three planets: Mercury, Venus, and the Earth. You can use the <?xml-stylesheet?> processing instruction in planets.xml to indicate what XSLT stylesheet to use, where you set the type attribute to text/xml (W3C also allows application/xml, and the Internet Explorer requires text/xsl) and the href attribute to the URI of the XSLT stylesheet, such as planets.xsl:

Listing 2.1 planets.xml
 <?xml version="1.0"?>  <?xml-stylesheet type="text/xml" href="planets.xsl"?>  <PLANETS>      <PLANET>          <NAME>Mercury</NAME>          <MASS UNITS="(Earth = 1)">.0553</MASS>          <DAY UNITS="days">58.65</DAY>          <RADIUS UNITS="miles">1516</RADIUS>          <DENSITY UNITS="(Earth = 1)">.983</DENSITY>          <DISTANCE UNITS="million miles">43.4</DISTANCE><!--At perihelion-->      </PLANET>      <PLANET>          <NAME>Venus</NAME>          <MASS UNITS="(Earth = 1)">.815</MASS>          <DAY UNITS="days">116.75</DAY>          <RADIUS UNITS="miles">3716</RADIUS>          <DENSITY UNITS="(Earth = 1)">.943</DENSITY>          <DISTANCE UNITS="million miles">66.8</DISTANCE><!--At perihelion-->      </PLANET>      <PLANET>          <NAME>Earth</NAME>          <MASS UNITS="(Earth = 1)">1</MASS>          <DAY UNITS="days">1</DAY>          <RADIUS UNITS="miles">2107</RADIUS>          <DENSITY UNITS="(Earth = 1)">1</DENSITY>          <DISTANCE UNITS="million miles">128.4</DISTANCE><!--At perihelion-->      </PLANET>  </PLANETS> 

Thats how to use the <?xml-stylesheet?> element; now its time to start working on writing the stylesheet itself. Ill do that by creating planets.xsl.



Inside XSLT
Inside Xslt
ISBN: B0031W8M4K
EAN: N/A
Year: 2005
Pages: 196

Similar book on Amazon

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