如何根据 dtd 验证 xml

发布于 2024-10-14 12:47:18 字数 8146 浏览 3 评论 0原文

我有一个 XML 文档:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE ONIXMessage SYSTEM "http://www.editeur.org/onix/2.1/reference/onix-international.dtd">
<ONIXMessage>
  <Header>
    <FromCompany>Companyy</FromCompany>
    <SentDate>20101213</SentDate>
  </Header>
  <Product>
    <RecordReference>153640</RecordReference>
    <NotificationType>03</NotificationType>
    <ProductIdentifier>
      <ProductIDType>03</ProductIDType>
      <IDValue>9785714009341</IDValue>
    </ProductIdentifier>
    <ProductForm>AC</ProductForm>
    <NumberOfPieces>1</NumberOfPieces>
    <ProductClassification>
        <ProductClassificationType>02</ProductClassificationType>
        <ProductClassificationCode>55101514</ProductClassificationCode>
    </ProductClassification>
    <Title>
      <TitleType>01</TitleType>
      <TitleText>Jazz Piano. Volume 3.</TitleText>
    </Title>
    <Contributor>
      <SequenceNumber>1</SequenceNumber>
      <ContributorRole>A01</ContributorRole>
      <PersonName>Waller Fats</PersonName>
      <PersonNameInverted>Fats, Waller</PersonNameInverted>
      <TitlesBeforeNames> </TitlesBeforeNames>
      <NamesBeforeKey>Waller</NamesBeforeKey>
      <PrefixToKey> </PrefixToKey>
      <KeyNames>Fats</KeyNames>
      <SuffixToKey> </SuffixToKey>
    </Contributor>
    <Contributor>
      <SequenceNumber>2</SequenceNumber>
      <ContributorRole>A01</ContributorRole>
      <PersonName>Peterson Oscar</PersonName>
      <PersonNameInverted>Oscar, Peterson</PersonNameInverted>
      <TitlesBeforeNames> </TitlesBeforeNames>
      <NamesBeforeKey>Peterson</NamesBeforeKey>
      <PrefixToKey> </PrefixToKey>
      <KeyNames>Oscar</KeyNames>
      <SuffixToKey> </SuffixToKey>
    </Contributor>
    <Contributor>
      <SequenceNumber>3</SequenceNumber>
      <ContributorRole>A01</ContributorRole>
      <PersonName>Kenton Stan</PersonName>
      <PersonNameInverted>Stan, Kenton</PersonNameInverted>
      <TitlesBeforeNames> </TitlesBeforeNames>
      <NamesBeforeKey>Kenton</NamesBeforeKey>
      <PrefixToKey> </PrefixToKey>
      <KeyNames>Stan</KeyNames>
      <SuffixToKey> </SuffixToKey>
    </Contributor>
    <Contributor>
      <SequenceNumber>4</SequenceNumber>
      <ContributorRole>A01</ContributorRole>
      <PersonName>Evans Bill</PersonName>
      <PersonNameInverted>Bill, Evans</PersonNameInverted>
      <TitlesBeforeNames> </TitlesBeforeNames>
      <NamesBeforeKey>Evans</NamesBeforeKey>
      <PrefixToKey> </PrefixToKey>
      <KeyNames>Bill</KeyNames>
      <SuffixToKey> </SuffixToKey>
    </Contributor>
    <Contributor>
      <SequenceNumber>5</SequenceNumber>
      <ContributorRole>A01</ContributorRole>
      <PersonName>Shearing George</PersonName>
      <PersonNameInverted>George, Shearing</PersonNameInverted>
      <TitlesBeforeNames> </TitlesBeforeNames>
      <NamesBeforeKey>Shearing</NamesBeforeKey>
      <PrefixToKey> </PrefixToKey>
      <KeyNames>George</KeyNames>
      <SuffixToKey> </SuffixToKey>
    </Contributor>
    <Contributor>
      <SequenceNumber>6</SequenceNumber>
      <ContributorRole>A01</ContributorRole>
      <PersonName>D. Ellington</PersonName>
      <PersonNameInverted>Ellington, D.</PersonNameInverted>
      <TitlesBeforeNames> </TitlesBeforeNames>
      <NamesBeforeKey>D.</NamesBeforeKey>
      <PrefixToKey> </PrefixToKey>
      <KeyNames>Ellington</KeyNames>
      <SuffixToKey> </SuffixToKey>
    </Contributor>
    <Contributor>
      <SequenceNumber>7</SequenceNumber>
      <ContributorRole>A01</ContributorRole>
      <PersonName>Corea Chic</PersonName>
      <PersonNameInverted>Chic, Corea</PersonNameInverted>
      <TitlesBeforeNames> </TitlesBeforeNames>
      <NamesBeforeKey>Corea</NamesBeforeKey>
      <PrefixToKey> </PrefixToKey>
      <KeyNames>Chic</KeyNames>
      <SuffixToKey> </SuffixToKey>
    </Contributor>
    <Language>
      <LanguageRole>01</LanguageRole>
      <LanguageCode>rus</LanguageCode>
    </Language>
    <NumberOfPages>0</NumberOfPages>
    <MainSubject>
      <MainSubjectSchemeIdentifier>20</MainSubjectSchemeIdentifier>
      <SubjectHeadingText>Nuotit, Jazz pianolle</SubjectHeadingText>
    </MainSubject>
    <OtherText>
      <TextTypeCode>01</TextTypeCode>
      <Text>Contents&lt;br&gt; 
Thomas «Fats» WALLER. Keepin' out of Mischief Now
Oscar PETERSON. Whisper Not
Stan KENTON. Painted Rhythm
Oscar PETERSON. The Nearness of You
Stan KENTON. Southern Scandal&lt;br&gt; Nat «King» COLE. Blues&lt;br&gt; Bill EVANS. Interplay (Blues F minor)
Bill EVANS. Very Early
George SHEARING. Changing with the Times
Duke ELLINGTON. Echoes of Harlem
Chick COREA. Gemini</Text>
    </OtherText>
    <MediaFile>
      <MediaFileTypeCode>04</MediaFileTypeCode>
      <MediaFileFormatCode>03</MediaFileFormatCode>
      <MediaFileLinkTypeCode>01</MediaFileLinkTypeCode>
      <MediaFileLink>9785714009341.jpg</MediaFileLink>
      <MediaFileDate>20101220</MediaFileDate>
    </MediaFile> 
    <ProductWebsite>
      <ProductWebsiteLink>http://www.site.com/344.html</ProductWebsiteLink>
    </ProductWebsite>
    <Publisher>
      <PublisherName>Muzyka</PublisherName>
    </Publisher>
    <CountryOfPublication>RU</CountryOfPublication>
    <PublishingStatus>04</PublishingStatus>
    <PublicationDate>2010</PublicationDate>
    <SupplyDetail>
      <SupplierName>Company</SupplierName>
      <ProductAvailability>21</ProductAvailability>
      <Price>
        <PriceTypeCode>02</PriceTypeCode>
        <PriceAmount>9.0000</PriceAmount>
        <CurrencyCode>EUR</CurrencyCode>
        <TaxRateCode1>S</TaxRateCode1>
        <TaxRatePercent1>9</TaxRatePercent1>
      </Price>
    </SupplyDetail>
  </Product>
</ONIXMessage>

验证代码是:

    xml = "this XML document above";

    var r = new XmlTextReader(new StringReader(xml));
    var settings = new XmlReaderSettings();
    var sb = new StringBuilder();

    settings.ProhibitDtd = false;
    settings.ValidationType = ValidationType.DTD;
    settings.ValidationEventHandler += (a, e) =>
                                       {
                                           sb.AppendLine(e.Message);
                                           ret = false;
                                       };

    XmlReader validator = XmlReader.Create(r, settings);

    while (validator.Read())
    {
    }
    validator.Close();

我收到验证错误:

元素“产品”无效 子元素“语言”。名单 预期可能的要素: '系列内贡献者数量 YearOfAnnual ISBNOfSet EAN13OfSet ProductIdentifier TitleOfSet 标题集 TextCaseFlag 独特标题 TitlePrefix 标题'。

但是当我将此 XML 发送给我的合作伙伴时,他说该文档不包含验证错误。 他们使用 org.dom4j.io.SAXReader 来验证它。 怎么可能?我做的评估正确吗?

I have a XML document:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE ONIXMessage SYSTEM "http://www.editeur.org/onix/2.1/reference/onix-international.dtd">
<ONIXMessage>
  <Header>
    <FromCompany>Companyy</FromCompany>
    <SentDate>20101213</SentDate>
  </Header>
  <Product>
    <RecordReference>153640</RecordReference>
    <NotificationType>03</NotificationType>
    <ProductIdentifier>
      <ProductIDType>03</ProductIDType>
      <IDValue>9785714009341</IDValue>
    </ProductIdentifier>
    <ProductForm>AC</ProductForm>
    <NumberOfPieces>1</NumberOfPieces>
    <ProductClassification>
        <ProductClassificationType>02</ProductClassificationType>
        <ProductClassificationCode>55101514</ProductClassificationCode>
    </ProductClassification>
    <Title>
      <TitleType>01</TitleType>
      <TitleText>Jazz Piano. Volume 3.</TitleText>
    </Title>
    <Contributor>
      <SequenceNumber>1</SequenceNumber>
      <ContributorRole>A01</ContributorRole>
      <PersonName>Waller Fats</PersonName>
      <PersonNameInverted>Fats, Waller</PersonNameInverted>
      <TitlesBeforeNames> </TitlesBeforeNames>
      <NamesBeforeKey>Waller</NamesBeforeKey>
      <PrefixToKey> </PrefixToKey>
      <KeyNames>Fats</KeyNames>
      <SuffixToKey> </SuffixToKey>
    </Contributor>
    <Contributor>
      <SequenceNumber>2</SequenceNumber>
      <ContributorRole>A01</ContributorRole>
      <PersonName>Peterson Oscar</PersonName>
      <PersonNameInverted>Oscar, Peterson</PersonNameInverted>
      <TitlesBeforeNames> </TitlesBeforeNames>
      <NamesBeforeKey>Peterson</NamesBeforeKey>
      <PrefixToKey> </PrefixToKey>
      <KeyNames>Oscar</KeyNames>
      <SuffixToKey> </SuffixToKey>
    </Contributor>
    <Contributor>
      <SequenceNumber>3</SequenceNumber>
      <ContributorRole>A01</ContributorRole>
      <PersonName>Kenton Stan</PersonName>
      <PersonNameInverted>Stan, Kenton</PersonNameInverted>
      <TitlesBeforeNames> </TitlesBeforeNames>
      <NamesBeforeKey>Kenton</NamesBeforeKey>
      <PrefixToKey> </PrefixToKey>
      <KeyNames>Stan</KeyNames>
      <SuffixToKey> </SuffixToKey>
    </Contributor>
    <Contributor>
      <SequenceNumber>4</SequenceNumber>
      <ContributorRole>A01</ContributorRole>
      <PersonName>Evans Bill</PersonName>
      <PersonNameInverted>Bill, Evans</PersonNameInverted>
      <TitlesBeforeNames> </TitlesBeforeNames>
      <NamesBeforeKey>Evans</NamesBeforeKey>
      <PrefixToKey> </PrefixToKey>
      <KeyNames>Bill</KeyNames>
      <SuffixToKey> </SuffixToKey>
    </Contributor>
    <Contributor>
      <SequenceNumber>5</SequenceNumber>
      <ContributorRole>A01</ContributorRole>
      <PersonName>Shearing George</PersonName>
      <PersonNameInverted>George, Shearing</PersonNameInverted>
      <TitlesBeforeNames> </TitlesBeforeNames>
      <NamesBeforeKey>Shearing</NamesBeforeKey>
      <PrefixToKey> </PrefixToKey>
      <KeyNames>George</KeyNames>
      <SuffixToKey> </SuffixToKey>
    </Contributor>
    <Contributor>
      <SequenceNumber>6</SequenceNumber>
      <ContributorRole>A01</ContributorRole>
      <PersonName>D. Ellington</PersonName>
      <PersonNameInverted>Ellington, D.</PersonNameInverted>
      <TitlesBeforeNames> </TitlesBeforeNames>
      <NamesBeforeKey>D.</NamesBeforeKey>
      <PrefixToKey> </PrefixToKey>
      <KeyNames>Ellington</KeyNames>
      <SuffixToKey> </SuffixToKey>
    </Contributor>
    <Contributor>
      <SequenceNumber>7</SequenceNumber>
      <ContributorRole>A01</ContributorRole>
      <PersonName>Corea Chic</PersonName>
      <PersonNameInverted>Chic, Corea</PersonNameInverted>
      <TitlesBeforeNames> </TitlesBeforeNames>
      <NamesBeforeKey>Corea</NamesBeforeKey>
      <PrefixToKey> </PrefixToKey>
      <KeyNames>Chic</KeyNames>
      <SuffixToKey> </SuffixToKey>
    </Contributor>
    <Language>
      <LanguageRole>01</LanguageRole>
      <LanguageCode>rus</LanguageCode>
    </Language>
    <NumberOfPages>0</NumberOfPages>
    <MainSubject>
      <MainSubjectSchemeIdentifier>20</MainSubjectSchemeIdentifier>
      <SubjectHeadingText>Nuotit, Jazz pianolle</SubjectHeadingText>
    </MainSubject>
    <OtherText>
      <TextTypeCode>01</TextTypeCode>
      <Text>Contents<br> 
