通过java在xslt转换中获取错误

时间:2015-05-21 16:49:36

标签: java xml xslt xstream xalan

我正在尝试一个xsl转换,现在由一个java对象生成一个中间XML,并且再次传递XML以最终生成一个新的XML,而在xsl转换中我收到错误,让我显示你在我试过的代码之下。

下面是我的类,首先我创建对象,然后通过xstream生成中间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);

上面的代码生成以下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>

现在我必须通过传递上面生成的XML然后生成一个我正在通过xalan进行的新XML来进行xsl转换。下面 是我试过但我的最终XML没有生成,因为我收到错误请告知如何克服这个,我使用的是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);
        }

我得到的错误请告知如何克服这个

[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 个答案:

答案 0 :(得分:0)

您将XML(不是具有2个根元素的有效文档)传递给newTransformer,而不是传递XSLT样式表。编译器甚至没有抱怨文档不是有效的样式表,它抱怨它是格式错误的XML(XSLT样式表本身就是一个XML文档)。我很害怕,你必须阅读TransformerFactory documentation