java 添加 xml-stylesheet 并获得 standalone = yes

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2651647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 22:12:37  来源:igfitidea点击:

Add xml-stylesheet and get standalone = yes

javaxmlxslt

提问by tumba25

I added the solution to the code below.

我在下面的代码中添加了解决方案。

The code at the bottom is what I have. I removed the creation of all tags.

底部的代码是我所拥有的。我删除了所有标签的创建。

At the top in the xml file I get.<?xml version="1.0" encoding="UTF-8" standalone="no"?>Note that standalone is no, even thou I have it set to yes.

在我得到的 xml 文件的顶部。<?xml version="1.0" encoding="UTF-8" standalone="no"?>请注意,即使我将其设置为是,独立也不是。

The first question: How do I get standalone = yes?

第一个问题:我如何获得独立 = 是?

I would like to add <?xml-stylesheet type="text/xsl" href="my.stylesheet.xsl"?>at line two in the xml file.

我想<?xml-stylesheet type="text/xsl" href="my.stylesheet.xsl"?>在 xml 文件的第二行添加。

Second question: How do I do that?

第二个问题:我该怎么做?

Some useful links? Anything?

一些有用的链接?任何事物?

DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();  
DocumentBuilder docBuilder = dbfac.newDocumentBuilder();  
Document doc = docBuilder.newDocument();
doc.setXmlStandalone(true);
ProcessingInstruction pi = doc.createProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"my.stylesheet.xsl\"");

Element root = doc.createElement("root-element");
doc.appendChild(root);
doc.insertBefore(pi, root);    

<cut>  

TransformerFactory transfac = TransformerFactory.newInstance();
transfac.setAttribute("indent-number", new Integer(2));
Transformer trans = transfac.newTransformer();
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
trans.setOutputProperty(OutputKeys.STANDALONE, "yes");
trans.setOutputProperty(OutputKeys.INDENT, "yes");
trans.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, "name");

FileOutputStream fout = new FileOutputStream(filepath);
BufferedOutputStream bout= new BufferedOutputStream(fout);
trans.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(bout, "utf-8")));

回答by tumba25

I added

我加了

doc.setXmlStandalone(true);
ProcessingInstruction pi = doc.createProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"my.stylesheet.xsl\"");`  

before the cut and

切割前和

doc.insertBefore(pi, root);

right after the root element was appended to the doc.

在根元素被附加到文档之后。

回答by yunus

in my code, I wrote :

在我的代码中,我写道:



DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();
document.setXmlStandalone(true);


TransformerFactory tfactory = TransformerFactory.newInstance();
Transformer transformer = tfactory.newTransformer();
transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");


output:

输出:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>