1

これには xslt または xalan プロパティが必要ですが、見つかりません。

外部システム エンティティを宣言する xml ファイルがあるとします。

<?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>

フラグメントは次のようになります。

<innerstuff name="b">
    <lala>Bye!</lala>
</innerstuff>

これを javax.xml クラスで読み込んで書き戻すと、次のようになります。

 <?xml version="1.0" standalone="yes" ?>
<stuff>
    <innerstuff name="a">
        <lala>Hello!</lala>
    </innerstuff>

    <innerstuff name="b" xml:base="file:/full/path/to/fragment.xml">
        <lala>Bye!</lala>
    </innerstuff>
</stuff>

xml:base追加された属性に注意してください。これを抑えるにはどうすればよいですか?

xml ファイルの読み取りと書き込みに使用するコードは次のとおりです。

    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) {
        //...
    }

編集:ジョーピーの答えはうまくいきました。彼の 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)));

    }
4

1 に答える 1

2

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>

実施例

于 2013-03-28T06:31:47.393 に答える