XSLT conversion XML to XML

Source: Internet
Author: User
Tags xsl xsl file xslt xslt processor xquery

The following is an example of a Java transit scheme:

Books. xml

View plaincopy to clipboardprint?
<? XML version = "1.0" encoding = "ISO-8859-1"?>
<Bookstore>

<Book category = "Cooking">
<Title lang = "en"> everyday Italian </title>
<Author> Giada De laurentiis </author>
<Year> 2005 </year>
<Price> 30.00 </price>
</Book>

<Book category = "children">
<Title lang = "en"> Harry Potter </title>
<Author> j k. Rowling </author>
<Year> 2005 </year>
<Price> 29.99 </price>
</Book>

<Book category = "Web">
<Title lang = "en"> XQuery kick start </title>
<Author> James McGovern </author>
<Author> Per bothner </author>
<Author> Kurt Cagle </author>
<Author> James Linn </author>
<Author> vaidyan#nagarajan </author>
<Year> 2003 </year>
<Price> 49.99 </price>
</Book>

<Book category = "Web">
<Title lang = "en"> learning XML </title>
<Author> Erik T. Ray </author>
<Year> 2003 </year>
<Price> 39.95 </price>
</Book>
</Bookstore>
<? XML version = "1.0" encoding = "ISO-8859-1"?>
<Bookstore>
 
<Book category = "Cooking">
<Title lang = "en"> everyday Italian </title>
<Author> Giada De laurentiis </author>
<Year> 2005 </year>
<Price> 30.00 </price>
</Book>
 
<Book category = "children">
<Title lang = "en"> Harry Potter </title>
<Author> j k. Rowling </author>
<Year> 2005 </year>
<Price> 29.99 </price>
</Book>
 
<Book category = "Web">
<Title lang = "en"> XQuery kick start </title>
<Author> James McGovern </author>
<Author> Per bothner </author>
<Author> Kurt Cagle </author>
<Author> James Linn </author>
<Author> vaidyan#nagarajan </author>
<Year> 2003 </year>
<Price> 49.99 </price>
</Book>
 
<Book category = "Web">
<Title lang = "en"> learning XML </title>
<Author> Erik T. Ray </author>
<Year> 2003 </year>
<Price> 39.95 </price>
</Book>
</Bookstore>

Books. XSL

View plaincopy to clipboardprint?
<? XML version = "1.0" encoding = "ISO-8859-1"?>
<XSL: stylesheet version = "1.0" xmlns: XSL = "http://www.w3.org/1999/XSL/Transform">
<XSL: template match = "/">
<HTML>
<Body>
<H2> This is my books </H2>
<Table border = "1">
<Tr>
<TH> book title </Th>
<TH> book author </Th>
</Tr>
<XSL: For-each select = "Bookstore/book">
<Tr>
<TD> <XSL: value-of select = "title"/> </TD>
<TD> <XSL: value-of select = "author"/> </TD>
</Tr>
</XSL: For-each>


</Table>
</Body>
</Html>
</XSL: Template>
</XSL: stylesheet>
<? XML version = "1.0" encoding = "ISO-8859-1"?>
<XSL: stylesheet version = "1.0" xmlns: XSL = "http://www.w3.org/1999/XSL/Transform">
<XSL: template match = "/">
<HTML>
<Body>
<H2> This is my books </H2>
<Table border = "1">
<Tr>
<TH> book title </Th>
<TH> book author </Th>
</Tr>
<XSL: For-each select = "Bookstore/book">
<Tr>
<TD> <XSL: value-of select = "title"/> </TD>
<TD> <XSL: value-of select = "author"/> </TD>
</Tr>
</XSL: For-each>


</Table>
</Body>
</Html>
</XSL: Template>
</XSL: stylesheet>

Convert java files:

View plaincopy to clipboardprint?
Package com. LWF. test;
Import javax. xml. Transform. result;
Import javax. xml. Transform. transformer;
Import javax. xml. Transform. transformerconfigurationexception;
Import javax. xml. Transform. transformerexception;
Import javax. xml. Transform. transformerfactory;
Import javax. xml. Transform. transformerfactoryconfigurationerror;
Import javax. xml. Transform. Stream. streamresult;
Import javax. xml. Transform. Stream. streamsource;
Public class test {
/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
Try {
Streamsource xmlsource = new streamsource ("E: // workspace_g2 // TT/src // books. xml ");
Streamsource XSL = new streamsource ("E: // workspace_g2 // TT // SRC ///books. XSL ");
Result outputtarget = new streamresult ("E: // workspace_g2 // TT/src // bookr. xml ");
Transformer Ts = transformerfactory. newinstance (). newtransformer (XSL );
TS. Transform (xmlsource, outputtarget );
} Catch (transformerconfigurationexception e ){
E. printstacktrace ();
} Catch (transformerfactoryconfigurationerror e ){
E. printstacktrace ();
} Catch (transformerexception e ){
E. printstacktrace ();
}
}
}
Package com. LWF. test;
Import javax. xml. Transform. result;
Import javax. xml. Transform. transformer;
Import javax. xml. Transform. transformerconfigurationexception;
Import javax. xml. Transform. transformerexception;
Import javax. xml. Transform. transformerfactory;
Import javax. xml. Transform. transformerfactoryconfigurationerror;
Import javax. xml. Transform. Stream. streamresult;
Import javax. xml. Transform. Stream. streamsource;
Public class test {
/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
Try {
Streamsource xmlsource = new streamsource ("E: // workspace_g2 // TT/src // books. xml ");
Streamsource XSL = new streamsource ("E: // workspace_g2 // TT // SRC ///books. XSL ");
Result outputtarget = new streamresult ("E: // workspace_g2 // TT/src // bookr. xml ");
Transformer Ts = transformerfactory. newinstance (). newtransformer (XSL );
TS. Transform (xmlsource, outputtarget );
} Catch (transformerconfigurationexception e ){
E. printstacktrace ();
} Catch (transformerfactoryconfigurationerror e ){
E. printstacktrace ();
} Catch (transformerexception e ){
E. printstacktrace ();
}
}
}
 

Converted file bookr. xml

View plaincopy to clipboardprint?
<HTML>
<Body>
<H2> This is my books </H2>
<Table border = "1">
<Tr>
<TH> book title </Th> <TH> book author </Th>
</Tr>
<Tr>
<TD> everyday Italian </TD> <TD> Giada De laurentiis </TD>
</Tr>
<Tr>
<TD> Harry Potter </TD> <TD> j k. Rowling </TD>
</Tr>
<Tr>
<TD> XQuery kick start </TD> <TD> James McGovern </TD>
</Tr>
<Tr>
<TD> learning XML </TD> <TD> Erik T. Ray </TD>
</Tr>
</Table>
</Body>
</Html>
<HTML>
<Body>
<H2> This is my books </H2>
<Table border = "1">
<Tr>
<TH> book title </Th> <TH> book author </Th>
</Tr>
<Tr>
<TD> everyday Italian </TD> <TD> Giada De laurentiis </TD>
</Tr>
<Tr>
<TD> Harry Potter </TD> <TD> j k. Rowling </TD>
</Tr>
<Tr>
<TD> XQuery kick start </TD> <TD> James McGovern </TD>
</Tr>
<Tr>
<TD> learning XML </TD> <TD> Erik T. Ray </TD>
</Tr>
</Table>
</Body>
</Html>
 

In this way, the server converts XML to XSL.

The server can also convert the document type into another document by using the XSL file. The Code is as follows:

View plaincopy to clipboardprint?
Public static document generatexmldom (
Document tradedoc,
String delimiter file ){
Document Doc = NULL;

Try {
Documentbuilderfactory DBF = documentbuilderfactory. newinstance ();
Documentbuilder DB = DBF. newdocumentbuilder ();
Document document = dB. parse (invalid file );
Domsource source = new domsource (document );
Transformer TF = transformerfactory. newinstance (). newtransformer (source); // single thread
// Templates Ts = transformerfactory. newinstance (). newtemplates (source );
// Transformer TF = ts. newtransformer ();

TF. Transform (New domsource (tradedoc), new domresult (DOC ));
} Catch (transformerconfigurationexception e ){
E. printstacktrace ();
} Catch (parserconfigurationexception e ){
E. printstacktrace ();
} Catch (saxexception e ){
E. printstacktrace ();
} Catch (ioexception e ){
E. printstacktrace ();
} Catch (transformerfactoryconfigurationerror e ){
E. printstacktrace ();
} Catch (transformerexception e ){
E. printstacktrace ();
}

Return Doc;
}

 

========================================================== ========================================================== ==========

XSLT is a language used to convert XML documents into other text documents. It is an international standard based on XML and XPath. It has many contents and is powerful.

For programmers, XSLT can be seen as a markup language that specifically processes the XML tree structure in the previous sequential traversal mode. Before programming, when outputting plain text data based on XML documents, you need to write the code in the previous sequential traversal mode to traverse the tree structure composed of XML document objects, for each XML node with a specific name or level to output different content, this process is complicated, the code is large, and a lot of status judgment is required. XSLT implements the same logic using a concise markup language. Therefore, XSLT is similar to a programming language that supports recursion from the perspective of program logic, and is specialized in processing XML documents.

The XSLT conversion process involves three text documents: one is the original XML document to be processed, and the other is the XSLT style sheet document, which contains the XSLT code, the XSLT code itself is in XML format, but uses the XML namespace. The third is the text document output by XSLT processing. Note that the output here is a plain text document. The specific format of this document is determined by the XSLT code. It can be another XML document, for HTML documents, SQL statement strings, or other string data in any format, XSLT conversion can only output plain text documents. In addition, there is no limit on the specific format of output documents.

The data XML document is a simple XML document, which is not described here. Let's focus on the XSLT style sheet document. The XSLT style sheet document itself is an XML document. It uses the tree structure of XML to describe the recursive processing process.

In the style sheet document, the root element is XSL: stylesheet, which defines a namespace named XSL. The root node and its attribute values are fixed.

The XSL: Output element is optional. Its method attribute is used to specify the output document format. It can be set to XML, HTML, or text values. The XML output style is used here, indicating that the output document is in XML format. XSLT conversion will generate XML documents as much as possible, but it is not guaranteed. Therefore, unqualified XML documents may still be generated.

XSL: Template is used to define an XSLT template. The template is similar to a function in a programming language and can be reused in XSLT code. The template can use the name attribute to define the name, or the match attribute to define the matching XPath path. This template uses the match attribute to match the XML document itself.

Then there is an HTML element. Because the HTML element does not use the XSL prefix, it does not belong to the XSLT code. Therefore, it is output as is, and the table element is the same as the following body.

The XSL: For-each element is similar to the foreach syntax result in C #, indicating the loop traversal element. It uses the select attribute to specify an XPATH relative path, XSLT uses this relative path to query all XML nodes to be traversed. The current node is the XML file itself. Therefore, the XSLT processor will call the selectnodes function of xmldocument to obtain the XML node to be traversed, the function parameter is table/record. So we started to traverse all the record elements cyclically.

When traversing a record element cyclically, each record element must output a child node of XSL: For-each. The first element is the tr element. This is not an XSLT element, so it is output as is. Another for-each element is defined here, so we start a new loop traversal. The relative XPath path specified by the new loop is an asterisk, the child element that matches all names. This is similar to using asterisks in the doscommand dir to match all files. In this example, all the field elements under the record element are cyclically traversed.

For each field element, first output the TD element, and then process the XSL: value-of element. XSL: value-of indicates the value of the node in the specified relative path, the specified XPath is a vertex, indicating the current node itself. Because the current node is an XML element, the text content of the output element is equivalent to the innertext attribute value of the output xmlelement.

XSLT file:

<? 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" encoding = "UTF-8"/>

<XSL: template match = "/">
<XSL: For-each select = "category">
<Category>
<XSL: attribute name = "navigate">
<XSL: value-of select = "@ navigate"/>
</XSL: attribute>
<XSL: element name = "name">
<XSL: value-of select = "name"/>
</XSL: Element>
<XSL: element name = "engname">
<XSL: value-of select = "engname"/>
</XSL: Element>
<XSL: element name = "chtname">
<XSL: value-of select = "chtname"/>
</XSL: Element>
<XSL: element name = "Description">
<XSL: value-of select = "Description"/>
</XSL: Element>
<XSL: element name = "engdescription">
<XSL: value-of select = "engdescription"/>
</XSL: Element>
<XSL: element name = "chtdescription">
<XSL: value-of select = "chtdescription"/>
</XSL: Element>
<XSL: element name = "Editor">
<XSL: value-of select = "Editor"/>
</XSL: Element>
<XSL: element name = "lastmodified">
<XSL: value-of select = "lastmodified"/>
</XSL: Element>
<XSL: element name = "Identity">
<XSL: Text> cat_ztf </XSL: Text>
</XSL: Element>
<XSL: element name = "Notes">
<XSL: value-of select = "Notes"/>
</XSL: Element>
<XSL: element name = "totalcount">
<XSL: Text> {0} </XSL: Text>
</XSL: Element>
<XSL: For-each select = "content">
<XSL: For-each select = "catitem">
<XSL: element name = "catitem">
<XSL: attribute name = "name">
<XSL: value-of select = "@ name"/>
</XSL: attribute>
<XSL: attribute name = "code">
<XSL: value-of select = "@ code"/>
</XSL: attribute>
<XSL: attribute name = "parentcode">
<XSL: value-of select = "@ parentcode"/>
</XSL: attribute>
<XSL: attribute name = "categorytype">
<XSL: Text> cat_ztf </XSL: Text>
</XSL: attribute>
</XSL: Element>
</XSL: For-each>
</XSL: For-each>
</Category>
</XSL: For-each>
</XSL: Template>
</XSL: stylesheet>

Original XML file:

<? XML version = "1.0" encoding = "gb2312"?>
<Category navigate = "1">
<Name> dedicated Category </Name>
<Engname> New </engname>
<Chtname> classification </chtname>
<Description/>
<Engdescription/>
<Chtdescription/>
<Editor> Liangying </Editor>
<Lastmodified> 2010-5-31 </lastmodified>
<Identity> cat_xxxx </identity>
<Notes> dedicated Category </Notes>
<Content COUNT = "97">

<Catitem name = "文" description =" "engdescription =" "chtdescription =" "code =" 001 "parentcode =" "nav =" tttt? = {0} "/>
<Catitem name = "" Description = "" engdescription = "" chtdescription = "" code = "001001" parentcode = "001" nav = "tttt? = {0} "/>
<Catitem name = "" Description = "" engdescription = "" chtdescription = "" code = "001002" parentcode = "001" nav = "tttt? = {0} "/>
<Catitem name = "Prose" Description = "" engdescription = "" chtdescription = "" code = "001003" parentcode = "001" nav = "tttt? = {0} "/>

</Content>
</Category>

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.