【问题标题】:Validating a Google atom feed验证 Google atom 提要
【发布时间】:2015-02-12 16:42:03
【问题描述】:

我在尝试基于 Google Atom 提要验证 XML 文档时遇到以下错误。

错误 - 第 8、10 行:org.xml.sax.SAXParseException;行号:8; 列号:10; cvc-complex-type.2.4.a:发现无效内容 从元素“g:brand”开始。 '{"xxx..w3.org/2005/Atom":id 之一, “xxx.w3.org/2005/Atom”:标题,“.w3.org/2005/Atom”:链接, 应为“xxx.w3.org/2005/Atom”:brand}'。

这是我的 XML:

    <?xml version="1.0" encoding="UTF-8"?>
    <feed xmlns="http://www.w3.org/2005/Atom" xmlns:g="http://base.google.com/ns/1.0">
        <title>The name of your data feed.</title>
        <link rel="self" href="http://www.example.com"/>
        <updated>2006-06-11T18:30:02Z</updated>
    <entry>
    <title>Red wool sweater</title>
    <g:brand>Acme</g:brand>
    <g:condition>new</g:condition>
    <summary>Comfortable and soft, this sweater will keep you warm on those cold winter nights.</summary>
    <id>1</id>
    <g:image_link>http://www.example.com/image1.jpg</g:image_link>
    <link href="http://www.example.com/item1-info-page.html"/>
    <g:mpn>ABC123</g:mpn>
    <g:price>25</g:price>
    <g:product_type>Clothing &amp; Accessories &gt; Clothing &gt; Outerwear &gt; Sweaters</g:product_type>
    <g:quantity>3</g:quantity>
    <g:shipping>
       <g:country>US</g:country>
       <g:region>MA</g:region>
       <g:service>Ground</g:service>
       <g:price>5.95</g:price>
    </g:shipping>
    <g:shipping>
       <g:country>US</g:country>
       <g:region>024*</g:region>
       <g:service>Ground</g:service>
       <g:price>7.95</g:price>
    </g:shipping>
    <g:tax>
       <g:country>US</g:country>
       <g:region>CA</g:region>
       <g:rate>8.25</g:rate>
       <g:tax_ship>y</g:tax_ship>
    </g:tax>
    <g:tax>
       <g:country>US</g:country>
       <g:region>926*</g:region>
       <g:rate>8.75</g:rate>
       <g:tax_ship>y</g:tax_ship>
    </g:tax>
    <g:upc>0001230001232</g:upc>
    <g:weight>0.1 lb</g:weight>
    </entry>
    </feed>

