通过java在xslt转换中出错 [英] getting error in xslt transformation through java

查看:22
本文介绍了通过java在xslt转换中出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 java 对象进行 xsl 转换,我正在生成中间 XML,并且现在再次传递该 XML 以生成新的 XML,而在 xsl 转换中我收到错误,让我展示一下你在我试过的一段代码下面.

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.

下面是我的类,首先我在其中创建对象,然后通过 xstream 生成中间 XML

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

以上代码生成以下 XML

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>

现在我必须进行 xsl 转换,即通过传递上面生成的 XML,然后生成我正在通过 xalan 执行的新 XML.以下是我尝试过的,但我的最终 XML 没有生成,因为我收到错误请告知如何克服这个问题,我正在使用 xalan api

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)

推荐答案

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

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.

这篇关于通过java在xslt转换中出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