Setting Character Encoding

Setting Character Encoding

Now that were doing more substantial rewrites of source documents, its useful to know that you can set the character encoding of result documents with the encoding attribute of the <xsl:output> element. However, thats no guarantee that your XSLT processor supports the encoding you want to use, because XSLT processors are required to support only UTF-8 and UTF-16. On the other hand, if you use a character that is not supported in the character encoding youre using, the XSLT processor should either output the character as an entity reference or generate an error.

More on Character Encoding

For more on character encoding, see Inside XML . The only approved character encodings are specified in the XML 1.0 recommendation, www.w3.org/TR/REC-xml.

The encoding attribute in a documents XML declaration is optional; if its not included, XML parsers are supposed to assume the encoding is UTF-8 by default. However, if you want to make the character encoding explicit or use another character encoding, you can specify that encoding as follows , where Im specifying UTF-16:

 <?xml version="1.0"?>  <xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">  <xsl:output method="xml" encoding="UTF-16"/>    <xsl:template match="@*node()">      <xsl:copy>        <xsl:apply-templates select="@*node()"/>      </xsl:copy>    </xsl:template>  </xsl:stylesheet> 

And heres what appears in the result document:

 <?xml version="1.0" encoding="UTF-16"?>  <?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>          .          .          . 


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