确保 XML 架构中的唯一属性值
Ensure Unique Attribute Values in XML Schema
我试图确保 <property_advert>
元素中的属性是唯一的。例如属性 agent_advert_reference
应该是唯一的。我希望下面的 XML 由于重复的属性而失败。
<?xml version="1.0" encoding="UTF-8"?>
<property_adverts xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xs:schemaLocation="http://www.example.org/XmplifyDerivedSchema.xsd">
<header>
<harvest_run_info>
<harvest_time>12:05:00</harvest_time>
<harvest_date>2015-10-10</harvest_date>
<harvester_version>1.0</harvester_version>
<harvester_path>/Users/xandrani/Sites/property_harvester_new</harvester_path>
</harvest_run_info>
<property_agent>
<company_name>CPS Homes</company_name>
</property_agent>
</header>
<adverts>
<property_advert agent_advert_reference="X1204" static_advert_url="https://www.example.com/properties/1204" original_html_file="/RandomName/advert_30Sep2015_14436555622486.html">
<rent_cost>450</rent_cost>
<rent_frequency>per month</rent_frequency>
<number_of_bedrooms>4</number_of_bedrooms>
<available_date>now</available_date>
<property_description></property_description>
<main_photo_url>https://www.example.com/properties/X1204/fred.jpg</main_photo_url>
<branch_name>Brusque Bay</branch_name>
<property_type>House</property_type>
<building_name/>
<street_name>High Street</street_name>
<postcode>CF11 6BP</postcode>
<furnished>true</furnished>
<location_area>Canton</location_area>
</property_advert>
<property_advert agent_advert_reference="X1204" static_advert_url="https://www.example.com/properties/1204" original_html_file="/RandomName/advert_30Sep2015_14436555622486.html">
<rent_cost>450</rent_cost>
<rent_frequency>per month</rent_frequency>
<number_of_bedrooms>4</number_of_bedrooms>
<available_date>now</available_date>
<property_description></property_description>
<main_photo_url>https://www.example.com/properties/X1204/fred.jpg</main_photo_url>
<branch_name>Brusque Bay</branch_name>
<property_type>House</property_type>
<building_name/>
<street_name>High Street</street_name>
<postcode>CF11 6BP</postcode>
<furnished>true</furnished>
<location_area>Canton</location_area>
</property_advert>
</adverts>
</property_adverts>
我的架构如下:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:annotation>
<xs:documentation xml:lang="en">
XML Schema.
</xs:documentation>
</xs:annotation>
<xs:element name="harvest_run_info" type="harvest_run_info_type"/>
<xs:complexType name="harvest_run_info_type">
<xs:all minOccurs="1" maxOccurs="1">
<xs:element name="harvest_time" type="xs:time" minOccurs="1" maxOccurs="1"/>
<xs:element name="harvest_date" type="xs:date" minOccurs="1" maxOccurs="1"/>
<xs:element name="harvester_version" type="harvester_version_type" minOccurs="1" maxOccurs="1"/>
<xs:element name="harvester_path" type="xs:string" minOccurs="1" maxOccurs="1"/>
</xs:all>
</xs:complexType>
<xs:element name="property_advert" type="property_advert_type"/>
<xs:complexType name="property_advert_type">
<xs:all>
<xs:element name="rent_cost" type="xs:positiveInteger" minOccurs="1" maxOccurs="1"/>
<xs:element name="rent_frequency" type="rent_frequency_type" minOccurs="1" maxOccurs="1"/>
<xs:element name="number_of_bedrooms" type="xs:positiveInteger" minOccurs="1" maxOccurs="1"/>
<xs:element name="available_date" type="available_date_type" minOccurs="1" maxOccurs="1"/>
<xs:element name="property_description" type="xs:string" minOccurs="1" maxOccurs="1"/>
<xs:element name="main_photo_url" type="xs:anyURI" minOccurs="0" maxOccurs="1"/>
<xs:element name="branch_name" type="xs:string" minOccurs="1" maxOccurs="1"/>
<xs:element name="property_type" type="property_type_type" minOccurs="1" maxOccurs="1"/>
<xs:element name="building_name" type="xs:string" minOccurs="0" maxOccurs="1"/>
<xs:element name="street_name" type="xs:string" minOccurs="0" maxOccurs="1"/>
<xs:element name="postcode" type="ukPostCode" minOccurs="0" maxOccurs="1"/>
<xs:element name="furnished" type="xs:boolean" minOccurs="1" maxOccurs="1"/>
<xs:element name="location_area" type="xs:string" minOccurs="0" maxOccurs="1"/>
</xs:all>
<xs:attribute name="agent_advert_reference" type="xs:string"/>
<xs:attribute name="original_html_file" type="xs:string" use="required"/>
<xs:attribute name="static_advert_url" type="xs:string"/>
</xs:complexType>
<xs:element name="rent_frequency" type="rent_frequency_type"/>
<xs:simpleType name="rent_frequency_type">
<xs:restriction base="xs:string">
<xs:enumeration value="per month"/>
<xs:enumeration value="per week"/>
<xs:enumeration value="Per Month"/>
<xs:enumeration value="Per Week"/>
<xs:enumeration value="Per month"/>
<xs:enumeration value="Per week"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="property_type" type="property_type_type"/>
<xs:simpleType name="property_type_type">
<xs:restriction base="xs:string">
<xs:enumeration value="Room"/>
<xs:enumeration value="House"/>
<xs:enumeration value="Flat"/>
<xs:enumeration value="room"/>
<xs:enumeration value="house"/>
<xs:enumeration value="flat"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="property_agent" type="property_agent_type"/>
<xs:complexType name="property_agent_type">
<xs:all>
<xs:element name="company_name" type="xs:string" minOccurs="1" maxOccurs="1"/>
</xs:all>
</xs:complexType>
<xs:simpleType name="available_date_type">
<xs:union>
<xs:simpleType>
<xs:restriction base="xs:date"/>
</xs:simpleType>
<xs:simpleType>
<xs:restriction base="xs:NMTOKEN">
<xs:enumeration value="Now"/>
<xs:enumeration value="now"/>
<xs:enumeration value="NOW"/>
</xs:restriction>
</xs:simpleType>
</xs:union>
</xs:simpleType>
<xs:simpleType name="harvester_version_type">
<xs:restriction base="xs:token">
<xs:pattern value="([0-9]+\.)?([0-9]+\.)?([0-9]+)"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="ukPostCode">
<xs:restriction base="xs:token">
<xs:pattern value="(GIR 0AA)|((([A-Z-[QVX]][0-9][0-9]?)|(([A-Z-[QVX]][A-Z-[IJZ]][0-9][0-9]?)|(([A-Z-[QVX]][0-9][A-HJKPSTUW])|([A-Z-[QVX]][A-Z-[IJZ]][0-9][ABEHMNPRVWXY])))) [0-9][A-Z-[CIKMOV]]{2})"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="header" type="header_type"/>
<xs:complexType name="header_type">
<xs:sequence>
<xs:element name="harvest_run_info" type="harvest_run_info_type" minOccurs="1" maxOccurs="1"/>
<xs:element name="property_agent" type="property_agent_type" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
<xs:element name="property_adverts" type="property_adverts_type"/>
<xs:complexType name="property_adverts_type">
<xs:sequence>
<xs:element name="header" type="header_type" minOccurs="1" maxOccurs="1"/>
<xs:element name="adverts" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
<xs:element name="adverts" type="adverts_type">
<xs:unique name="unique_original_html_file">
<xs:selector xpath="property_advert"/>
<xs:field xpath="@original_html_file"/>
</xs:unique>
<xs:unique name="unique_agent_advert_reference">
<xs:selector xpath="property_advert"/>
<xs:field xpath="@agent_advert_reference"/>
</xs:unique>
<xs:unique name="unique_static_advert_url">
<xs:selector xpath="property_advert"/>
<xs:field xpath="@static_advert_url"/>
</xs:unique>
</xs:element>
<xs:complexType name="adverts_type">
<xs:sequence>
<xs:element name="property_advert" type="property_advert_type" minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
我只使用 XML 架构两天,所以请耐心等待我的新问题。提前致谢。
您的架构中的唯一约束根本没有被考虑,因为在复杂类型 property_adverts_type 中,您使用隐含的复杂类型 xs:anyType 重新定义了一个名为 adverts 的元素(因此,它会接受广告元素下的几乎所有内容)。相反,您应该使用 ref 属性引用广告的全局元素声明。
总之,你需要更换
<xs:element name="adverts" minOccurs="1" maxOccurs="1"/>
和
<xs:element ref="adverts" minOccurs="1" maxOccurs="1"/>
我已经在本地检查过这样做 returns 您的文档存在身份约束错误。
我试图确保 <property_advert>
元素中的属性是唯一的。例如属性 agent_advert_reference
应该是唯一的。我希望下面的 XML 由于重复的属性而失败。
<?xml version="1.0" encoding="UTF-8"?>
<property_adverts xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xs:schemaLocation="http://www.example.org/XmplifyDerivedSchema.xsd">
<header>
<harvest_run_info>
<harvest_time>12:05:00</harvest_time>
<harvest_date>2015-10-10</harvest_date>
<harvester_version>1.0</harvester_version>
<harvester_path>/Users/xandrani/Sites/property_harvester_new</harvester_path>
</harvest_run_info>
<property_agent>
<company_name>CPS Homes</company_name>
</property_agent>
</header>
<adverts>
<property_advert agent_advert_reference="X1204" static_advert_url="https://www.example.com/properties/1204" original_html_file="/RandomName/advert_30Sep2015_14436555622486.html">
<rent_cost>450</rent_cost>
<rent_frequency>per month</rent_frequency>
<number_of_bedrooms>4</number_of_bedrooms>
<available_date>now</available_date>
<property_description></property_description>
<main_photo_url>https://www.example.com/properties/X1204/fred.jpg</main_photo_url>
<branch_name>Brusque Bay</branch_name>
<property_type>House</property_type>
<building_name/>
<street_name>High Street</street_name>
<postcode>CF11 6BP</postcode>
<furnished>true</furnished>
<location_area>Canton</location_area>
</property_advert>
<property_advert agent_advert_reference="X1204" static_advert_url="https://www.example.com/properties/1204" original_html_file="/RandomName/advert_30Sep2015_14436555622486.html">
<rent_cost>450</rent_cost>
<rent_frequency>per month</rent_frequency>
<number_of_bedrooms>4</number_of_bedrooms>
<available_date>now</available_date>
<property_description></property_description>
<main_photo_url>https://www.example.com/properties/X1204/fred.jpg</main_photo_url>
<branch_name>Brusque Bay</branch_name>
<property_type>House</property_type>
<building_name/>
<street_name>High Street</street_name>
<postcode>CF11 6BP</postcode>
<furnished>true</furnished>
<location_area>Canton</location_area>
</property_advert>
</adverts>
</property_adverts>
我的架构如下:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:annotation>
<xs:documentation xml:lang="en">
XML Schema.
</xs:documentation>
</xs:annotation>
<xs:element name="harvest_run_info" type="harvest_run_info_type"/>
<xs:complexType name="harvest_run_info_type">
<xs:all minOccurs="1" maxOccurs="1">
<xs:element name="harvest_time" type="xs:time" minOccurs="1" maxOccurs="1"/>
<xs:element name="harvest_date" type="xs:date" minOccurs="1" maxOccurs="1"/>
<xs:element name="harvester_version" type="harvester_version_type" minOccurs="1" maxOccurs="1"/>
<xs:element name="harvester_path" type="xs:string" minOccurs="1" maxOccurs="1"/>
</xs:all>
</xs:complexType>
<xs:element name="property_advert" type="property_advert_type"/>
<xs:complexType name="property_advert_type">
<xs:all>
<xs:element name="rent_cost" type="xs:positiveInteger" minOccurs="1" maxOccurs="1"/>
<xs:element name="rent_frequency" type="rent_frequency_type" minOccurs="1" maxOccurs="1"/>
<xs:element name="number_of_bedrooms" type="xs:positiveInteger" minOccurs="1" maxOccurs="1"/>
<xs:element name="available_date" type="available_date_type" minOccurs="1" maxOccurs="1"/>
<xs:element name="property_description" type="xs:string" minOccurs="1" maxOccurs="1"/>
<xs:element name="main_photo_url" type="xs:anyURI" minOccurs="0" maxOccurs="1"/>
<xs:element name="branch_name" type="xs:string" minOccurs="1" maxOccurs="1"/>
<xs:element name="property_type" type="property_type_type" minOccurs="1" maxOccurs="1"/>
<xs:element name="building_name" type="xs:string" minOccurs="0" maxOccurs="1"/>
<xs:element name="street_name" type="xs:string" minOccurs="0" maxOccurs="1"/>
<xs:element name="postcode" type="ukPostCode" minOccurs="0" maxOccurs="1"/>
<xs:element name="furnished" type="xs:boolean" minOccurs="1" maxOccurs="1"/>
<xs:element name="location_area" type="xs:string" minOccurs="0" maxOccurs="1"/>
</xs:all>
<xs:attribute name="agent_advert_reference" type="xs:string"/>
<xs:attribute name="original_html_file" type="xs:string" use="required"/>
<xs:attribute name="static_advert_url" type="xs:string"/>
</xs:complexType>
<xs:element name="rent_frequency" type="rent_frequency_type"/>
<xs:simpleType name="rent_frequency_type">
<xs:restriction base="xs:string">
<xs:enumeration value="per month"/>
<xs:enumeration value="per week"/>
<xs:enumeration value="Per Month"/>
<xs:enumeration value="Per Week"/>
<xs:enumeration value="Per month"/>
<xs:enumeration value="Per week"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="property_type" type="property_type_type"/>
<xs:simpleType name="property_type_type">
<xs:restriction base="xs:string">
<xs:enumeration value="Room"/>
<xs:enumeration value="House"/>
<xs:enumeration value="Flat"/>
<xs:enumeration value="room"/>
<xs:enumeration value="house"/>
<xs:enumeration value="flat"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="property_agent" type="property_agent_type"/>
<xs:complexType name="property_agent_type">
<xs:all>
<xs:element name="company_name" type="xs:string" minOccurs="1" maxOccurs="1"/>
</xs:all>
</xs:complexType>
<xs:simpleType name="available_date_type">
<xs:union>
<xs:simpleType>
<xs:restriction base="xs:date"/>
</xs:simpleType>
<xs:simpleType>
<xs:restriction base="xs:NMTOKEN">
<xs:enumeration value="Now"/>
<xs:enumeration value="now"/>
<xs:enumeration value="NOW"/>
</xs:restriction>
</xs:simpleType>
</xs:union>
</xs:simpleType>
<xs:simpleType name="harvester_version_type">
<xs:restriction base="xs:token">
<xs:pattern value="([0-9]+\.)?([0-9]+\.)?([0-9]+)"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="ukPostCode">
<xs:restriction base="xs:token">
<xs:pattern value="(GIR 0AA)|((([A-Z-[QVX]][0-9][0-9]?)|(([A-Z-[QVX]][A-Z-[IJZ]][0-9][0-9]?)|(([A-Z-[QVX]][0-9][A-HJKPSTUW])|([A-Z-[QVX]][A-Z-[IJZ]][0-9][ABEHMNPRVWXY])))) [0-9][A-Z-[CIKMOV]]{2})"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="header" type="header_type"/>
<xs:complexType name="header_type">
<xs:sequence>
<xs:element name="harvest_run_info" type="harvest_run_info_type" minOccurs="1" maxOccurs="1"/>
<xs:element name="property_agent" type="property_agent_type" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
<xs:element name="property_adverts" type="property_adverts_type"/>
<xs:complexType name="property_adverts_type">
<xs:sequence>
<xs:element name="header" type="header_type" minOccurs="1" maxOccurs="1"/>
<xs:element name="adverts" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
<xs:element name="adverts" type="adverts_type">
<xs:unique name="unique_original_html_file">
<xs:selector xpath="property_advert"/>
<xs:field xpath="@original_html_file"/>
</xs:unique>
<xs:unique name="unique_agent_advert_reference">
<xs:selector xpath="property_advert"/>
<xs:field xpath="@agent_advert_reference"/>
</xs:unique>
<xs:unique name="unique_static_advert_url">
<xs:selector xpath="property_advert"/>
<xs:field xpath="@static_advert_url"/>
</xs:unique>
</xs:element>
<xs:complexType name="adverts_type">
<xs:sequence>
<xs:element name="property_advert" type="property_advert_type" minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
我只使用 XML 架构两天,所以请耐心等待我的新问题。提前致谢。
您的架构中的唯一约束根本没有被考虑,因为在复杂类型 property_adverts_type 中,您使用隐含的复杂类型 xs:anyType 重新定义了一个名为 adverts 的元素(因此,它会接受广告元素下的几乎所有内容)。相反,您应该使用 ref 属性引用广告的全局元素声明。
总之,你需要更换
<xs:element name="adverts" minOccurs="1" maxOccurs="1"/>
和
<xs:element ref="adverts" minOccurs="1" maxOccurs="1"/>
我已经在本地检查过这样做 returns 您的文档存在身份约束错误。