开发者

xmlns and xslt-transformation in java

i'm transforming a xml file A of xmlns="ans" via xslt 2.0 saxon in java with the javax.xml.Transformer into a xml file B of xmlns="bns".

When the transformation has performed the output xml shows only values of the xml file A and an error occurs:

[Fatal Error] :3:4: Con开发者_如何学运维tent is not allowed in prolog.

When I delete the xmlns="ans" of file A, the result xml file b is correct and no errors while transforming occurs. For my use case the xml messages which will be transformed will contain a namespace. Any ideas how to solve that without deleting the namespace declaration of the input file?

Appendix:

My java code:

System.setProperty("javax.xml.transform.TransformerFactory",  
"net.sf.saxon.TransformerFactoryImpl");  

TransformerFactory transFactory = TransformerFactory.newInstance();
StreamSource stylesource = new StreamSource("transformation.xsl");
Templates template = transFactory.newTemplates(stylesource);
Transformer transformer = template.newTransformer();
StreamSource source = new StreamSource(new File("filea.xml"));

StreamResult result = new StreamResult(new StringWriter());
transformer.transform(source, result);
//result will be written to fileb.xml

My filea.xml

<?xml version="1.0" encoding="UTF-8"?>
<message xmlns="ans">...
</message>

My transformation.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns="bns">
<xsl:output method="xml" indent="yes" />
...
</xsl>


Well unless you show us the stylesheet code processing elements we can only guess. I suspect putting the attribute xpath-default-namespace="ans" on your xsl:stylesheet element might fix the problem. If not then please post enough details of your code allowing us to reproduce the problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