【问题标题】:Add xml-stylesheet and get standalone = yes添加 xml-stylesheet 并获得独立 = 是
【发布时间】:2011-02-08 17:56:48
【问题描述】:

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

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

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

第一个问题:如何获得standalone = yes?

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

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

一些有用的链接?有什么事吗?

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

【问题讨论】:

    标签: java xml xslt


    【解决方案1】:

    在我的代码中,我写道:


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

    输出:

    &lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;

    【讨论】:

      【解决方案2】:

      我加了

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

      剪辑前

      doc.insertBefore(pi, root);
      

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

      【讨论】:

        猜你喜欢
        • 2013-08-12
        • 2012-09-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-27
        • 1970-01-01
        • 2015-08-05
        相关资源
        最近更新 更多