getting error in xslt transformation through java

2019-03-07 03:41发布

I am trying to an xsl transformation that right now by a java object I am generating an intermediary XML and that XML is again passed to generate a new XML finally right now while in xsl transformation I am getting the error, let me show you below my piece of code that I have tried.

Below is my class in which first I am creating objects and then through xstream generating the intermediary XML

InvoiceReferenceNotificationMessage invoiceReferenceNotificationMessage = new InvoiceReferenceNotificationMessage();
        invoiceReferenceNotificationMessage.setInvoiceReference("SM/8315");
        invoiceReferenceNotificationMessage.setABSReference("IRMA5456R157311");
        invoiceReferenceNotificationMessage.setCurrency("GBP");
        invoiceReferenceNotificationMessage.setInvoiceAmount(255646);
        invoiceReferenceNotificationMessage.setPaidAmount(1246565);
        invoiceReferenceNotificationMessage.setBalanceAmount(0);
        invoiceReferenceNotificationMessage.setValueDate(new Date());
        invoiceReferenceNotificationMessage.setRemarks("abc");


        InvoiceReferenceNotificationMessage invoiceReferenceNotificationMessage1 = new InvoiceReferenceNotificationMessage();
        invoiceReferenceNotificationMessage1.setInvoiceReference("SM/1655");
        invoiceReferenceNotificationMessage1.setABSReference("I1575656311");
        invoiceReferenceNotificationMessage1.setCurrency("EUR");
        invoiceReferenceNotificationMessage1.setInvoiceAmount(25655546);
        invoiceReferenceNotificationMessage1.setPaidAmount(1255565645);
        invoiceReferenceNotificationMessage1.setBalanceAmount(0);
        invoiceReferenceNotificationMessage1.setValueDate(new Date());
        invoiceReferenceNotificationMessage1.setRemarks("abERRc");

  Transformer xsltTransformer = null;
        Mail m = new Mail();

        m.setInvoiceReferenceNotificationMessage(invoiceReferenceNotificationMessage);

        XStream xstream = new XStream();
        xstream.alias("brokermail",Mail.class);
        String abc = xstream.toXML(m);
        //System.out.println(abc);

        m.setInvoiceReferenceNotificationMessage(invoiceReferenceNotificationMessage1);
        xstream.alias("brokermail",Mail.class);
        String def = xstream.toXML(m);
        //System.out.println(def);

        String t =abc+def;
        System.out.println(t);

The above code generates the following below XML

 <brokermail>
  <invoiceReferenceNotificationMessage>
    <InvoiceReference>SM/82973409/0315</InvoiceReference>
    <ABSReference>IRMAR43157311</ABSReference>
    <Currency>GBP</Currency>
    <InvoiceAmount>25446.0</InvoiceAmount>
    <PaidAmount>12435.0</PaidAmount>
    <BalanceAmount>0.0</BalanceAmount>
    <ValueDate>2015-05-21 21:58:04.439 IST</ValueDate>
    <Remarks>abc</Remarks>
  </invoiceReferenceNotificationMessage>
</brokermail><brokermail>
  <invoiceReferenceNotificationMessage>
    <InvoiceReference>SM/13435</InvoiceReference>
    <ABSReference>I157343411</ABSReference>
    <Currency>EUR</Currency>
    <InvoiceAmount>2554546.0</InvoiceAmount>
    <PaidAmount>12534545.0</PaidAmount>
    <BalanceAmount>0.0</BalanceAmount>
    <ValueDate>2015-05-21 21:58:04.439 IST</ValueDate>
    <Remarks>abERRc</Remarks>
  </invoiceReferenceNotificationMessage>
</brokermail>

Now I have to do xsl transformation that is by passing the above generated XML and then generating a new XML which I am doing through xalan. Below is what I have tried but my final XML is not generated as I am getting the error please advise how to overcome this, I am using xalan api

System.out.println("******%%%%%%%%%%%%%*************");
        String t =abc+def;
        System.out.println(t);
        Source msgStreamSource = null;

        ByteArrayInputStream byteStream = new ByteArrayInputStream(t.getBytes());
        BufferedInputStream buffMsg = new BufferedInputStream(byteStream);
        msgStreamSource = new StreamSource(buffMsg);

             Transformer xsltTransformer1 = null;

            StringWriter strWriter = new StringWriter();
            // Result stream
            Result xmlOutput = new StreamResult(strWriter);
            // String to hold transformed xml
            String transformedMessage = null;
            TransformerFactory transFact = TransformerFactory.newInstance();
            try
            {
            //*******#########****** error is on this below line ********###########***********
                xsltTransformer1 = transFact.newTransformer(msgStreamSource);
            //*******#########****** error is on this below line ********###########***********
                // perform XSL transformation
                xsltTransformer1.transform(msgStreamSource, xmlOutput);
                // get the transformed xml
                transformedMessage = strWriter.toString();
            }
            finally
            {
                strWriter.close();
            }

            System.out.println(transformedMessage);
        }

the err that i am getting is please advise how to overcome from this

[Fatal Error] :12:15: The markup in the document following the root element must be well-formed.
ERROR:  'The markup in the document following the root element must be well-formed.'
FATAL ERROR:  'Could not compile stylesheet'
Exception in thread "main" javax.xml.transform.TransformerConfigurationException: Could not compile stylesheet
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTemplates(Unknown Source)
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTransformer(Unknown Source)

1条回答
The star\"
2楼-- · 2019-03-07 04:07

You pass the XML (not a valid document, with 2 root elements) to newTransformer, instead of passing the XSLT stylesheet. The compiler does not even complain about the document not to be a valid stylesheet, it complains it is ill-formed XML (an XSLT stylesheet is itself an XML document). You have to read TransformerFactory documentation, I am afraid.

查看更多
登录 后发表回答