How to use DOCTYPE in Spring XML file

2019-06-10 03:14发布

问题:

Most of the time we do not declare DOCTYPE in Spring. But I want to declare a DOCTYPE in my XML context file so that I can use ENTITY in my xml file.

For example:

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
                  "http://www.springframework.org/dtd/spring-beans-2.0.dtd"
    [<!ENTITY % crmHome SYSTEM "crm-home.dtd"> %crmHome;]
>

This gives many errors like...

 - Attribute "xmlns" must be declared for element type "beans".

 - Attribute "xmlns:xsi" must be declared for element type "beans".

    etc.....

What is the way to achieve this?

回答1:

This works for me. Using Spring Framework V.4.2.1

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">



回答2:

If you're using schema validation anyway then you could just define the internal DTD subset sufficient to declare the parameter entity and not refer to the http://www.springframework.org/dtd/spring-beans-2.0.dtd:

<!DOCTYPE beans [
  <!ENTITY % crmHome SYSTEM "crm-home.dtd">
   %crmHome;
]>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
             http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans.xsd">