这是我的 XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://www.w3.org/2005/Atom"
           elementFormDefault="qualified"
           attributeFormDefault="unqualified"
           xmlns:atom="http://www.w3.org/2005/Atom"
           xmlns:g="http://base.google.com/ns/1.0"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:import namespace="http://www.w3.org/XML/1998/namespace"
             schemaLocation="http://www.w3.org/2001/03/xml.xsd"/>
  <xs:element name="feed" type="atom:feedType"/>
  <xs:element name="entry" type="atom:entryType"/>
  <xs:element name="extended_attribute" type="atom:extendedAttributeType"/>
  <xs:complexType name="textType" mixed="true">
    <xs:sequence>
      <xs:any namespace="http://www.w3.org/1999/xhtml" minOccurs="0"/>
    </xs:sequence>
    <xs:attribute name="type">
      <xs:simpleType>
        <xs:restriction base="xs:token">
          <xs:enumeration value="text"/>
          <xs:enumeration value="html"/>
          <xs:enumeration value="xhtml"/>
        </xs:restriction>
      </xs:simpleType>
    </xs:attribute>
    <xs:attributeGroup ref="atom:commonAttributes"/>
  </xs:complexType>
  <xs:complexType name="idType">
    <xs:simpleContent>
      <xs:extension base="xs:anyURI">
        <xs:attributeGroup ref="atom:commonAttributes"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="feedType">
    <xs:choice minOccurs="3" maxOccurs="unbounded">
      <xs:element name="link" type="atom:linkType" minOccurs="0" maxOccurs="unbounded"/>
      <xs:element name="title" type="atom:textType" minOccurs="1" maxOccurs="1"/>
      <xs:element name="updated" type="atom:dateTimeType" minOccurs="1" maxOccurs="1"/>
      <xs:element name="entry" type="atom:entryType" minOccurs="0" maxOccurs="unbounded"/>
      <xs:any namespace="##other" minOccurs="0" maxOccurs="unbounded"/>
    </xs:choice>
    <xs:attributeGroup ref="atom:commonAttributes"/>
  </xs:complexType>
  <xs:complexType name="extendedAttributeType">
    <xs:choice  maxOccurs="unbounded">
      <xs:element name="name" type="xs:string" minOccurs="1" maxOccurs="1"/>
      <xs:element name="value" type="xs:string" minOccurs="1" maxOccurs="1"/>
    </xs:choice>
    <xs:attributeGroup ref="atom:commonAttributes"/>
  </xs:complexType>
  <xs:complexType name="entryType">
    <xs:choice maxOccurs="unbounded">
      <xs:element name="id" type="atom:idType" minOccurs="1" maxOccurs="1"/>
      <xs:element name="title" type="atom:textType" minOccurs="1" maxOccurs="1"/>
      <xs:element name="link" type="atom:linkType" minOccurs="0" maxOccurs="unbounded"/>
      <xs:element name="brand" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
    </xs:choice>
    <xs:attributeGroup ref="atom:commonAttributes"/>
  </xs:complexType>

  <xs:complexType name="linkType" mixed="true">
    <xs:attribute name="href" use="required" type="xs:anyURI"/>
    <xs:attribute name="rel" type="xs:string" use="optional"/>
    <xs:attribute name="type" use="optional" type="xs:string"/>
    <xs:attribute name="hreflang" use="optional" type="xs:NMTOKEN"/>
    <xs:attribute name="title" use="optional" type="xs:string"/>
    <xs:attribute name="length" use="optional" type="xs:positiveInteger"/>
    <xs:attributeGroup ref="atom:commonAttributes"/>
  </xs:complexType>

  <xs:complexType name="dateTimeType">
    <xs:simpleContent>
      <xs:extension base="xs:dateTime">
        <xs:attributeGroup ref="atom:commonAttributes"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:attributeGroup name="commonAttributes">
    <xs:attribute ref="xml:base"/>
    <xs:attribute ref="xml:lang"/>
    <xs:anyAttribute namespace="##other"/>
  </xs:attributeGroup>
</xs:schema>

我猜这个错误与我在 XML (xmlns:g="http://base.google.com/ns/1.0") 中声明的命名空间有关。我已经在 XSD 的 targetNamespace 中输入了这个,但我无法让它工作。

【问题讨论】:

  • 您希望更改 XML 以符合 XSD 还是反之亦然?
  • 您好,感谢您回复这是我正在努力解决的 xsd 以及我希望更改的 xsd,因为 xml 是正确的

标签: xml xsd xsd-validation


【解决方案1】:

Atom 1.0 XSD

