xmltask confused about dtd

2019-04-09 05:05发布

问题:

I'm trying to use xmltask for ant to modify a file in a subdirectory:

project/path/to/file.xml

The file refers to a DTD like this:

<!DOCTYPE data SYSTEM "mydtd.dtd">

I don't have the flexibility to change these documents.

This DTD is stored in the same subdirectory, which has always worked fine:

project/path/to/mydtd.dtd

Unfortunately, xmltask is trying to locate the dtd in my project's top-level directory, which is where my build file is located, and where I run from:

[xmltask] java.io.FileNotFoundException: /home/me/project/mydtd.dtd (The system cannot find the file specified)

I see in the xmltask documentation that I can correct this with an xmlcatalog element to tell it where to look up the file. But I need to use a dtd element, and I can only find examples for this element, not documentation; the examples show only a publicId, and if I understand XML correctly this document does not have one. I shouldn't need to specify this, anyway, right, since my document already says my DTD is stored locally and shows right where it is?

Why isn't xmltask finding the DTD correctly? What's the best way to correct or work around this situation?

回答1:

An XML Catalog is the way to go here, it just needs a bit more perseverance.

As you correctly pointed out, the standard Ant <XmlCatalog> type only allows you to specify public DTD references when using the inline syntax, which is of no use to you. However, <XmlCatalog> also lets you specify a standard OASIS-syntax catalog, which is far richer, including resolving SYSTEM DTD references.

An OASIS catalog (full spec here) looks like this:

<catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">      
  <system systemId="mydtd.dtd" uri="project/path/to/mydtd.dtd"/>
</catalog>

You can then reference this catalog from the <XmlCatalog>:

<xmlcatalog refid="commonDTDs"/>
  <catalogpath>
    <pathelement location="path/to/oasis.catalog"/>
  </catalogpath>
</xmlcatalog>

And that's that. It's a good idea to build up a reusable OASIS catalog file, and refer to it from various XML-related Ant tasks, all of which can use <XmlCatalog>.



回答2:

As an alternative, it looks like I can skip the whole validation by creating a blank file with the same name as the DVD file, and then deleting the file when I am done. Odds are I am going to go that route instead of using the catalog.



回答3:

xmltask isn't finding it because it is looking in the current working directory. Ant allows you to specify a base directory using the basedir attribute of the <target> element. So I suggest you try this:

<target basedir="path/to" ...>
  <xmltask...
</target>

It strikes me that it is not the XML/DTD that you really have the problem with, but getting xmltask to interact with the two of them as you want.

If that fails, you could use the Ant Copy task to copy the XML and DTD to the root folder before processing with xmltask, then copying back again.



回答4:

Have you tried:

<!DOCTYPE data SYSTEM "./path/to/mydtd.dtd">

? Or an absolute path?

Also, you can find <dtd> description here.



回答5:

I had a similar problem where an XML file had a doctype with SYSTEM reference that could not be changed.

<!DOCTYPE opencms SYSTEM "http://www.opencms.org/dtd/6.0/opencms-modules.dtd">

I first went down the road and created a catalog file with the OASIS catalog as described above, but to be able to use external catalogs I had to include the Apache Commons Resolver 1.1 (resolver.jar) in the Ant classpath (see http://ant.apache.org/manual/Types/xmlcatalog.html).

Because I had multiple machines on which this build was supposed to run this seemed overkill, especially since xmltask worked fine if I just removed the doctype definition. I wasn't allowed to remove it permanently because the doctype was needed elsewhere.

Ultimately I used this workaround: I commented out the doctype definition using Ant's replace task, ran the xmltask, and then put the doctype back into the file.

<replace file="myxmlfile.xml">
    <replacetoken>&lt;!DOCTYPE opencms SYSTEM "http://www.opencms.org/dtd/6.0/opencms-modules.dtd"&gt;</replacetoken>
    <replacevalue>&lt;!-- !DOCTYPE opencms SYSTEM "http://www.opencms.org/dtd/6.0/opencms-modules.dtd" --&gt;</replacevalue>
</replace>

<xmltask .../>

<replace file="${local.opencms.webapp.webinf}/config/opencms-modules.xml">
    <replacetoken>&lt;!-- !DOCTYPE opencms SYSTEM "http://www.opencms.org/dtd/6.0/opencms-modules.dtd" --&gt;</replacetoken>
    <replacevalue>&lt;!DOCTYPE opencms SYSTEM "http://www.opencms.org/dtd/6.0/opencms-modules.dtd"&gt;</replacevalue>
</replace>


标签: ant dtd