如何在XML模式中声明引用元素的必需属性?

时间:2019-02-27 16:44:11

标签: xml xsd

  

因此,我一直在处理此XML和XSD文件,并尝试对其进行验证,但是在XSD文件中的cid属性出现错误,而在XML中的另一个错误可能与关闭目录标签上的XSD文件有关。

     

XML中的错误指出“ cvc-complex-type.2.4.b:元素'catalog'的内容不完整。应为'{“”:photo,“”:name}'之一。

 <?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<!--
   New Perspectives on XML
   Tutorial 3
   Case Problem 1

   Catalog of photos from the Our Lady of Bergen Historical Society
   Author: Joshua Carpentier
   Date:   2/20/19

   Filename:         catalog.xml
   Supporting Files: catalog.xsd
-->

<catalog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:noNamespaceSchemaLocation="catalog.xsd"
>

<!-- did you want us to included this attribute not sure xsi:noNamespaceSchemaLocation="catalog.xsd"-->
   <photo cid="c1748" donatedBy="John Borelli">
      <name metadata="tunis cooper property museum">Tunis R. Cooper property</name>
      <description>
      <![CDATA[ 
         This photo was taken by John Borelli's great-grandfather. It is believed to have been taken around 1830.
         David Demarest originally purchased the site of the chair factory in 1663. This site was under the ownership 
         of the Demarest family until 1758.  The property is listed in the National and New Jersey Registers of Historic Places.
      ]]>
      </description>
      <date>circa 1830</date>
      <images>
         <img src="1748a.jpg" />
         <img src="1748b.jpg" />
      </images>
   </photo>

   <photo cid="c1749" donatedBy="John Borelli">
      <name metadata="tunis cooper property museum">Tunis R. Cooper property</name>
      <description>
      <![CDATA[ 
         A more recent picture of the property taken by the Borelli family.  The property is listed in the 
         National and New Jersey Registers of Historic Places.
      ]]>
      </description>
      <date>circa 1950</date>
      <images>
         <img src="1749a.jpg" />
      </images>
   </photo>

   <photo cid="c1411" donatedBy="Saint Johns Catholic Church">
      <name metadata="saint johns catholic church">Saint Johns Church</name>
      <description>
      <![CDATA[ 
         A more recent picture of the property taken by the Borelli family.  The property is listed in the 
         National and New Jersey Registers of Historic Places.
      ]]>
      </description>      
      <date>1921</date>
   </photo>


   <photo cid="c2003" donatedBy="Linda Choo">
      <name metadata="bergenfield elementary school">Bergenfield School</name>
      <description>
      <![CDATA[ 
         The No. 5 Public School, a.k.a. Bergenfield School
      ]]>
      </description>
      <date>circa 1920</date>
      <images>
         <img src="2003a.jpg" />
         <img src="2003b.jpg" />
      </images>
   </photo>

   <photo cid="c2078" donatedBy="Maria Giodelli">
      <name metadata="coopers pond water">Coopers Pond</name>
      <description>
      <![CDATA[ 
         A favorite spot where we used to go hang out as kids.  The picture shows my brothers Robert and Michael.
      ]]>
      </description>
      <date>May 4, 1941</date>
      <images>
         <img src="2078a.jpg" />
      </images>
   </photo>

   <photo cid="c2079" donatedBy="Linda Uffington">
      <name metadata="watch timekeeping pocket railway">Pocket Watch</name>
      <description>
      <![CDATA[ 
         A more recent picture of the property taken by the Borelli family.  The property is listed in the 
         National and New Jersey Registers of Historic Places.
      ]]>
      </description>
      <date>circa 1870</date>
      <images>
         <img src="2079a.jpg" />
         <img src="2079b.jpg" />
      </images>
   </photo>

   <photo cid="c3233">
      <name metadata="hotel">Bergenfield Hotel</name>
      <description>
      <![CDATA[ 
         The Knollfield Hotel was known as the Bergenfield Hotel.  The property is listed in the 
         National and New Jersey Registers of Historic Places.
      ]]>
      </description>
      <date>circa 1920</date>
   </photo>

   <photo cid="c3433">
      <name metadata="sweeney coal fuel">Sweeney Coal</name>
      <description>
      <![CDATA[ 
         Sweeney Fuel Company located near New Bridge Road and railroad tracks.
      ]]>
      </description>
      <date>1920</date>
      <images>
         <img src="3433a.jpg" />
         <img src="3433b.jpg" />
         <img src="3433c.jpg" />
      </images>
   </photo>

</catalog>
  

XSD中的错误状态为“ s4s-elt-must-match.1:'目录'的内容必须匹配(注释?,(simpleType | complexType)?,(唯一|键| keyref)*)”)。发现问题始于:属性。”

     
      
  1. 到目前为止,我已经基于ID数据类型为cidType创建了simpleType,并且将其限制为正则表达式模式c \ d {4},并且基于字符串数据类型并已创建了另一个simpleType srcType,并且将其限制为正则表达式表情模式[a-zA-Z0-9] +。jpg
  2.   
     

这些是具体的问题,6b是我不确定在其他步骤方面我的代码是否还有其他问题。

     
      
  1. 声明照片元素,其中包含以下嵌套的子元素序列-名称,   说明,日期和图像。为嵌套元素设置以下属性:
  2.   
     

a。所有子元素都应包含字符串数据。名称元素还应该支持元数据属性。

     

b。 cid属性是必需的。 donatedBy属性是可选的。

     
      
  1. 声明img元素。它没有内容,并且包含必需的属性src。
  2.   
  3. 声明以下属性和元素:
  4.   
     

a。属性元数据必须具有字符串数据类型。

     

b。属性cid必须具有cidType数据类型。

     

c。属性src必须具有srcType数据类型。

     d。属性donatedBy必须具有字符串数据类型。

     

e。元素描述必须具有字符串数据类型。

     

f。元素日期必须具有字符串数据类型。

 <?xml version="1.0" encoding="UTF-8" ?>
<!--
   New Perspectives on XML
   Tutorial 3
   Case Problem 1

   Catalog of photos from the Our Lady of Bergen Historical Society
   Author: Joshua Carpentier
   Date:   2/20/19

   Filename:         catalog.xsd
   Supporting Files: catalog.xml
-->      
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:simpleType name="cidType"> 
<xs:restriction base ="xs:ID"> 
<xs:pattern value= "c\d{4}"/> 
</xs:restriction> 
</xs:simpleType> 

<xs:simpleType name="srcType"> 
<xs:restriction base ="xs:string"> 
<xs:pattern value= "[a-zA-Z0-9]+.jpg"/> 
</xs:restriction> 
</xs:simpleType> 

<xs:element name="catalog">



<xs:complexType> 
 <xs:sequence>
 <xs:element name="photo" minOccurs="1" maxOccurs="unbounded"/>
 <xs:element name="name" type="xs:string"/>
 <xs:element name="description" type="xs:string"/>
 <xs:element name="date" type="xs:string"/>
 <xs:element name="images" type="xs:string"/>
 </xs:sequence>
</xs:complexType> 


<xs:attribute name="cid" ref="cidType" use="required"/>
<xs:attribute name="donatedBy" use="optional" type="string"/>
<xs:attribute name="metadata"/>



<xs:element name="img" use="required" src=".jpg"/>
<xs:element name="metadata" type="xs:string"/>
<xs:element name="cid"  type="cidType"/>
<xs:element name="src"  type="srcType"/>
 </xs:element>

</xs:schema>

0 个答案:

没有答案
相关问题