如何抑制java.xml转换器添加到导入的外部系统实体的xml:base属性?
这必须有xslt或xalan属性,但我找不到它 假设我有一个声明外部系统实体的xml文件:如何抑制java.xml转换器添加到导入的外部系统实体的xml:base属性?,java,xml,xslt,dom,xml-parsing,Java,Xml,Xslt,Dom,Xml Parsing,这必须有xslt或xalan属性,但我找不到它 假设我有一个声明外部系统实体的xml文件: <?xml version="1.0" standalone="yes" ?> <!DOCTYPE stuff [ <!ENTITY imported_fragment SYSTEM "fragment.xml"> ]> <stuff> <innerstuff name="a"> <l
<?xml version="1.0" standalone="yes" ?>
<!DOCTYPE stuff [
<!ENTITY imported_fragment SYSTEM "fragment.xml">
]>
<stuff>
<innerstuff name="a">
<lala>Hello!</lala>
</innerstuff>
&imported_fragment;
</stuff>
编辑:乔皮的答案奏效了。下面是合并了他的XSLT的java代码
try {
Transformer tr = TransformerFactory.newInstance().newTransformer();
DOMResult domResult = new DOMResult();
tr.transform(new DOMSource(dom), domResult);
StreamSource source = new StreamSource(getClass().getResourceAsStream("removexmlbase.xslt"));
Transformer tr2 = TransformerFactory.newInstance().newTransformer(source);
tr2.setOutputProperty(OutputKeys.INDENT, "yes");
tr2.setOutputProperty(OutputKeys.METHOD, "xml");
tr2.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
tr2.setOutputProperty(OutputKeys.STANDALONE, "yes");
//tr2.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
File outputFile = new File(fOutputDirectory, fXMLFile.getName());
getLog().info("Writing xml output to: " + outputFile);
// send DOM to file
tr2.transform(new DOMSource(domResult.getNode()), new StreamResult(new FileOutputStream(outputFile)));
}
用于抑制xml:base属性的XSLT 1.0模板:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- Suppress xml:base attributes -->
<xsl:template match="@xml:base"/>
</xsl:transform>
谢谢,这很有效,我能够将它合并到我的java中。请注意,这意味着xml:base不是由转换器添加的,而是由解析器添加的。
Document dom;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
try {
db = dbf.newDocumentBuilder();
sLogger.info("Parsing file: " + fInputFile);
dom = db.parse(fInputFile);
} catch (SAXException | IOException | ParserConfigurationException e) {
//...
}
try {
Transformer tr = TransformerFactory.newInstance().newTransformer();
tr.setOutputProperty(OutputKeys.INDENT, "yes");
tr.setOutputProperty(OutputKeys.METHOD, "xml");
tr.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
tr.setOutputProperty(OutputKeys.STANDALONE, "yes");
//tr.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
File outputFile = new File(fOutputDirectory, fInputFile.getName());
sLogger..info("Writing xml output to: " + outputFile);
tr.transform(new DOMSource(dom),
new StreamResult(new FileOutputStream(outputFile)));
} catch (IOException | TransformerException e) {
//...
}
try {
Transformer tr = TransformerFactory.newInstance().newTransformer();
DOMResult domResult = new DOMResult();
tr.transform(new DOMSource(dom), domResult);
StreamSource source = new StreamSource(getClass().getResourceAsStream("removexmlbase.xslt"));
Transformer tr2 = TransformerFactory.newInstance().newTransformer(source);
tr2.setOutputProperty(OutputKeys.INDENT, "yes");
tr2.setOutputProperty(OutputKeys.METHOD, "xml");
tr2.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
tr2.setOutputProperty(OutputKeys.STANDALONE, "yes");
//tr2.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
File outputFile = new File(fOutputDirectory, fXMLFile.getName());
getLog().info("Writing xml output to: " + outputFile);
// send DOM to file
tr2.transform(new DOMSource(domResult.getNode()), new StreamResult(new FileOutputStream(outputFile)));
}
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- Suppress xml:base attributes -->
<xsl:template match="@xml:base"/>
</xsl:transform>