以下内容为程序代码: <?xml version="1.0" encoding="ISO-8859-1" ?> <bean> <name>Product</name> <comments>This bean represents a product that the 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 was tested on animals</comments> </property> <property> <name>availableSince</name> <type>java.util.Date</type> <comments>the date when the company started offering this product to its customers</comments> </property> </bean> |
以下内容为程序代码: <?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"> <!--这里就是指定通过这个xsl所转换的结果的类型,是text格式--> <xsl:output method = "text"/> <!--xslt使用模版来处理xml中的节点--> <xsl:template match="bean"> /** * <xsl:value-of select="comments"/>//这里是获取xml文档中的节点值 * 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:apply-templates select="property"/> } </xsl:template> <xsl:template match="property"> private <xsl:value-of select="type"/> <xsl:text> </xsl:text>//输出文本,这里是输出一个空格 <xsl:value-of select="name"/>; <xsl:variable name="name" select="name"/>//定义xsl中要使用的变量 <xsl:variable name="cname" select="java:Capitalizer.capitalize($name)"/>//这里使用了xslt extensions,它可以允许在xslt中直接引用java中方法,非常方便。 /** * 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"/>; } /** * Returns <xsl:value-of select="comments"/> * @return <xsl:value-of select="comments"/> */ public <xsl:value-of select="type"/><xsl:text></xsl:text> <xsl:apply-templates select="type"/><xsl:value-of select="$cname"/>() { return <xsl:value-of select="name"/>; } </xsl:template> <xsl:template match="type"> <xsl:variable name="type" select="."/> <xsl:choose> <xsl:when test="$type='boolean'">is</xsl:when> <xsl:otherwise>get</xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet> |
以下内容为程序代码: /** * This bean represents a product that 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 was tested on animals * @param testedOnAnimals is the flag that indicates if the product was tested on animals */ public void setTestedOnAnimals(boolean testedOnAnimals) { this.testedOnAnimals = testedOnAnimals; } /** * Returns the flag that indicates if the product was tested on animals * @return the flag that indicates if the product was 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; } } |
……