简体   繁体   English

使用ColdFusion 10验证XML时找不到元素的声明

[英]Cannot find the declaration of element when Validating XML with ColdFusion 10

I am trying to use ColdFusion's XMLValidate() function to validate an XML Document against the ONIX XSD Schema . 我正在尝试使用ColdFusion的XMLValidate()函数针对ONIX XSD Schema验证XML文档。 ONIX is a 'standard' XML format used by some aspects of the book publishing industry. ONIX是书籍出版行业某些方面使用的“标准” XML格式。

This is a sample XML document [I modified some data for Client NDA type reasons; 这是一个XML文档样本[出于客户端NDA类型的原因,我修改了一些数据; sorry for the length] 抱歉!

<?xml version="1.0"?>
<ONIXMessage release="3.0">
  <Header>       
    <Sender>
      <SenderName>Me</SenderName>   
    </Sender>
    <SentDateTime>20131030T090000Z</SentDateTime>
    <MessageNote>My Test for SO</MessageNote>
  </Header>
  <Product>  
    <RecordReference>12345</RecordReference>     
    <NotificationType>03</NotificationType>
    <RecordSourceType>01</RecordSourceType>
    <RecordSourceName>Me</RecordSourceName>   
    <ProductIdentifier>
      <ProductIDType>15</ProductIDType>
      <IDValue>12324567801011</IDValue>
    </ProductIdentifier>
    <DescriptiveDetail>
      <ProductComposition>00</ProductComposition>
      <ProductForm>ED</ProductForm>
      <ProductFormDetail>E101</ProductFormDetail>
      <ProductFormDetail>E127</ProductFormDetail>
      <PrimaryContentType>10</PrimaryContentType>
      <EpubTechnicalProtection>01</EpubTechnicalProtection>
      <Collection>
        <CollectionType>10</CollectionType>
        <CollectionSequence>
          <CollectionSequenceType>02</CollectionSequenceType>
          <CollectionSequenceNumber>11</CollectionSequenceNumber>
        </CollectionSequence>
        <TitleDetail>
           <TitleType>01</TitleType>
           <TitleElement>
              <TitleElementLevel>02</TitleElementLevel>
              <TitlePrefix><![CDATA[The]]></TitlePrefix>
              <TitleWithoutPrefix><![CDATA[Something]]></TitleWithoutPrefix>
           </TitleElement>
        </TitleDetail>
      </Collection>    
      <TitleDetail>
        <TitleType>01</TitleType>
        <TitleElement>
          <SequenceNumber>1</SequenceNumber>
          <TitleElementLevel>01</TitleElementLevel>
          <TitleText>
            <![CDATA[ForSO]]>
          </TitleText>
        </TitleElement>
        <TitleStatement><![CDATA[The Something for SO]]></TitleStatement>
      </TitleDetail>
      <Contributor>  
        <SequenceNumber>1</SequenceNumber>
        <ContributorRole>A01</ContributorRole>
        <PersonName>Me, Myself</PersonName>
        <PersonNameInverted>Myself, Me</PersonNameInverted>
        <NamesBeforeKey>Myself</NamesBeforeKey>
        <KeyNames>Me</KeyNames>
      </Contributor>
      <Contributor>
        <SequenceNumber>2</SequenceNumber>
        <ContributorRole>A01</ContributorRole>
        <PersonName>Someone Else</PersonName>
        <PersonNameInverted>Else, Someone</PersonNameInverted>
        <NamesBeforeKey>Someone</NamesBeforeKey>
        <KeyNames>Else</KeyNames>
      </Contributor>
      <ContributorStatement>Me Myself and Someone Else</ContributorStatement>
      <Language>
        <LanguageRole>01</LanguageRole>
        <LanguageCode>eng</LanguageCode>
      </Language>
      <Extent>                              
        <ExtentType>00</ExtentType>
        <ExtentValue>40</ExtentValue>
        <ExtentUnit>03</ExtentUnit>
      </Extent>
      <Subject>
        <MainSubject/>
        <SubjectSchemeIdentifier>10</SubjectSchemeIdentifier>
        <SubjectCode>JUV001000</SubjectCode>
      </Subject>
      <Audience>
        <AudienceCodeType>01</AudienceCodeType>
        <AudienceCodeValue>02</AudienceCodeValue>
      </Audience>
      <AudienceRange>
        <AudienceRangeQualifier>17</AudienceRangeQualifier>
        <AudienceRangePrecision>03</AudienceRangePrecision>
        <AudienceRangeValue>8</AudienceRangeValue>
        <AudienceRangePrecision>04</AudienceRangePrecision>
        <AudienceRangeValue>12</AudienceRangeValue>
      </AudienceRange>
    </DescriptiveDetail>
    <CollateralDetail>
      <TextContent>
        <TextType>03</TextType>
        <ContentAudience>00</ContentAudience>
        <Text textformat="02">
          <![CDATA[Something, Something, Something, Dark Side]]>
        </Text>
      </TextContent>
    </CollateralDetail>
    <PublishingDetail>
      <Imprint>
        <ImprintName>Fake Publisher</ImprintName>
      </Imprint>
      <Publisher>
        <PublishingRole>01</PublishingRole>
        <PublisherName>Fake Publisher</PublisherName>
      </Publisher>
      <PublishingStatus>02</PublishingStatus>
      <PublishingDate>
        <PublishingDateRole>01</PublishingDateRole>
        <DateFormat>00</DateFormat>
        <Date>20110701</Date>
      </PublishingDate> 
      <SalesRights>                                  
        <SalesRightsType>02</SalesRightsType>
        <Territory>                                         
          <CountriesIncluded>GB AU NZ</CountriesIncluded>
        </Territory>
      </SalesRights>
      <SalesRestriction>
        <SalesRestrictionType>04</SalesRestrictionType>
        <SalesOutlet>
          <SalesOutletIdentifier>
            <SalesOutletIDType>03</SalesOutletIDType>
            <IDValue>AMZ</IDValue>
          </SalesOutletIdentifier>
          <SalesOutletName>Amazon</SalesOutletName>
        </SalesOutlet>
        <SalesOutlet>
          <SalesOutletIdentifier>
            <SalesOutletIDType>03</SalesOutletIDType>
            <IDValue>BNO</IDValue>
          </SalesOutletIdentifier>
          <SalesOutletName>Barnes And Noble</SalesOutletName>
        </SalesOutlet>
      </SalesRestriction>
    </PublishingDetail>
    <ProductSupply>
      <SupplyDetail>
        <Supplier>
          <SupplierRole>01</SupplierRole>
          <SupplierName>Me</SupplierName>
        </Supplier>
        <ProductAvailability>20</ProductAvailability>
        <Price>                          
          <PriceAmount>12.99</PriceAmount>
          <CurrencyCode>USD</CurrencyCode>
          <Territory>
            <CountriesIncluded>GB AU NZ</CountriesIncluded>
          </Territory>
        </Price>
      </SupplyDetail>
    </ProductSupply>
  </Product>
</ONIXMessage>  

Here is some sample code to validate the above Document against the schema. 这是一些示例代码,用于根据架构验证上述文档。 To run this code you'll have to download the scheme from the link above and unzip the schema documents into the root directory of your web server. 要运行此代码,您必须从上面的链接下载方案,然后将模式文档解压缩到Web服务器的根目录中。 Save the above XML as a file named sample.xml; 将上述XML保存为名为sample.xml的文件; you may have to modify the file path on the cffile tag. 您可能需要修改cffile标记上的文件路径。

<cffile action="read" file="Sample.xml" variable="xmlFileResults" >
<Cfset xmlValidateResults = xmlValidate(xmlFileResults,'#cgi.http_host#/ONIX_BookProduct_3.0_reference.xsd') />
<cfdump var="#xmlValidateResults#" /><Br/><br/>

It provides this error: 它提供了以下错误:

[Error] :2:28: cvc-elt.1: Cannot find the declaration of element 'ONIXMessage'. [错误]:2:28:cvc-elt.1:找不到元素'ONIXMessage'的声明。

If I translate the error correctly, it cannot find the ONIXMessage tag. 如果我正确翻译了错误,则找不到ONIXMessage标记。 But, I'm rather confused as to why. 但是,我很困惑为什么。

If I take a simpler XML file: 如果我使用一个简单的XML文件:

<?xml version="1.0"?>
<ONIXMessage release="3.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
</ONIXMessage>  

I still get the same error. 我仍然遇到相同的错误。

I'm trying to determine whether their is an error with the XML or the XSD or CFs validation functionality. 我正在尝试确定它们是否与XML或XSD或CFs验证功能有关。

Has anyone seen this? 有人看过吗? Does anyone have any insight? 有人有见识吗?


Based on comments, I wanted to add this: 根据评论,我想添加以下内容:

I have no idea if this helps debug; 我不知道这是否有助于调试; but if I change the XML Header to reference this: 但是如果我更改XML标头以引用此内容:

<ONIXMessage release="3.0" xmlns="http://ns.editeur.org/onix/3.0/reference"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
            xsi:schemaLocation="http://ns.editeur.org/onix/3.0/reference http://myserver/ONIX_BookProduct_3.0_reference.xsd"
            >

and remove the XSD reference from the XMLValidate() function: 并从XMLValidate()函数中删除XSD引用:

<cffile action="read" file="Sample.xml" variable="xmlFileResults" >
<Cfset xmlValidateResults = xmlValidate(xmlFileResults) />
<cfdump var="#xmlValidateResults#" /><Br/><br/>

I get a slew of different errors, although they seem valid. 尽管出现了很多错误,但我还是碰到了很多其他错误。 It has no trouble finding the declaration of the ONIXMesessage; 找到ONIXMesessage的声明没有问题; but does highlight a lot of other errors. 但确实突出了许多其他错误。 [I'm not sure if they are valid yet or not]. [我不确定它们是否有效]。

Unfortunately, It is not, practical, in our environment to have to "hard code" a schema location in the XML document, though. 不幸的是,在我们的环境中,必须“硬编码” XML文档中的架构位置是不实际的。

I was able to solve this issue. 我能够解决这个问题。 I had to do a few things. 我不得不做几件事。

First, the original schema made use of includes to include other files: 首先,利用包括的原始架构包括其他文件:

<xs:include schemaLocation="ONIX_BookProduct_CodeLists.xsd"/>
<xs:include schemaLocation="ONIX_XHTML_Subset.xsd"/>

I had to remove these includes and combine the three files into a single file for the validation to work. 我必须删除这些包含文件,并将这三个文件合并为一个文件,以便验证工作。 I do not know if it was an issue with the validator finding the files--due to the local path--or an issue the validation routine has with includes. 我不知道验证器查找文件是否有问题(由于本地路径),还是验证例程包含的问题。 Creating a single schema without the use of includes solved the issue. 在不使用include的情况下创建单个架构可以解决此问题。

I also had to add the schema location to the XML Header. 我还必须将架构位置添加到XML标头中。 This was the original: 这是原始的:

<ONIXMessage release="3.0">

And this is the modified header: 这是修改后的标头:

<ONIXMessage xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" release="3.0"
             xmlns="http://ns.editeur.org/onix/3.0/reference"  
             xsi:schemaLocation="http://ns.editeur.org/onix/3.0/reference http://mydomain.com/ONIX_BookProduct_3.0_reference.xsd">

Then the validation works successfully--or at least it appears to be; 然后验证成功进行-或至少看起来是这样; I'm still getting errors but I feel they are errors in the XML documents I'm trying to validate. 我仍然遇到错误,但是我认为它们是我要验证的XML文档中的错误。

<cffile action="read" file="Sample.xml" variable="xmlFileResults" >
<Cfset xmlValidateResults = xmlValidate(xmlFileResults) />
<cfdump var="#xmlValidateResults#" /><Br/><br/>

In order to prevent changes to the process creating the files; 为了防止更改创建文件的过程; I'm using this code to add the schema information to the header "on the fly" before we perform validation: 在执行验证之前,我正在使用以下代码将架构信息“即时”添加到标题中:

<cfset sourceString = "<ONIXMessage ([^<>]+)?>" />
<cfset replacementString = "<ONIXMessage xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" release=""3.0"" xmlns=""http://ns.editeur.org/onix/3.0/reference"" xsi:schemaLocation=""http://ns.editeur.org/onix/3.0/reference #someVaraibleWithAbsoluteURLToOnixSchema#"">" />
<Cfset fileToValidate = REReplaceNoCase(fileToValidate, sourceString, replacementString ) />

Thanks to this answer for the Regex for finding an opening tag. 感谢正则表达式的这个答案 ,因为它找到了一个开始标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM