ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

c# – 如何针对dtd验证xml

2019-07-10 05:04:37  阅读:173  来源: 互联网

标签:c dtd xml-validation


我有一个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();

我收到验证错误:

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’.

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

解决方法:

从我能读到的内容我猜你的.dtd包含错误,而不是你的代码.您是否检查过.dtd包含< Language>的定义?这是孩子们的?

标签:c,dtd,xml-validation
来源: https://codeday.me/bug/20190710/1420789.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有