【问题标题】:XML and DTD's error that will not go awayXML 和 DTD 的错误不会消失
【发布时间】:2018-08-06 14:45:52
【问题描述】:

我有一个简短的问题,我到底在做什么错。我有我的 XML 和 DTD,无论我做什么,我都会遇到同样的错误。请帮忙。这是针对我已经多次更改此代码的课程,所以如果有任何遗漏,请随时告诉我。

这是我的 XML、sation 和 nameofshows,是我收到错误的地方...

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

<tvlisting>
    <changelocation>
        <provider>Cox, ComCast, Charter, Dishnetwork, DirectTV</provider>
    </changelocation>
    <date>July 1, 2018</date>
    <time>2:55pm</time>
    <searchlisting>
        <channelsearch>CBS, ABC, NBC, FOX, PBS, CW, MNT, ION, TELMUN, UNI, AandE,AMC,ANIMAL,
            BBC,ETC.</channelsearch>
    </searchlisting>
    <tabs>pick by genre</tabs>
    <legend>name and time show airs</legend>
    <tableofshows>
        <sation>AMC</sation>
        <name>Limitless</name>
        <timeaired>1:00pm-3:00pm</timeaired>
        <sation>ANIMAL</sation>
        <name>Extinct or Alive</name>
        <timeaired>1:00pm-3:00pm</timeaired>
        <sation>BBC</sation>
        <name>Black Hawk Down</name>
        <timeaired>1:00pm-4:00pm</timeaired>
        <sation>BET</sation>
        <name>Tyler Perry's Good Deeds</name>
        <timeaired>1:00pm-3:00pm</timeaired>
        <sation>BRAVO</sation>
        <name>Kandi Koated Nighs</name>
        <timeaired>2:30-3:00pm</timeaired>
    </tableofshows>
    <!-- sorry, the hour changed and some of the shows listed above are no longer on. -->
    <showdetails>
        <nameofshow>Black Hawk Down</nameofshow>
        <information>An American mission to capture the top aides of a Somali warlord goes awry when
            enemy forces shoot down two Black Hawk helicopters and surround the soldiers on the
            ground. The ensuing firefight is a merciless 15-hour ordeal and the longest ground
            battle involving American soldiers since the Vietnam War.</information>
        <nameofshow>Tyler Perry's Good Deeds</nameofshow>
        <information>Upstanding and engaged businessman Wesley (Tyler Perry) finds his upcoming
            marriage thrown into question when his connection with a down-on-her-luck cleaning woman
            turns unexpectedly romantic. </information>
    </showdetails>
    <filterforhd>
        <options>1080p,720p,480p,standard</options>
    </filterforhd>
</tvlisting>

这是我制作的 DTD,我的老师说这看起来完全没问题,但仍然感觉这缺少可以使它成为更好代码的东西。

 <?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT tvlisting (changelocation+,date?,time?,searchlisting+,tabs*,legend?,tableofshows+,showdetails,filterforhd+) >
<!ELEMENT changelocation (provider?) >
<!ELEMENT provider (#PCDATA) >
<!ELEMENT date (#PCDATA) >
<!ELEMENT time (#PCDATA) >
<!ELEMENT searchlisting (channelsearch+) >
<!ELEMENT channelsearch (#PCDATA) >
<!ELEMENT tabs (#PCDATA) >
<!ELEMENT legend (#PCDATA) >
<!ELEMENT tableofshows (sation+, name+,timeaired+) >
<!ELEMENT sation (#PCDATA) >
<!ELEMENT name (#PCDATA) >
<!ELEMENT timeaired (#PCDATA) >
<!ELEMENT showdetails (nameofshow+, information+) >
<!ELEMENT nameofshow (#PCDATA) >
<!ELEMENT information (#PCDATA) >
<!ELEMENT filterforhd (options+) >
<!ELEMENT options (#PCDATA) >

【问题讨论】:

    标签: xml xml-validation dtd


    【解决方案1】:

    您没有包含您遇到的错误,所以我假设它类似于以下内容:

    意外元素“sation”。父元素类型的内容 必须匹配“(sation+, name+, timeaired+)”。

    这告诉您sation 的父级,即tableofshows,必须匹配(sation+,name+,timeaired+)。那是因为这是您在 DTD 中为 tableofshows 声明的内容。

    所以你的声明是说tableshows必须有一个或多个sation元素,后跟一个或多个name元素,后跟一个或多个timeaired元素。

    + 字符表示“一个或多个”,, 字符表示“跟随”。)

    这不是您的 XML 中的内容。在您的 XML 中,您只有一个 sation 元素,后跟一个 name 元素,后跟一个 timeaired 元素,并且该组(在括号内)发生一次或多次。

    要使声明与您的 XML 匹配,您必须将其更改为:

    <!ELEMENT tableofshows (sation, name, timeaired)+ >
    

    showdetails 元素也是如此。您需要将声明更改为一个 nameofshow 元素后跟一个 information 元素,而不是一个或多个 nameofshow 元素后跟一个或多个 information 元素,并且该组可以出现一次或多次。 .

    <!ELEMENT showdetails (nameofshow, information)+ >
    

    这应该可以解决您的验证错误。这是否是一个好的 DTD 设计取决于您要完成的任务,并且过于宽泛,无法在这里回答。

    【讨论】:

    • 非常感谢您的帮助。另外,对不起,如果这有点令人困惑,我是新手,在线提问。但再次感谢您的帮助:)。
    • @BrandiBlankenfeld - 欢迎来到 StackOverflow!我很高兴能帮上忙。没必要道歉。请考虑通过单击旁边的复选标记来接受此答案(有关详细信息,请参阅stackoverflow.com/help/someone-answers)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-11
    相关资源
    最近更新 更多