Dynamic XSL itself is a well-formed XML that converts an XML document into another XML document, or transforms it into a text file, HTML file, and so on. Here is the use of XSL to dynamically generate the Java files we want (in some ways, Java code is actually a text file), hoping to see through this article, XML and related technologies have the power!
Let's start with an XML example, where we'll extract useful information from the XML file using an XSL to generate Java code (actually a JavaBean):
The following is the program code
<?xml version= "1.0" encoding= "Iso-8859-1"?>
<bean>
<name>Product</name>
<comments>this Bean represents a product that's company
Offers to its customers</comments>
<property>
<name>code</name>
<type>int</type>
<comments>the Product Inventory code</comments>
</property>
<property>
<name>name</name>
<type>String</type>
<comments>the Product name</comments>
</property>
<property>
<name>testedOnAnimals</name>
<type>boolean</type>
<comments>the flag that indicates if the product is
Tested on animals</comments>
</property>
<property>
<name>availableSince</name>
<type>java.util.Date</type>
<comments>the Date started offering this
Product to its customers</comments>
</property>
</bean>
I'll just give you a translation of the XSL, and if you don't know the XSL very well, you can look at some of the data and then understand. I made a little comment in it:
The following is the program code
<?xml version= "1.0"?>
<xsl:stylesheet
Xmlns:xsl= "Http://www.w3.org/1999/XSL/Transform"
Version= "1.0"
Xmlns:java= "Http://xml.apache.org/xslt/java"
Exclude-result-prefixes= "Java" >
<!--here is the type that specifies the result that is converted through this XSL, which is the text format-->
<xsl:output method = "Text"/>
<!--XSLT uses templates to handle nodes in XML-->
<xsl:template match= "Bean" >
/**
* <xsl:value-of select= "Comments"/>//this is to get the node values in the XML document
* This class has been generated by the XSLT processor from the
Metadata
*/
public class <xsl:value-of select= "name"/> {
/**
* Creates a new instance of the <xsl:value-of
select= "name"/> Bean
*/
Public <xsl:value-of select= "name"/> () {}
<xsl:template match= "Property" >
Private <xsl:value-of select= "type"/>
<xsl:text> </xsl:text>//output text, here is the output of a space
<xsl:value-of select= "Name"/>;
<xsl:variable name= "name" select= "name"/>//define the variables to be used in the XSL
<xsl:variable name= "CNAME" select= "java:Capitalizer.capitalize ($name)"/>//used XSLT extensions here, It is convenient to allow direct referencing of Java methods in XSLT.
/**
* Sets <xsl:value-of select= "Comments"/>
* @param <xsl:value-of select= "name"/> is <xsl:value-of
select= "Comments"/>
*/
public void set<xsl:value-of select= "$cname"/> (<xsl:value-
of select= "type"/> <xsl:text> </xsl:text><xsl:value-
of select= "name"/>) {
this.<xsl:value-of select= "name"/> = <xsl:value-of
select= "Name"/>;
}
Well, after completing the above work, as long as in CMD, enter the following command line, you can get the results we want:
Java org.apache.xalan.xslt.process-in xmlsource-xsl stylesheet-out outputfile
The results are listed here:
The following is the program code
/**
* This bean represents a product, the company offers to its
Customers
* This class has been generated by the XSLT processor from the
Metadata
*/
public class Product {
/**
* Creates a new instance of the Product bean
*/
Public Product () {}
private int code;
/**
* Sets the Product Inventory code
* @param code is the product inventory code
*/
public void Setcode (int code) {
This.code = code;
}
/**
* Returns the Product Inventory code
* @return The Product Inventory code
*/
public int GetCode () {
return code;
}
private String name;
/**
* Sets the product name
* @param name is the product name
*/
public void SetName (String name) {
THIS.name = name;
}
/**
* Returns the product name
* @return The product name
*/
Public String GetName () {
return name;
}
Private Boolean testedonanimals;
/**
* Sets The flag that indicates if the product is tested on animals
* @param testedonanimals is the flag that indicates if the product
is tested on animals
*/
public void Settestedonanimals (Boolean testedonanimals) {
This.testedonanimals = testedonanimals;
}
/**
* Returns The flag that indicates if the product is tested on
Animals
* @return The flag that indicates if the product is tested on
Animals
*/
public Boolean istestedonanimals () {
return testedonanimals;
}
Private Java.util.Date availablesince;
/**
* Sets the date when the company started offering this product to
Its customers
* @param availablesince is the date when the company started
Offering this product to its customers
*/
public void Setavailablesince (Java.util.Date availablesince) {
This.availablesince = availablesince;
}
/**
* Returns the date when the company started offering this product
to its customers
* @return The date when the company started offering this product
to its customers
*/
Public Java.util.Date getavailablesince () {
return availablesince;
}
}
Summarize:
1. After familiarity with the basic use of XSL, understanding the above content is not difficult;
2. This is better suited to know some of the logic functions in advance, but for some reason, the need for dynamic generation, or to save unnecessary duplication of work, you can automatically generate code;
3. It is more convenient to modify this XSL.
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.