The Atom Syndication Format 是一个带有existing XSD 的建立标准:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://www.w3.org/2005/Atom"
           elementFormDefault="qualified"
           attributeFormDefault="unqualified"
           xmlns:atom="http://www.w3.org/2005/Atom"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:annotation>
    <xs:documentation>
      This version of the Atom schema is based on version 1.0 of the format specifications,
      found here http://www.atomenabled.org/developers/syndication/atom-format-spec.php.
    </xs:documentation>
  </xs:annotation>
  <xs:import namespace="http://www.w3.org/XML/1998/namespace"
             schemaLocation="http://www.w3.org/2001/03/xml.xsd"/>
  <xs:annotation>
    <xs:documentation>
      An Atom document may have two root elements, feed and entry, as defined in section 2.
    </xs:documentation>
  </xs:annotation>
  <xs:element name="feed" type="atom:feedType"/>
  <xs:element name="entry" type="atom:entryType"/>
  <xs:complexType name="textType" mixed="true">
    <xs:annotation>
      <xs:documentation>
        The Atom text construct is defined in section 3.1 of the format spec.
      </xs:documentation>
    </xs:annotation>
    <xs:sequence>
      <xs:any namespace="http://www.w3.org/1999/xhtml" minOccurs="0"/>
    </xs:sequence>
    <xs:attribute name="type">
      <xs:simpleType>
        <xs:restriction base="xs:token">
          <xs:enumeration value="text"/>
          <xs:enumeration value="html"/>
          <xs:enumeration value="xhtml"/>
        </xs:restriction>
      </xs:simpleType>
    </xs:attribute>
    <xs:attributeGroup ref="atom:commonAttributes"/>
  </xs:complexType>
  <xs:complexType name="personType">
    <xs:annotation>
      <xs:documentation>
        The Atom person construct is defined in section 3.2 of the format spec.
      </xs:documentation>
    </xs:annotation>
    <xs:choice minOccurs="1" maxOccurs="unbounded">
      <xs:element name="name" type="xs:string" minOccurs="1" maxOccurs="1"/>
      <xs:element name="uri" type="atom:uriType" minOccurs="0" maxOccurs="1"/>
      <xs:element name="email" type="atom:emailType" minOccurs="0" maxOccurs="1"/>
      <xs:any namespace="##other"/>
    </xs:choice>
    <xs:attributeGroup ref="atom:commonAttributes"/>
  </xs:complexType>
  <xs:simpleType name="emailType">
    <xs:annotation>
      <xs:documentation>
        Schema definition for an email address.
      </xs:documentation>
    </xs:annotation>
    <xs:restriction base="xs:normalizedString">
      <xs:pattern value="\w+@(\w+\.)+\w+"/>
    </xs:restriction>
  </xs:simpleType>
  <xs:complexType name="feedType">
    <xs:annotation>
      <xs:documentation>
        The Atom feed construct is defined in section 4.1.1 of the format spec.
      </xs:documentation>
    </xs:annotation>
    <xs:choice minOccurs="3" maxOccurs="unbounded">
      <xs:element name="author" type="atom:personType" minOccurs="0" maxOccurs="unbounded"/>
      <xs:element name="category" type="atom:categoryType" minOccurs="0" maxOccurs="unbounded"/>
      <xs:element name="contributor" type="atom:personType" minOccurs="0" maxOccurs="unbounded"/>
      <xs:element name="generator" type="atom:generatorType" minOccurs="0" maxOccurs="1"/>
      <xs:element name="icon" type="atom:iconType" minOccurs="0" maxOccurs="1"/>
      <xs:element name="id" type="atom:idType" minOccurs="1" maxOccurs="1"/>
      <xs:element name="link" type="atom:linkType" minOccurs="0" maxOccurs="unbounded"/>
      <xs:element name="logo" type="atom:logoType" minOccurs="0" maxOccurs="1"/>
      <xs:element name="rights" type="atom:textType" minOccurs="0" maxOccurs="1"/>
      <xs:element name="subtitle" type="atom:textType" minOccurs="0" maxOccurs="1"/>
      <xs:element name="title" type="atom:textType" minOccurs="1" maxOccurs="1"/>
      <xs:element name="updated" type="atom:dateTimeType" minOccurs="1" maxOccurs="1"/>
      <xs:element name="entry" type="atom:entryType" minOccurs="0" maxOccurs="unbounded"/>
      <xs:any namespace="##other" minOccurs="0" maxOccurs="unbounded"/>
    </xs:choice>
    <xs:attributeGroup ref="atom:commonAttributes"/>
  </xs:complexType>
  <xs:complexType name="entryType">
    <xs:annotation>
      <xs:documentation>
        The Atom entry construct is defined in section 4.1.2 of the format spec.
      </xs:documentation>
    </xs:annotation>
    <xs:choice maxOccurs="unbounded">
      <xs:element name="author" type="atom:personType" minOccurs="0" maxOccurs="unbounded"/>
      <xs:element name="category" type="atom:categoryType" minOccurs="0" maxOccurs="unbounded"/>
      <xs:element name="content" type="atom:contentType" minOccurs="0" maxOccurs="1"/>
      <xs:element name="contributor" type="atom:personType" minOccurs="0" maxOccurs="unbounded"/>
      <xs:element name="id" type="atom:idType" minOccurs="1" maxOccurs="1"/>
      <xs:element name="link" type="atom:linkType" minOccurs="0" maxOccurs="unbounded"/>
      <xs:element name="published" type="atom:dateTimeType" minOccurs="0" maxOccurs="1"/>
      <xs:element name="rights" type="atom:textType" minOccurs="0" maxOccurs="1"/>
      <xs:element name="source" type="atom:sourceType" minOccurs="0" maxOccurs="1"/>
      <xs:element name="summary" type="atom:textType" minOccurs="0" maxOccurs="1"/>
      <xs:element name="title" type="atom:textType" minOccurs="1" maxOccurs="1"/>
      <xs:element name="updated" type="atom:dateTimeType" minOccurs="1" maxOccurs="1"/>
      <xs:any namespace="##other" minOccurs="0" maxOccurs="unbounded"/>
    </xs:choice>
    <xs:attributeGroup ref="atom:commonAttributes"/>
  </xs:complexType>
  <xs:complexType name="contentType" mixed="true">
    <xs:annotation>
      <xs:documentation>
        The Atom content construct is defined in section 4.1.3 of the format spec.
      </xs:documentation>
    </xs:annotation>
    <xs:sequence>
      <xs:any namespace="##other" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
    <xs:attribute name="type" type="xs:string"/>
    <xs:attribute name="src" type="xs:anyURI"/>
    <xs:attributeGroup ref="atom:commonAttributes"/>
  </xs:complexType>
  <xs:complexType name="categoryType">
    <xs:annotation>
      <xs:documentation>
        The Atom cagegory construct is defined in section 4.2.2 of the format spec.
      </xs:documentation>
    </xs:annotation>
    <xs:attribute name="term" type="xs:string" use="required"/>
    <xs:attribute name="scheme" type="xs:anyURI" use="optional"/>
    <xs:attribute name="label" type="xs:string" use="optional"/>
    <xs:attributeGroup ref="atom:commonAttributes"/>
  </xs:complexType>
  <xs:complexType name="generatorType">
    <xs:annotation>
      <xs:documentation>
        The Atom generator element is defined in section 4.2.4 of the format spec.
      </xs:documentation>
    </xs:annotation>
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute name="uri" use="optional" type="xs:anyURI"/>
        <xs:attribute name="version" use="optional" type="xs:string"/>
        <xs:attributeGroup ref="atom:commonAttributes"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="iconType">
    <xs:annotation>
      <xs:documentation>
        The Atom icon construct is defined in section 4.2.5 of the format spec.
      </xs:documentation>
    </xs:annotation>
    <xs:simpleContent>
      <xs:extension base="xs:anyURI">
        <xs:attributeGroup ref="atom:commonAttributes"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="idType">
    <xs:annotation>
      <xs:documentation>
        The Atom id construct is defined in section 4.2.6 of the format spec.
      </xs:documentation>
    </xs:annotation>
    <xs:simpleContent>
      <xs:extension base="xs:anyURI">
        <xs:attributeGroup ref="atom:commonAttributes"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="linkType" mixed="true">
    <xs:annotation>
      <xs:documentation>
        The Atom link construct is defined in section 3.4 of the format spec.
      </xs:documentation>
    </xs:annotation>
    <xs:attribute name="href" use="required" type="xs:anyURI"/>
    <xs:attribute name="rel" type="xs:string" use="optional"/>
    <xs:attribute name="type" use="optional" type="xs:string"/>
    <xs:attribute name="hreflang" use="optional" type="xs:NMTOKEN"/>
    <xs:attribute name="title" use="optional" type="xs:string"/>
    <xs:attribute name="length" use="optional" type="xs:positiveInteger"/>
    <xs:attributeGroup ref="atom:commonAttributes"/>
  </xs:complexType>
  <xs:complexType name="logoType">
    <xs:annotation>
      <xs:documentation>
        The Atom logo construct is defined in section 4.2.8 of the format spec.
      </xs:documentation>
    </xs:annotation>
    <xs:simpleContent>
      <xs:extension base="xs:anyURI">
        <xs:attributeGroup ref="atom:commonAttributes"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="sourceType">
    <xs:annotation>
      <xs:documentation>
        The Atom source construct is defined in section 4.2.11 of the format spec.
      </xs:documentation>
    </xs:annotation>
    <xs:choice maxOccurs="unbounded">
      <xs:element name="author" type="atom:personType" minOccurs="0" maxOccurs="unbounded"/>
      <xs:element name="category" type="atom:categoryType" minOccurs="0" maxOccurs="unbounded"/>
      <xs:element name="contributor" type="atom:personType" minOccurs="0" maxOccurs="unbounded"/>
      <xs:element name="generator" type="atom:generatorType" minOccurs="0" maxOccurs="1"/>
      <xs:element name="icon" type="atom:iconType" minOccurs="0" maxOccurs="1"/>
      <xs:element name="id" type="atom:idType" minOccurs="0" maxOccurs="1"/>
      <xs:element name="link" type="atom:linkType" minOccurs="0" maxOccurs="unbounded"/>
      <xs:element name="logo" type="atom:logoType" minOccurs="0" maxOccurs="1"/>
      <xs:element name="rights" type="atom:textType" minOccurs="0" maxOccurs="1"/>
      <xs:element name="subtitle" type="atom:textType" minOccurs="0" maxOccurs="1"/>
      <xs:element name="title" type="atom:textType" minOccurs="0" maxOccurs="1"/>
      <xs:element name="updated" type="atom:dateTimeType" minOccurs="0" maxOccurs="1"/>
      <xs:any namespace="##other" minOccurs="0" maxOccurs="unbounded"/>
    </xs:choice>
    <xs:attributeGroup ref="atom:commonAttributes"/>
  </xs:complexType>
  <xs:complexType name="uriType">
    <xs:simpleContent>
      <xs:extension base="xs:anyURI">
        <xs:attributeGroup ref="atom:commonAttributes"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="dateTimeType">
    <xs:simpleContent>
      <xs:extension base="xs:dateTime">
        <xs:attributeGroup ref="atom:commonAttributes"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:attributeGroup name="commonAttributes">
    <xs:attribute ref="xml:base"/>
    <xs:attribute ref="xml:lang"/>
    <xs:anyAttribute namespace="##other"/>
  </xs:attributeGroup>
