How to convert xslt result to java objects
I have an XML file :
<?xml version="1.0" encoding="ISO-8859-1"?> <catalog> <cd> <title>Empire Burlesque</title> <artist>Bob Dylan</artist> <country>USA</country> <company>Columbia</company> <price>10.90</price> <year>1985</year> </cd> </catalog> And this XSL file :
<?xml version="1.0" ?> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <xsl:value-of select="/catalog/cd/artist"/> <xsl:variable name = "artist" select = "/catalog/cd/artist()"/> <xsl:variable name="year" select="/catalog/cd/year()"/> <xsl:Object-bean name="{$artist}" id="{$year}"> </xsl:Object-bean> </xsl:template> </xsl:stylesheet> Now I want to convert the result to a java class.
Java:
@XmlRootElement(name = "Object-bean") @XmlAccessorType(XmlAccessType.NONE) public class ObjectBean { @XmlAttribute(name = "name") private String name; @XmlAttribute private String id; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } } but when I run it, it will show me this error:
Error at xsl:Object-bean on line 7 column 49 of test.xsl: XTSE0010: Unknown XSLT element: Object-bean Exception in thread "main" javax.xml.transform.TransformerConfigurationException: Failed to compile stylesheet. 1 error detected. at net.sf.saxon.PreparedStylesheet.prepare(PreparedStylesheet.java:176) at net.sf.saxon.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:139) at net.sf.saxon.TransformerFactoryImpl.newTransformer(TransformerFactoryImpl.java:91) at XslExecutor.main(XslExecutor.java:28) please help as soon as possible.
XML stores raw data (document A). XSLT is a conversion template that converts XML data (Document A) into another XML document (Document B). And finally, you are trying to customize the output of an XSLT template (Document B) to a POJO annotated with JAXB. JAXB annotations work like the XSLT template. They provide a binding mechanism between XML and POJO.
XSLT JAXB (XML document A) ---------------------> (XML document B) --------------- - ----> POJO
This explains, simply in order to establish a common understanding, the output you show suggests that the XSLT transform does not work. In fact, the XSL that you provide is completely erroneous. Start with something similar that works with the XML you provided:
<?xml version="1.0" ?> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <xsl:element name="Object-bean"> <xsl:attribute name="artist"> <xsl:value-of select="/catalog/cd/artist"/> </xsl:attribute> <xsl:attribute name="year"> <xsl:value-of select="/catalog/cd/year"/> </xsl:attribute> </xsl:element> </xsl:template> </xsl:stylesheet> The cause of the error is your invalid xslt template. What do you want to achieve with xslt conversion? If at the same time you want to build a POJO, this is not a good idea.
First you need to convert your initial xml file using the xslt template, after which you need to untie the xml with the POJO using JAXB.