XSL Parsing Process

Source: Internet
Author: User
Tags xsl xsl file

 
XSL Parsing process:

When XSLT is transforming, it first traverses the XML Source Document Tree, finds the node to be processed, and then applies the defined template information to the node.

XML:

<? XML version = "1.0" encoding = "gb2312" standalone = "no"?>
<? XML: stylesheet type = "text/XSL" href = "bbs2.xsl"?>
<BBS xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: nonamespaceschemalocation = "bbs. XSD">
<Root id = "1">
<Title> learning Java </title>
<Content> How do you learn Java? </Content>
<Sender> sunny </sender>
<Sendtime> 09-3-21 </sendtime>
<Istop> 0 </Istop>
<Isgood> 0 </isgood>
<Answer id = "1">
<Title> learning Java </title>
<Content> first, be patient and patient. </content>
<Sender> Li shengguang </sender>
<Sendtime> 09-3-21 </sendtime>
</Answer>
<Answer id = "1">
<Title> learning Java </title>
<Content> code multiple times. </content>
<Sender> LSG </sender>
<Sendtime> 09-3-21 </sendtime>
</Answer>
</Root>
<Root id = "423bda04-2aa8-4f52-af75-641718c423da">
<Title> what is JSP </title>
<Content> JSP is a Java Server Page! </Content>
<Sender> Li shengguang </sender>
<Sendtime> mon Mar 23 16:28:00 CST 2009 </sendtime>
<Istop> 0 </Istop>
<Isgood> 0 </isgood>
</Root>
<Root id = "16eb57fb-d7fa-47a3-b33e-617968bd54a2">
<Title> JAVA concept </title>
<Content> what are the three main features of Java? </Content>
<Sender> Li shengguang </sender>
<Sendtime> mon Mar 23 16:33:50 CST 2009 </sendtime>
<Istop> 0 </Istop>
<Isgood> 0 </isgood>
</Root>
<Root id = "ec30d056-074e-4af1-9148-acf4500a86c8">
<Title> JAVA is good </title>
<Content> JAVA is good </content>
<Sender> sunny </sender>
<Sendtime> Tue Mar 24 08:25:24 CST 2009 </sendtime>
<Istop> 0 </Istop>
<Isgood> 0 </isgood>
</Root>
</BBS>

 

The first method of XSL:

<? XML version = "1.0" encoding = "UTF-8"?>

<XSL: stylesheet xmlns: XSL = "http://www.w3.org/1999/XSL/Transform" version = "1.0">
<XSL: template match = "/">
<HTML>
<Head>
<Title> bbs. XSL </title>
</Head>
<Body>
<H2 align = "center"> Software Technology home </H2>
<Center>
<Table border = "1" width = "80%">
<Tr>
<TD width = "70%"> title </TD>
<TD width = "15%"> poster </TD>
<TD width = "15%"> posting time </TD>
</Tr>
<XSL: For-each select = "BBS/root">
<Tr>
<TD width = "60%"> <XSL: value-of select = "title"/> </TD>
<TD width = "15%"> <XSL: value-of select = "sender"/> </TD>
<TD width = "30%"> <XSL: value-of select = "sendtime"/> </TD>
</Tr>
</XSL: For-each>
</Table>
</Center>
</Body>
</Html>
</XSL: Template>

</XSL: stylesheet>

Process Analysis:
First, parse the converter to load XML to a tree in the memory form, and then start (top-down) to read the XSL file according to the <XSL: template match = "/"> Find and locate the node in the tree, and then click <XSL: template match = "/"> the content in the tag is written to the target HTML file. When a tag in the XSL namespace is encountered, the corresponding tag function is executed. If <XSL: for-each select = "BBS/root">, perform node loop matching in the XML document. Each time a corresponding node is found, the corresponding value is obtained. returns the result after the loop ends. <XSL: template match = "/"> This sentence matches the next sentence. The parsing ends because there is only one.

XSL in the second method:

<? XML version = "1.0" encoding = "UTF-8"?>
<XSL: stylesheet version = "1.0" xmlns: XSL = "http://www.w3.org/1999/XSL/Transform" xmlns: fo = "http://www.w3.org/1999/XSL/Format">
<XSL: template match = "BBS">
<HTML>
<Head>
<Title> Software Technology home </title>
</Head>
<Body>
<H2 align = "center"> Software Technology home 2 </H2>
<Center>
<Table border = "1" width = "80%">
<Tr>
<TD width = "70%"> title </TD>
<TD width = "15%"> poster </TD>
<TD width = "15%"> posting time </TD>
</Tr>
<XSL: Apply-templates select = "root"/>
</Table>
</Center>
</Body>
</Html>
</XSL: Template>

<XSL: template match = "root">
<Tr>
<TD width = "60%"> <XSL: value-of select = "title"/> </TD>
<TD width = "15%"> <XSL: value-of select = "sender"/> </TD>
<TD width = "30%"> <XSL: value-of select = "sendtime"/> </TD>
</Tr>
</XSL: Template>
</XSL: stylesheet>

Process Analysis:
First, parse the converter to load XML to a tree in the memory form, and then start (top-down) to read the XSL file according to the <XSL: template match = "BBS"> Find and locate the node in the tree. First, find the BBS node in XML and output it in the HTML file:
<HTML>
<Head>
<Title> Software Technology home </title>
</Head>
<Body>
<H2 align = "center"> Software Technology home 2 </H2>
<Center>
<Table border = "1" width = "80%">
<Tr>
<TD width = "70%"> title </TD>
<TD width = "15%"> poster </TD>
<TD width = "15%"> posting time </TD>
</Tr>
The system can see the <XSL: Apply-templates select = "root"/> indication, so it searches for the node marked as "root" in the XML source tree for matching. Like a function call, the system jumps to the "function" defined in <XSL: template match = "root"> to generate the following HTML code:
<Tr>
<TD width = "60%"> <XSL: value-of select = "title"/> </TD>
<TD width = "15%"> <XSL: value-of select = "sender"/> </TD>
<TD width = "30%"> <XSL: value-of select = "sendtime"/> </TD>
</Tr>
The parser continues to match the "root" tag in XML until the XML does not have the root tag. In this way, the code above the multi-segment can be obtained. <XSL: template match = "root"> after the matching is completed, run:
</Table>
</Center>
</Body>
</Html>
After the execution, the tag "BBS" is matched. Because the "BBS" tag only ends with one matching.

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.