Thomas «Fats» WALLER. Keepin' out of Mischief Now
Oscar PETERSON. Whisper Not
Stan KENTON. Painted Rhythm
Oscar PETERSON. The Nearness of You
Stan KENTON. Southern Scandal<br> Nat «King» COLE. Blues<br> Bill EVANS. Interplay (Blues F minor)
Bill EVANS. Very Early
George SHEARING. Changing with the Times
Duke ELLINGTON. Echoes of Harlem
Chick COREA. Gemini</Text>
    </OtherText>
    <MediaFile>
      <MediaFileTypeCode>04</MediaFileTypeCode>
      <MediaFileFormatCode>03</MediaFileFormatCode>
      <MediaFileLinkTypeCode>01</MediaFileLinkTypeCode>
      <MediaFileLink>9785714009341.jpg</MediaFileLink>
      <MediaFileDate>20101220</MediaFileDate>
    </MediaFile> 
    <ProductWebsite>
      <ProductWebsiteLink>http://www.site.com/344.html</ProductWebsiteLink>
    </ProductWebsite>
    <Publisher>
      <PublisherName>Muzyka</PublisherName>
    </Publisher>
    <CountryOfPublication>RU</CountryOfPublication>
    <PublishingStatus>04</PublishingStatus>
    <PublicationDate>2010</PublicationDate>
    <SupplyDetail>
      <SupplierName>Company</SupplierName>
      <ProductAvailability>21</ProductAvailability>
      <Price>
        <PriceTypeCode>02</PriceTypeCode>
        <PriceAmount>9.0000</PriceAmount>
        <CurrencyCode>EUR</CurrencyCode>
        <TaxRateCode1>S</TaxRateCode1>
        <TaxRatePercent1>9</TaxRatePercent1>
      </Price>
    </SupplyDetail>
  </Product>
</ONIXMessage>

Validation code is:

    xml = "this XML document above";

    var r = new XmlTextReader(new StringReader(xml));
    var settings = new XmlReaderSettings();
    var sb = new StringBuilder();

    settings.ProhibitDtd = false;
    settings.ValidationType = ValidationType.DTD;
    settings.ValidationEventHandler += (a, e) =>
                                       {
                                           sb.AppendLine(e.Message);
                                           ret = false;
                                       };

    XmlReader validator = XmlReader.Create(r, settings);

    while (validator.Read())
    {
    }
    validator.Close();

I got validation error:

The element 'Product' has invalid
child element 'Language'. List of
possible elements expected:
'Contributor NumberWithinSeries
YearOfAnnual ISBNOfSet EAN13OfSet
ProductIdentifier TitleOfSet Title Set
TextCaseFlag DistinctiveTitle
TitlePrefix Title'.

But when I send this XML to my partner, he says that document contains no validation errors.
They use org.dom4j.io.SAXReader to validate it.
How can it be possible? Do I do valiation right?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生生漫 2024-10-21 12:47:18

从我所读到的内容来看,我猜测您的 .dtd 包含错误,而不是您的代码。您是否检查过 .dtd 是否包含 及其子项的定义?

Well from what I can read I would guess your .dtd contains errors, not your code. Have you checked that the .dtd contains a definition for <Language> and it's children?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文