简体   繁体   English

XSL转换不会在元素之前写入名称空间

[英]XSL transformation doesn't write namespace before elements

I have to update the file content.xml in an odt file. 我必须更新的ODT文件的文件的content.xml。 I want to obtain the same file content without spaces from elements and without line break. 我想获得相同的文件内容,而元素之间没有空格,也没有换行符。

I tried to use an xsl transormation with the Transformer object in Java, and it partly works. 我试图在Java中将xsl transormation与Transformer对象一起使用,并且部分起作用。 For example, having a simple odt, which the content.xml is the following: 例如,有一个简单的odt,content.xml如下:

<?xml version="1.0" encoding="utf-8" ?>
<office:document-content xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" xmlns:math="http://www.w3.org/1998/Math/MathML" xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" xmlns:ooo="http://openoffice.org/2004/office" xmlns:ooow="http://openoffice.org/2004/writer" xmlns:oooc="http://openoffice.org/2004/calc" xmlns:dom="http://www.w3.org/2001/xml-events" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" office:version="1.2">
    <office:font-face-decls>
        <style:font-face style:name="Courier New" style:font-family-generic="modern" style:font-pitch="fixed" svg:font-family="'Courier New'" />
    </office:font-face-decls>
    <office:automatic-styles>

    </office:automatic-styles>
    <office:body>
        <office:text>
            <text:p text:style-name="Title">TODO supply a title</text:p>
            <text:p text:style-name="Text_20_body">TODO write content</text:p>
            <text:h text:style-name="Heading_20_1" text:outline-level="1">My First Heading</text:h>
            <text:p text:style-name="First_20_paragraph">My first paragraph.</text:p>
        </office:text>
    </office:body>
</office:document-content>

I wrote this xsl file to remove all line breaks, blank spaces between elements, so I want obtain the same XML content serialize in one line. 我写了这个xsl文件来删除所有换行符,元素之间的空格,所以我想在一行中获得相同的XML内容序列化。

<?xml version="1.0" encoding="ISO-8859-15" ?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xalan="http://xml.apache.org/xslt"
                xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" 
                xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" 
                xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" 
                xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" 
                xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0"
                xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" 
                xmlns:xlink="http://www.w3.org/1999/xlink" 
                xmlns:dc="http://purl.org/dc/elements/1.1/" 
                xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" 
                xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" 
                xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" 
                xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" 
                xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" 
                xmlns:math="http://www.w3.org/1998/Math/MathML" 
                xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" 
                xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" 
                xmlns:ooo="http://openoffice.org/2004/office" 
                xmlns:ooow="http://openoffice.org/2004/writer" 
                xmlns:oooc="http://openoffice.org/2004/calc" 
                xmlns:dom="http://www.w3.org/2001/xml-events"
                xmlns:xforms="http://www.w3.org/2002/xforms" 
                xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xmlns="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
                extension-element-prefixes="office style text"
>
    <xsl:output method="xml" encoding="ISO-8859-15" indent="no"/>
    <xsl:strip-space elements="*" />


    <xsl:template match="@*|node()|comment()|processing-instruction()|text()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()|comment()|processing-instruction()|text()" />
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

I used indent="no" and strip-space elements="*" to have no identation, but now my problem is the namespaces. 我使用了indent =“ no”和strip-space elements =“ *”没有标识,但是现在我的问题是名称空间。 The XML which I obtain using this xsl is the following: 我使用此xsl获得的XML如下:

<?xml version="1.0" encoding="ISO-8859-15"?><document-content xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dom="http://www.w3.org/2001/xml-events" xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" xmlns:math="http://www.w3.org/1998/Math/MathML" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:ooo="http://openoffice.org/2004/office" xmlns:oooc="http://openoffice.org/2004/calc" xmlns:ooow="http://openoffice.org/2004/writer" xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" office:version="1.2"><font-face-decls><font-face style:font-family-generic="modern" style:font-pitch="fixed" style:name="Courier New" svg:font-family="'Courier New'"/></font-face-decls><automatic-styles/><body><text><p text:style-name="Title">TODO supply a title</p><p text:style-name="Text_20_body">TODO write content</p><h text:outline-level="1" text:style-name="Heading_20_1">My First Heading</h><p text:style-name="First_20_paragraph">My first paragraph.</p></text></body></document-content>

It's in one line, as I want, but the elements has no namespace prefix. 正如我想要的那样,它在一行中,但是元素没有名称空间前缀。 Note that it doesn't happen with attributes, which have correct namespaces. 请注意,具有正确名称空间的属性不会发生这种情况。

In the xsl I specified to match node, comment and attributes, the namespaces in the <xsl:stylesheet> element and the extension-element-prefixes passing the list of allowed namespaces. 在XSL我指定匹配节点,注释和属性,在名称空间<xsl:stylesheet>元件和通过允许命名空间列表中的伸长元件前缀。 If I remove the extension-element-prefixes nothing change. 如果删除了extension-element-prefix,则没有任何变化。

The Java code which use the Transformer object is the following: 使用Transformer对象的Java代码如下:

 public void serializeXML(String filePath, String destinationPath) throws Exception {
    File xmlFile = new File(filePath);
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(xmlFile);
    doc.normalizeDocument();

    DOMSource domSource = new DOMSource(doc);
    TransformerFactory transformerFactory = TransformerFactory.newInstance();

    InputStream is = XMLSerializer.class.getClassLoader().getResourceAsStream("identer.xsl");

    Transformer transformer = transformerFactory.newTransformer(new StreamSource(is));

    StringWriter sw = new StringWriter();
    StreamResult sr = new StreamResult(sw);
    transformer.transform(domSource, sr);

    Result result = new StreamResult(new FileOutputStream(new File(destinationPath)));

    transformer.transform(domSource, result);

    LOGGER.info(sw.toString());
}

I declare the Transformer object, passing to it the inputStream of the xsl file. 我声明了Transformer对象,并将xsl文件的inputStream传递给它。 I don't set any properties because I declare all (in my opinion) is necessary in the xsl file, and after that I tranform the document, saving the result in a file and printing it with a logger. 因为我宣布所有(在我看来),我不设置任何属性是必要的XSL文件,之后我等转换的文件,并保存结果在一个文件中,并用记录仪打印。

Can someone help me? 有人能帮我吗?

Thank you. 谢谢。

I solved, thanks to the users who answer to my question. 感谢用户回答了我的问题,我解决了。

I revised my code, so the xsl now is: 我修改了代码,所以xsl现在是:

<xsl:output method="xml" encoding="ISO-8859-15" indent="no"/>
<xsl:strip-space elements="*" />

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

And then I simplified the Java code which use the Transformer: 然后,我简化了使用Transformer的Java代码:

 public void serializeXML(String filePath, String destinationPath) throws Exception {

        TransformerFactory factory = TransformerFactory.newInstance();
        InputStream is = XMLSerializer.class.getClassLoader().getResourceAsStream("identer.xsl");
        Source xslt = new StreamSource(is);
        Transformer transformer = factory.newTransformer(xslt);

        Source text = new StreamSource(new File(filePath));
        transformer.transform(text, new StreamResult(new File(destinationPath)));

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM