2013-07-31 15 views
6

Ho un file XML:Come trasformare il risultato xslt in oggetti Java

<?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> 

E questo file XSL:

<?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> 

Ora voglio trasformare il risultato in Java classe.

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; 
    } 
} 

ma quando l'eseguo mi mostra questo errore:

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) 

si prega di aiuto subito.

risposta

1

L'XML contiene i dati originali (Documento A). XSLT è un modello di trasformazione che traduce i dati XML (Documento A) in un altro documento XML (Documento B). Infine, si sta tentando di eseguire il marshalling dell'output del modello XSLT (Documento B) in un POJO annotato con JAXB. Le annotazioni JAXB funzionano in modo simile al modello XSLT. Forniscono un meccanismo vincolante tra XML e POJO.

    XSLT       JAXB 

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

Ciò ha spiegato che, solo per impostare una comprensione comune, l'output che viene visualizzato indica che la trasformazione XSLT non riesce. In effetti l'XSL che fornisci è completamente sbagliato. Inizia con qualcosa di simile, che funziona con l'XML che hai fornito:

<?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> 
0

Il motivo dell'errore è il tuo modello xslt errato. Cosa vuoi ottenere applicando la trasformazione xslt? Se così facendo vuoi costruire POJO non è una buona idea ..

All'inizio devi trasformare il tuo file xml iniziale con il modello xslt e dopo questo devi unmarshal xml al tuo POJO usando JAXB.

+0

sì, questo è quello che voglio. – user2335004

Problemi correlati