Xls xlst xml 1

Source: Internet
Author: User
Tags xsl xslt xslt processor

1. Introduction to XSLT
XSLT is the abbreviation of Extensible Stylesheet Language transformations.
XSLT is used to convert an XML document to another document type, which is usually XHTML.
XSLT uses two input files:
-XML document containing actual data
-XSL that contains the "architecture" of the XSLT command to insert data and contains the XSL document for inserting data and the XSLT command Architecture
The Code is as follows:
Hello. xml

<? XML version = "1.0" encoding = "UTF-8"?>
<? XML-stylesheet type = "text/XSL" href = "Hello. XSLT"?>
<Message> Hello, world! </Message>

Hello. XSLT

<? XML version = "1.0" encoding = "UTF-8"?>
<XSL: stylesheet version = "1.0" xmlns: XSL = "http://www.w3.org/1999/XSL/Transform"
Xmlns: msxsl = "urn: Schemas-Microsoft-com: XSLT" exclude-result-prefixes = "msxsl"
>
<XSL: output method = "XML" indent = "yes"/>

<XSL: template match = "/">
<HTML>
<Body>
<H1>
<XSL: value-of select = "message"/>
</H1>
</Body>
</Html>
</XSL: Template>
</XSL: stylesheet>
To see the effect, right-click "view in Browser ".

Find message
<XSL: template match = "/">
-This template selects the entire document.
You can think that it indicates selecting the root node of the XML tree
In this template:
-<XSL: value-of select = "message"/>
Indicates selecting the message element.
-You can also use the following XPath expression:
./Message
/Message/text () (text () is an XPATH function)
./Messa g e/text () g ()
The XSL documentation is as follows:
<XSL: template match = "/">
<HTML> <body>
<H1> <XSL: value-of select = "message"/>
</Body> </XSL: Template>
The <XSL: template match = "/"> chooses the root
The <HTML> <body> The contents of message is written to the output file
The Final result:
<HTML> <body>
<H1> Hello, world! </H1>
</Body>  
2. How XSLT works
A) First, the XML document is read into the memory and stored as a node tree.
B) <XSL: template match = "/"> the template is used to select the root node of the entire tree.
C) The rules in the template are used for node matching to change the structure of the XML tree.
D) if there are other templates, they must be displayed and called from the master template. If there are other templates, they must be displayed and called from the master template.
E) the unmatched parts in the XML tree remain unchanged.
F) after the template is applied, the tree is output as a new text document.

3. XSLT syntax
A) XSL: Value
<XSL: value-of select = "XPath expression"/>
Selects the contents of an element and adds it to the output stream
-The select attribute is required
-Notice that XSL: value-of is not a iner, hence it needs to end with a slash
Example:
<H1> <XSL: value-of select = "message"/>
B) XSL: For-each
XSL: For-each is a kind of loop statement
The syntax is
<XSL: For-each select = "XPath expression">
Text to insert and rules to apply
</XSL: For-each>
Example: To select every book (// book) and make
Unordered list (<ul>) of their titles (title ),
Use:
<Ul>
<XSL: For-each select = "// book">
<Li> <XSL: value-of select = "title"/> </LI>
</XSL: For-each>
</Ul>

C) Filter
You can filter (restrict) output by adding a criterion to the select attribute's value:
<Ul>
<XSL: For-each select = "// book">
<Li>
<XSL: value-of select = "title [../author = 'refactor ']"/>
</LI>
</XSL: For-each>
</Ul>
This will select book titles by refactor
Learn more:
Here is the filter we just used:
<XSL: value-of select = "title [../author = 'refactor ']"/>
Authoris a sibling of title, so from title we have to go up to its parent,
Book, then back down to author
This filter requires a quote within a quote,
So we need both single quotes and double quotes
Legal filter operators are:
=! = & Lt; & gt;
-Numbers shoshould be quoted, but apparently don't have to be
D) XSL: If
XSL: If allows us to include content if a given condition (in the test attribute) is ture
Example:
<XSL: For-each select = "// book">
<XSL: If test = "author = 'reface'">
<Li>
<XSL: value-of select = "title"/>
</LI>
</XSL: If>
</XSL: For-each>

E) XSL: Choose
The XSL: Choose... XSL: When... XSL: otherwise construct is XML's equivalent of C #'s switch...
Case default statement case... default statement
The syntax is:
<XSL: Choose>
<XSL: When test = "some condition">
... Some code...
</XSL: When>
<XSL: otherwise>
... Some code...
</XSL: otherwise>
</XSL: Choose>
F) XSL: Sort
You can place an XSL: Sort inside an XSL: For-eash
The attribute of the sort tells what field to sort on
Example:
<Ul>
<XSL: For-each select = "// book">
<XSL: Sort select = "author"/>
<Li> <XSL: value-of select = "title"/>
<XSL: value-of select = "author"/> </LI>
</XSL: For-each>
</Ul>
-This example creates a list of titles and authors sorted by author
G) XSL: Text
XSL: text helps deal with two common problems:
-XSL isn't very careful with whitespace in the document
This doesn' t matter much for HTML, which collapses all whitespace anyway
(Though the HTML source may look uugly)
<XSL: Text> gives you much better control over whitespace;
It acts like the <PRE> element in HTML
-Since XML defines only five entities, you cannot readily put other entities
(Such as & nbsp;) in your XSL
& Amp; nbsp; almost works, but & nbsp; is visible on the page
Here's the secret formula for entities
H) XSL: Apply-templates
The <XSL: Apply-templates> element applies a template rule
The current element or to the current element's Child Nodes
If we add a select attribute, it applies
Template rule only to the child that matches
If we have multiple <XSL: Apply-templates> elements with select attributes,
The child nodes are processed in the same order as the are processed
In the same order as the <XSL: Apply-templates> Elements

