Java EE XML xsl Transformation (XSLT)

Source: Internet
Author: User

XSL Transformations (XSLT)

@author Ixenos

Defined:

The XSL transformation mechanism can specify rules that convert XML documents to other formats, such as TXT plain text, XHTML, or any other XML format.

Use:

XSLT is often used to translate a machine-readable XML format into another machine-readable XML format, or to translate XML into a representation format suitable for human reading

Steps:

1. An XSLT stylesheet is required that describes the rules for converting an XML document to a format

2. The XSLT processor reads the XML document and the XSLT style sheet , producing the desired output

XSLT style sheet:

<?xml version= "1.0" encoding= "Utf-8"?>

<xsl:stylesheet

Xmlns:xsl=http://www.w3c.org/1999/xsl/transform

Version= "1.0" >

<xsl:output method= "html"/>//Specify the output format as HTML, others have XML, text

Template1 templates

.

.

.

Template2

</xsl:stylesheet>

Template example:

<xsl:template match= "/staf/employee" >

<tr><xsl:apply-templates/></tr>

</xsl:template>

The value of the Match property is an XPath expression

The template represents: whenever you understand a node in an XPath set/staff/employee

(1) Generate string <tr>

(2) Continue to apply the template for the child elements to be processed (recursive!) )

(3) When all child elements have been processed, the string </tr>

Copy the attribute value to the template in the output:

<xsl:template match= "/staff/employee/hiredate" >

<td><xsl:value-of Select= "@year"/>-<xsl:value-of select= "@month"/>-<xsl: value-of select= "@day"/></td>

</xsl:template>

Here the xsl:value-of statement is used to calculate the node set (attribute node) specified by the XPath value of the Select, String value

XML go to HTML example:

<?xml version= "1.0" encoding= "Iso-8859-1"?>

<xsl:stylesheet

Xmlns:xsl= "Http://www.w3.org/1999/XSL/Transform"

Version= "1.0" >

<xsl:output method= "html"/>//Specify Output as HTML format

<xsl:template match= "/staff" >//Using XPath to locate each

<table border= "1" ><xsl:apply-templates/></table>

</xsl:template>

<xsl:template match= "/staff/employee" >//Using XPath to locate each

<tr><xsl:apply-templates/></tr>

</xsl:template>

<xsl:template match= "/staff/employee/name" >//Using XPath to locate each

<td><xsl:apply-templates/></td>

</xsl:template>

<xsl:template match= "/staff/employee/salary" >//Using XPath to locate each

<td>$<xsl:apply-templates/></td>

</xsl:template>

<xsl:template match= "/staff/employee/hiredate" >//Using XPath to locate each

<td><xsl:value-of select= "@year"/>-<xsl:value-of

select= "@month"/>-<xsl:value-of select= "@day"/></td>

</xsl:template>

</xsl:stylesheet>

Convert to Plain text example:

<?xml version= "1.0"?>

<xsl:stylesheet

Xmlns:xsl= "Http://www.w3.org/1999/XSL/Transform"

Version= "1.0" >

<xsl:output method= "text" omit-xml-declaration= "yes"/>

<xsl:template match= "/staff/employee" >

employee.<xsl:value-of select= "position ()"//Note the middle Sporogenous the text employee.

/>.name=<xsl:value-of select= "Name/text ()"/>

employee.<xsl:value-of select= "position ()"

/>.salary=<xsl:value-of select= "Salary/text ()"/>

employee.<xsl:value-of select= "position ()"

/>.hiredate=<xsl:value-of select= "hiredate/@year"

/>-<xsl:value-of select= "hiredate/@month"

/>-<xsl:value-of select= "hiredate/@day"/>

</xsl:template>

</xsl:stylesheet>

XSL Transformations (XSLT) under the Java platform:

(1) Set up a converter factory for each style sheet, get a converter object, and let it convert a source into a result

File StyleSheet = new file (fileName); An XSL file

Streamsource Stylesource = new Streamsource (styleSheet);

Transformer t = transformerfactory.newinstance (). Newtransformer (Stylesource);

T.transform (source, result);

(2) Talk about source and result

The source interface has four implementation classes: Domsource, SAXSource, Staxsource, Streamsource, which enable us to pass through a file, stream, Reader, URL, Dom tree node to a source

For example: Domsource from the DOM tree node (the incoming document is equivalent to the entire tree)

The result interface has three implementation classes: Domresult, SAXResult, Streamresult

If you want to store the results of the conversion in the DOM tree, create a new document node wrapped in Domresult:

Document doc = Builder.newdocument ();

T.transform (source, new Domresult (DOC));

If you want to save the conversion results to a file, you can use the Streamresult

T.transform (source, new Streamresult (file));

Off-topic: a little trick to convert non-XML legacy data into XML

The Saxsource in source can be obtained from the SAX implementation of the XmlReader interface! You do not have to start working from an existing XML file.

First on the code:

T.transform (New SAXSource (New Employeereader (), New InputSource (new FileInputStream (filename)), result);

Where Employeereader is the XmlReader Sax implementation class, InputSource is the source of the file, result determines the form of preservation

Of course, most XSLT applications already have input data in XML format, and simply call the transform method on a Streamsource object:

T.transform (new Streamsource (file), result);

Java EE XML xsl Transformation (XSLT)

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.