【问题标题】:how to declare a photo element in xsd containing a sequence of nested child element?如何在 xsd 中声明包含一系列嵌套子元素的照片元素?
【发布时间】:2019-07-20 09:35:16
【问题描述】:

所以我在我的照片元素的 xsd 文件中不断收到此错误,它显示“s4s-elt-must-match.1:'目录'的内容必须匹配(注释?,(simpleType | complexType)?,( unique | key | keyref)*))。发现问题从:元素开始。”

<?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">`enter code here`
     <xs:element name="photo" minOccurs="1" maxOccurs="unbounded">


    <xs:complexType > 
     <xs:sequence>
     <xs:element ref="name" type="string" />
     <xs:element ref="description" type="string" />
     <xs:element ref="date" type="string" />
     <xs:element ref="images" type="string" />
     </xs:sequence>
    </xs:complexType> 


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

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


</xs:schema>



**im trying to achieve this

声明包含以下嵌套子元素序列的照片元素 - 名称, 描述、日期和图像。为嵌套元素设置以下属性: 一种。所有子元素都应包含字符串数据。 name 元素也应该支持 元数据属性。 湾。 cid 属性是必需的。 donatedBy 属性是可选的。

**

<?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">

       <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 和 XML 是否正确。反引号中的“在此处输入代码”文本看起来很不合适,并且有些元素未关闭。这显然不是一个有效的架构,但错误与您报告的错误消息没有明确的关系,所以我怀疑除了真实错误之外,我们还看到了虚假错误。

标签: xml xsd


【解决方案1】:

您的 XSD 文件无效。它不是格式良好的。要更正它,请使用结束标记完成元素。所以替换

<xs:element name="catalog">`enter code here`
  <xs:element name="photo" minOccurs="1" maxOccurs="unbounded">
  ...
</xs:element>

<xs:element name="catalog">
    <xs:element name="photo" minOccurs="1" maxOccurs="unbounded">
      <xs:complexType>
        <xs:sequence>
          <xs:element ref="name" type="string"/>
          <xs:element ref="description" type="string"/>
          <xs:element ref="date" type="string"/>
          <xs:element ref="images" type="string"/>
        </xs:sequence>
      </xs:complexType>
      <xs:attribute ref="cid" use="required"/>
      <xs:attribute ref="metadata"/>
      <xs:attribute ref="donatedBy" use="optional" type="string"/>
    </xs:element>
    <xs:element name="img" use="required" src=".jpg"/>
    <xs:element ref="metadata" type="string"/>
    <xs:element ref="cid" type="cidType"/>
    <xs:element ref="src" type="srcType"/>
</xs:element>

现在您的 XSD 将验证您的 XML。

【讨论】:

  • 这仍然不是格式正确的 - 元素 name="photo" 没有关闭。即使它格式正确,也不是有效的模式文档; xs:element 不能有 xs:element 作为孩子,
  • 对不起,原始问题中的缩进错误和您答案中的缩进错误的组合让我感到困惑。我认为你已经把它变成了格式良好(虽然缩进很糟糕)的 XML,但你肯定没有把它变成一个有效的模式。
  • @MichaelKay:我也很困惑。我现在改变了我的答案,但我仍然不确定这是否正确。我确实使用xmlstarlet 来验证 XSD,它确实将 XML 验证为 valid
  • 我仍然想知道为什么第一个版本被xmlstarlet认为是“有效的”。
【解决方案2】:

xs:element 元素不能有另一个 xs:element 元素作为子元素。通常xs:elementxs:complexType 作为子级,它本身将xs:sequence 作为子级,而xs:element 作为子级。

【讨论】:

    猜你喜欢
    • 2012-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-30
    • 1970-01-01
    • 2021-10-23
    • 2013-05-06
    • 2018-10-13
    相关资源
    最近更新 更多