XML和DTD的错误不会消失

时间:2018-08-05 22:33:48

标签: xml dtd xml-validation xml-dtd

我有一个快速的问题,我到底在做什么错。我有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) >

1 个答案:

答案 0 :(得分:0)

您没有包含所得到的错误,因此我将假定它是以下内容:

  

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

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

因此,您的声明要说的是tableofshows必须具有一个或多个tableshows元素,然后是一个或多个sation元素,然后是一个或多个name元素。

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

这不是XML中的内容。在XML中,您恰好有一个,元素,然后是正好一个sation元素,然后是正好一个name元素,并且该组(在括号内)发生了一次或多次。 >

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

timeaired

<!ELEMENT tableofshows (sation, name, timeaired)+ > 元素相同。您需要将声明更改为一个showdetails元素,然后是一个nameofshow元素,然后是该组,而不是一个或多个information元素,然后是一个或多个nameofshow元素可能发生一次或多次...

information

那应该可以解决您的验证错误。这是否是一个好的DTD设计,取决于您要完成的工作,在这里无法回答。