</xs:schema>

但是,即使使用此 XSD 来验证您的 XML,仍然会导致如下错误:

[错误] feed.xml:12:14: cvc-complex-type.2.4.c: 匹配的通配符 是严格的,但找不到元素“g:brand”的声明。

Google Base 命名空间

要消除与http://base.google.com/ns/1.0 Google Base namespace extensions 相关的错误,您有两种选择:

  1. 将 Google Base XSD 导入 Atom XSD:

    <xs:import namespace="http://base.google.com/ns/1.0"
               schemaLocation="base-google.xsd"/>
    

    这里的挑战是 (a) 与 http://base.google.com/ns/1.0 命名空间关联的 XSD 似乎不在知名的官方位置,并且 (b) XSD 可能没有得到维护。我找到了一个here。 [如果有人知道更正式或更最新的 Google Base XSD,请发表评论,我会更新链接。]

  2. xsd:any/@processContents="strict"(默认)松开为lax。 (区别见processContents strict vs lax vs skip for xsd:any。)

您选择哪条路线取决于您希望对xsd:any 下的 Google 元素进行多少验证。第一条路线将提供更严格的验证,但您的下一个问题将是您必须更改 XML 或 XSD 以修复更多错误,例如没有声明 g:mpn。第二条路线会很高兴地忽略这些错误。

【讨论】:

  • 魔术感谢您的回复。我要离开一个星期,但会做的。我回来后的变化
  • 如果我添加了 processcontents lax 那么我的验证都不起作用,所以我删除了这个并添加了谷歌原子命名空间的导入,但现在我收到以下错误错误 - 第 13、25 行:org. xml.sax.SAXParseException;行号:13;列号:25; cvc-complex-type.2.4.c:匹配通配符是严格的,但找不到元素“g:availability”的声明。所以在原子导入中没有声明可用性我如何在本地或没有名称空间声明它??
  • 理想情况下,您应该为 http://base.google.com/ns/1.0 命名空间使用最新的、完全维护的 XSD。除此之外,我要么直接更新你现有的那个,要么变得更漂亮并扩展它。既然您知道您的 XML 是正确的并且 XSD 已过时,那么直接编辑 XSD(如果其他人想要使用它,也可能使其公开可用)不会是一个糟糕的选择。
【解决方案2】:

我在任何地方都找不到 XSD,但 Google 在此处提供了提要规范: https://support.google.com/merchants/answer/7052112

你不是 XSD。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-16
    • 1970-01-01
    • 2010-10-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-29
    • 1970-01-01
    相关资源
    最近更新 更多