【发布时间】:2023-03-17 06:56:01
【问题描述】:
我正在设计一个 XML 产品提要,许多网店将使用它来发布他们的产品数据。此产品 Feed 的结构将基于 Atom XML standard,类似于 Google's Atom product feed。我将发布一个 XSD 文件,可用于验证产品提要。
基本上,每个<entry> 元素将代表一个产品。我需要向<entry> 元素添加一些子元素,其中将包含产品价格、运费等数据。
问题在于创建 XSD 文件。我不确定如何扩展 Atom 标准,以便可以将子元素添加到 <entry>。目前我只是将额外元素定义为顶级元素,但这不允许我指定出现指示器(minOccurs 和 maxOccurs)。
我想要做的是指定每个<entry> 元素中所需的元素数量。它们可以是我的架构引入的新元素(例如包含产品价格的 <price> 元素),也可以是现有的 Atom 元素(例如由 Atom 定义但不是必需的 <link> 元素) .
这是我当前的 product-feed.xsd(简化版):
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://example.com/schemas/product-feed"
xmlns:p="http://example.com/schemas/product-feed"
xmlns:atom="http://www.w3.org/2005/Atom"
elementFormDefault="qualified">
<xs:element name="brand" type="xs:string" />
<xs:element name="price" type="p:money" />
<xs:element name="shipping" type="p:money" />
<xs:complexType name="money">
<xs:simpleContent>
<xs:extension base="xs:decimal">
<xs:attribute name="currency"
type="p:currency"
use="required" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:simpleType name="currency">
<xs:restriction base="xs:string">
<xs:enumeration value="EUR" />
<xs:enumeration value="USD" />
<xs:enumeration value="GBP" />
</xs:restriction>
</xs:simpleType>
</xs:schema>
这是一个示例 xml 提要:
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"
xmlns:p="http://example.com/schemas/product-feed"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<title>Example Store</title>
<link href="http://www.example-store.com/" rel="self" />
<updated>2014-08-08T10:44:20Z</updated>
<entry>
<title>Foo</title>
<link href="http://www.example-store.com/products/foo.html" />
<p:price currency="EUR">32.95</p:price>
<p:shipping currency="EUR">6.75</p:shipping>
</entry>
<entry>
<title>Bar</title>
<link href="http://www.example-store.com/products/acme-bar.html" />
<p:brand>Acme</p:brand>
<p:price currency="EUR">12.50</p:price>
<p:shipping currency="EUR">6.75</p:shipping>
</entry>
</feed>
如何扩展 Atom 架构,使我的自定义元素只允许在 <entry> 元素内,并且我可以定义它们可能出现的次数?
我能想到的唯一替代解决方案是复制一个 Atom 模式定义文件(例如 this one),然后对其进行修改(添加我自己的元素,并更改我想要的 Atom 元素)。这感觉不太好(我不会再扩展 Atom,我只会创建一个全新的模式)所以我希望有更好的解决方案。
【问题讨论】:
-
您只是想要一个 XSD 架构来验证您的数据,还是需要它为 Atom 命名空间合并一些现有的 XSD 架构?如果是后者,那么您希望使用 Atom 命名空间的现有 XSD 架构吗?
-
您说“将额外元素定义为顶级元素......允许它们在任何地方使用,而不仅仅是在
<entry>元素内”,这表明您不希望它们在<entry>之外有效,覆盖Atom 模式的规则,即它们在其他地方在 有效。对于 Atom 模式,您还想强制执行哪些其他限制?您要编写的架构还有哪些其他要求? -
@C.M.Sperberg-McQueen 据我所知,Atom 标准没有权威的 XSD 架构,但我确实找到了 this one。我不需要扩展现有模式,但我更愿意这样做,而不是在我自己的 XSD 中复制所有内容。你是对的,Atom 允许在多个地方添加额外的元素。但是,例如,我希望能够要求每个
元素包含一个 元素(我引入的自定义元素)和一个 元素(由 Atom 定义,但不是必需的)。请参阅我更新的问题。