Generate the html DOCTYPE element using xsl: output

Source: Internet
Author: User
Tags xsl xslt xslt processor
If you wowould like to generate the DOCTYPE specification in the html output when using xslt transformation, you can not write the code as following: <? Xml version = "1.0" encoding = "UTF-8"?>
<Xsl: stylesheetversion = "1.0"
Xmlns: xsl = "http://www.w3.org/1999/XSL/Transform">
<Xsl: template match = "/">
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html>
<Body>
</Body>
</Html>
</Xsl: template>
</Xsl: stylesheet>

It is because we can not write the DOCTYPE element in the xslt file, it's not a well-formed xml element. To achieve this goal you must use xsl: output, just as below <? Xml version = "1.0" encoding = "UTF-8"?>
<Xsl: stylesheetversion = "1.0"
Xmlns: xsl = "http://www.w3.org/1999/XSL/Transform">
<Xsl: output
Method = "html"
Encoding = "UTF-8"
Doctype-public = "-// W3C // dtd xhtml 1.0 Transitional // EN"
Doctype-system = "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>
<Xsl: template match = "/">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> </title>
</Head>
<Body>
</Body>
</Html>
</Xsl: template>
</Xsl: stylesheet>

The result is something like: <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> </title>
</Head>
<Body>
</Body>
</Html>

In the xsl: output element:
Method: identifies the method that shocould be used for outputting the result, it can be one of xml, html or text.
Encoding: specifies the preferred character encoding that the XSLT processor shocould use to encode sequences of characters
Sequences of bytes
Doctype-public: specifies the public identifier to be used in the document type declaration. If given this attribute it will generate a DOCTYPE in the output document, such as <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN">
Doctype-system: specifies the system identifier to be used in the document type declaration. If given this attribute it will generate a DOCTYPE in the output
Document, such as <! DOCTYPE html SYSTEM "./dtds/book. dtd">
If both the doctype-public and doctype-system attributes are specified in the xsl: output element, for example, <xsl: output
Method = "html" doctype-public = "-// W3C // dtd xhtml 1.0 Transitional // EN"
Doctype-system = "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
/> Will generate the DOCTYPE like: <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">. In this situation the doctype-system only identifies the resource (DTD) referenced.
Cdata-section-elements: specifies a list of the names of elements whose text node children shoshould be output using CDATA sections, for example, cdata-section-elements = "script" will result in all the text in the elements <script> </script> are treated just as they are in <! [CDATA []> by the XSLT processor.

The c # code handling the transformation is below: Using System;
Using System. Xml;
Using System. Xml. Xsl;
Using System. Xml. XPath;
Using System. Text;
Using System. IO;

This. Response. ContentType = "text/html ";
// Retrieve the xml document object from the model
XmlDocument doc = BoxImpl. BoxXml (this. _ templateId, this. _ type, this. _ boxId, this. _ op );
// In. NET framework 2.0, it is recommended that use the specified compiledtransform instead of your Transform
Extends compiledtransform transform = new extends compiledtransform ();
Transform. Load (this. Server. MapPath ("cc. xsl "));
MemoryStream stream = new MemoryStream ();
Transform. Transform (doc. CreateNavigator (), null, stream );
// Maybe the Position was moved to the end of the MemoryStream while transforming,
// So it's necessary to set this attribute to 0 before reading the stream
Stream. Position = 0;
// The encoding attribute specified in the xsl: output node is UTF-8,
// So here we read the output stream using the System. Text. Encoding. UTF8 correspondingly
StreamReader reader = new StreamReader (stream, System. Text. Encoding. UTF8 );
This. Response. Write (reader. ReadToEnd ());
Reader. Close ();
Stream. Close ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.