Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to transform an xslt result into 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 transform the result into 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 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)
like image 209
user2335004 Avatar asked Nov 12 '22 00:11

user2335004


1 Answers

The XML holds the original data (Document A). The XSLT is a transformation template that translates the XML data (Document A) into other XML document (Document B).And finally you are trying to marshall the output of the XSLT template (Document B) into a POJO annotated with JAXB. JAXB annotations work similar to the XSLT template. They provide a binding mechanism between XML and a POJO.

                  XSLT                           JAXB

(XML Document A) ---------------------> (XML Document B) -------------------->POJO

That explained, just to set a common understanding, the output you are showing says the XSLT transformation is failing. In fact the XSL you provide is completely wrong. Start with something like this, 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>
like image 139
Daniel Cerecedo Avatar answered Nov 14 '22 21:11

Daniel Cerecedo