5

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)

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

Comments

0

The reason of the error is your incorrect xslt template. What do you want to achieve by applying xslt transformation? If by doing so you want to build POJO it is not a good idea..

At first you have to transform your initial xml file with xslt template and after this you have to unmarshal xml to your POJO using JAXB.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.