XML 意外元素,父元素的内容必须匹配类型

huangapple go评论98阅读模式
英文:

XML Unexpected element, content of the parent element must match type

问题

I'm new to XML (and new to stackoverflow) and I'm trying to write an XML file & an accompanying DTD file containing some basic information about geographic subdivisions (states and provinces). The first of my elements is working fine, but oXygen is giving me the following error at the start of the second element:

Unexpected element "Subdivision". The content of the parent element type must match "(Subdivision)".

I'm not sure what's causing the first one to work and the second one to error, I'm probably missing something but I can't figure out what it is.

Here's the XML code in question, somewhat trimmed:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Subdivisions SYSTEM "Subdivision.dtd">

<Subdivisions>
	<Subdivision subdivisionName = "Nebraska">
		<official_name>State of Nebraska</official_name>
		<country>United States</country>
		<iso_3166_code>US-NE</iso_3166_code>
		<regional_capital>Lincoln</regional_capital>
		<population>1961504</population>
		<land_area_miles>77358</land_area_miles>
	</Subdivision>
	
	<Subdivision subdivisionName = "Southwest Finland"> <!-- the error is on this line-->
		<official_name>Southwest Finland</official_name>
		<country>Finland</country>
		<iso_3166_code>FI-19</iso_3166_code>
		<regional_capital>Turku</regional_capital>
		<population>481403</population>
		<land_area_miles>4212</land_area_miles>
	</Subdivision>
	
	<!-- trimmed the rest -->

</Subdivisions>

And the DTD that goes with it:

<?xml encoding="UTF-8">

<!ELEMENT Subdivisions (Subdivision)>

<!ELEMENT Subdivision (official_name, country, iso_3166_code, regional_capital, population, land_area_miles)>

<!ELEMENT official_name (#PCDATA)>
<!ELEMENT country (#PCDATA)>
<!ELEMENT iso_3166_code (#PCDATA)>
<!ELEMENT regional_capital (#PCDATA)>
<!ELEMENT population (#PCDATA)>
<!ELEMENT land_area_miles (#PCDATA)>

<!ATTLIST Subdivision subdivisionName CDATA #REQUIRED>

This is my first time doing XML and my first time asking a StackOverflow question so I apologize if I did it wrong or provided too little (or too much?) context/code.

Any help is greatly appreciated!

英文:

I'm new to XML (and new to stackoverflow) and I'm trying to write an XML file & an accompanying DTD file containing some basic information about geographic subdivisions (states and provinces). The first of my &lt;Subdivision&gt; elements is working fine, but oXygen is giving me the following error at the start of the second &lt;Subdivision&gt; element:

> Unexpected element "Subdivision". The content of the parent element type must match "(Subdivision)".

I'm not sure what's causing the first one to work and the second one to error, I'm probably missing something but I can't figure out what it is.

Here's the XML code in question, somewhat trimmed:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;!DOCTYPE Subdivisions SYSTEM &quot;Subdivision.dtd&quot;&gt;

&lt;Subdivisions&gt;
	&lt;Subdivision subdivisionName = &quot;Nebraska&quot;&gt;
		&lt;official_name&gt;State of Nebraska&lt;/official_name&gt;
		&lt;country&gt;United States&lt;/country&gt;
		&lt;iso_3166_code&gt;US-NE&lt;/iso_3166_code&gt;
		&lt;regional_capital&gt;Lincoln&lt;/regional_capital&gt;
		&lt;population&gt;1961504&lt;/population&gt;
		&lt;land_area_miles&gt;77358&lt;/land_area_miles&gt;
	&lt;/Subdivision&gt;
	
	&lt;Subdivision subdivisionName = &quot;Southwest Finland&quot;&gt; &lt;!-- the error is on this line--&gt;
		&lt;official_name&gt;Southwest Finland&lt;/official_name&gt;
		&lt;country&gt;Finland&lt;/country&gt;
		&lt;iso_3166_code&gt;FI-19&lt;/iso_3166_code&gt;
		&lt;regional_capital&gt;Turku&lt;/regional_capital&gt;
		&lt;population&gt;481403&lt;/population&gt;
		&lt;land_area_miles&gt;4212&lt;/land_area_miles&gt;
	&lt;/Subdivision&gt;
	
	&lt;!-- trimmed the rest --&gt;

&lt;/Subdivisions&gt;

And the DTD that goes with it:

&lt;?xml encoding=&quot;UTF-8&quot;?&gt;

&lt;!ELEMENT Subdivisions (Subdivision)&gt;

&lt;!ELEMENT Subdivision (official_name, country, iso_3166_code, regional_capital, population, land_area_miles)&gt;

&lt;!ELEMENT official_name (#PCDATA)&gt;
&lt;!ELEMENT country (#PCDATA)&gt;
&lt;!ELEMENT iso_3166_code (#PCDATA)&gt;
&lt;!ELEMENT regional_capital (#PCDATA)&gt;
&lt;!ELEMENT population (#PCDATA)&gt;
&lt;!ELEMENT land_area_miles (#PCDATA)&gt;

&lt;!ATTLIST Subdivision subdivisionName CDATA #REQUIRED&gt; 

This is my first time doing XML and my first time asking a StackOverflow question so I apologize if I did it wrong or provided too little (or too much?) context/code.

Any help is greatly appreciated!

答案1

得分: 2

朋友告诉我一个解决方案,解决了我的问题,我不知道在 DTD 文件中可以使用 + 符号表示一个元素存在一个或多个。

将这行代码更改为:

<!ELEMENT Subdivisions (Subdivision+)>
英文:

My friend told me a solution that solved my problem, I didn't know that you can use the + symbol in the DTD file to indicate that there are one or more of an element.

Changed the line

&lt;!ELEMENT Subdivisions (Subdivision)&gt;

to this:

&lt;!ELEMENT Subdivisions (Subdivision+)&gt;

huangapple
  • 本文由 发表于 2023年2月19日 13:23:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75498173.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定