4. When will the template be ignored?
Templates aren't used unless they are applied
-Exception: Processing always starts with select = "/"
-If it didn't, nothing wowould ever happen
If your templates are ignored, you probably forgot to apply them
If you apply a template to an element that has child elements, templates are not automatically
Applied to those child elements applied to those child elements

5. Apply the template to the child element
<Book>
<Title> XML </title>
<Author> refactor </author>
</Book>
<XSL: template match = "/">
<HTML> <B> <XSL: value-of select = "/book/Title"/> </B>
<XSL: Apply-templates select = "/book/author"/>
</Body> </XSL: Template>
<XSL: template match "/book/author">
By <I> <XSL: value-of select = "."/> </I>
</XSL: Template>

6. Call the naming Template
You can name a template, then call it, similar to the way you wowould
Call a method in C #
The named template:
<XSL: Template Name = "mytemplatename">
... Body of template...
</XSL: Template>
A call to the template:
<XSL: Call-Template Name = "mytemplatename"/>
Or:
<XSL: Call-Template Name = "mytemplatename">
... P arameters... P
</XSL: Call-template>

7. Use the parameter Application Template
Parameters, if present, are encoded in the content of XSL: Template,
But are the only content of XSL: Call-template
Example call:
<XSL: Call-Template Name = "doonetype">
<XSL: With-Param name = "Header" select = "'ctures '"/>
<XSL: With-Param name = "nodes" select = "// lecture"/>
</XSL: Call-template>
Example template:
<XSL: Template Name = "doonetype"> XSL: Template Name doonetype
<XSL: Param name = "Header"/>
<XSL: Param name = "nodes"/>
... Template body... refer to parameters as "$ Header" and "$ nodes"
</XSL: Template>
Parameters are matched up by name, not by position
 
8. Implement XSLT in. net
A) use the compiledtransform class
The xslcompiledtransform class is a. NET Framework XSLT processor.
This class is used to compile a style sheet and execute XSLT conversion.
B) inputs of the compute compiledtransform class
Ixpathnavigable Interface
-The xmlnode class is based on the W3C Document Object Model (DOM) and has the editing function.
-The xpathdocument class is a read-only data storage based on the XPath data model.
Xpathdocument is a recommended class for XSLT processing. Compared with the xmlnode class
Class has better performance.
Xmlreader object
-The transform method is loaded from the current node of xmlreader and all its subnodes. In this way,
You can use part of the document as the context document. After the transform method returns,
Xmlreader is located on the next node after the end of the context document. If you already have
The end of the file. xmlreader will be at the end of the file (EOF ).
String URI
-You can also specify the source document URI as An XSLT input. Xmlresolver is used to parse the URI.
You can specify the xmlresolver to be used by passing it to the transform method.
If xmlresolver is not specified, the transform method uses the default without creden
Xmlurlresolver.
C) Compile compiledtransform class output
Xmlwriter
-Xmlwriter class outputs XML streams or files. You can use the xmlwritersettings class to specify
Functions to be supported on xmlwriter objects, including output options.
The xmlwriter class is an essential part of the system. xml framework.
This output type can be used to send the output results to another XML Process through a pipeline.
String
-You can use this output type to specify the URI of the output file.
Stream
-Stream is the abstraction of byte sequences, such as files, input/output devices, communication pipelines in processes, or TCP/IP sets.
. The stream class and its derived classes provide a general view of these different types of input and output, enabling the program
It is isolated from the details of the Basic device of the operating system.
-This output type can be used to send data to filestream, memorystream, or output stream.
(Response. outputstream ).
Textwriter
-Textwriter: output sequence characters. This output type is implemented in the stringwriter and streamwriter classes.
Output characters to strings or streams respectively. If you want to output data to a string, use this output type.
9. Convert
// Load an xpathdocument
Xpathdocument Doc = new xpathdocument ("books. xml ");
// Locate the node Fragment
Xpathnavigator nav = Doc. createnavigator ();
Xpathnavigator mybook = nav. selectsinglenode ("Descendant: Book [@ ISBN = '0-201-63361-2 ']");
// Create a new object with just the node fragment.
Xmlreader reader = mybook. readsubtree ();
Reader. movetocontent ();
// Load the style sheet.
Extends compiledtransform XLST = new extends compiledtransform ();
XSLT. Load ("single. XSL ");
// Transform the node Fragment
XSLT. Transform (reader, xmlwriter. Create (console. Out, XSLT. outputsettings ))

10. The following section focuses on instances.

To be continued, please wait...

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.