我如何在浏览器中验证 XML ,否则我应该怎么做?
How can I validate XML in the browser, or how should I do it otherwise?
我不熟悉 xsd,我从 w3schools 复制代码,
<shiporder orderid="889923"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="shiporder.xsd">
<orderperson>John Smith</orderperson>
<shipto>
<name>Ola Nordmann</name>
<address>Langgt 23</address>
<city>4000 Stavanger</city>
<country>Norway</country>
</shipto>
<item>
<title>Empire Burlesque</title>
<note>Special Edition</note>
<quantity>1</quantity>
<price>10.90</price>
</item>
<item>
<title>Hide your heart</title>
<quantity>1</quantity>
<price>9.90</price>
</item>
</shiporder>
xsd
<xs:element name="shiporder">
<xs:complexType>
<xs:sequence>
<xs:element name="orderperson" type="xs:string"/>
<xs:element name="shipto">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="item" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="note" type="xs:string" minOccurs="0"/>
<xs:element name="quantity" type="xs:positiveInteger"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="orderid" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
我将项目的 maxOccurs 更改为 1
<xs:element name="item" maxOccurs="1">
i 运行浏览器中的xml(两项),它仍然打印出xml,那么xsd的用法是什么?
i run the xml in the browser (two items), it still print out the xml, so what is the usage of the xsd?
XSD 或 XML 模式定义,为 XML 提供定义。要利用这样的定义,您必须使用验证处理器,例如 Xerces(存在许多其他处理器)。
它的典型用例是在两方之间交换数据。为了确保数据符合他们约定的格式,他们用某种模式语言描述了 XML 结构的格式:
- DTD, dtd,这是 XML 本身的一部分
- XSD, xsd,可以说是最常用的标准
- RelaxNG, relax-ng,一种流行且更灵活的 XSD
替代方法
- Schematron, schematron,一种基于规则的验证语言
最常见的情况是,您会在使用 SOAP 协议编写 Web 服务时发现这种情况,该协议通常在 XSD 中定义其类型。如果通过其定义文件(通常是 WSDL)导入 Web 服务,它可以自动转换为现成的 POJO 或 POCO 类(代表对象模型的类),即对创建业务逻辑有很大帮助。
对于任何有数据发送端和接收端的场景,如果接收方能够在继续之前验证 XML 是否符合模式中定义的数据模型,那将是一个很大的优势。假设您有一个字段 birth-date
,并且发送方发送 "fourth of july 1977"
,验证器可以引发错误,警告用户出现问题并允许系统 gracefully degrade.
浏览器与模式无关,至少在向用户报告它的意义上是这样,因为他们试图在接受的内容上保持自由,因为很多人创建 HTML 非-符合,但仍应以某种方式显示。
另一个明显的用途是标准化。例如,DocBook 格式 is defined in RelaxNG as is the OpenDocument format. These are defined in XSD: XSLT (albeit non-normative), XSD itself, WSDL, SMIL, all Office Open XML formats, all parts of SOAP (here is the envelope schema)。等等,名单很长。
我不熟悉 xsd,我从 w3schools 复制代码,
<shiporder orderid="889923"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="shiporder.xsd">
<orderperson>John Smith</orderperson>
<shipto>
<name>Ola Nordmann</name>
<address>Langgt 23</address>
<city>4000 Stavanger</city>
<country>Norway</country>
</shipto>
<item>
<title>Empire Burlesque</title>
<note>Special Edition</note>
<quantity>1</quantity>
<price>10.90</price>
</item>
<item>
<title>Hide your heart</title>
<quantity>1</quantity>
<price>9.90</price>
</item>
</shiporder>
xsd
<xs:element name="shiporder">
<xs:complexType>
<xs:sequence>
<xs:element name="orderperson" type="xs:string"/>
<xs:element name="shipto">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="item" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="note" type="xs:string" minOccurs="0"/>
<xs:element name="quantity" type="xs:positiveInteger"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="orderid" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
我将项目的 maxOccurs 更改为 1
<xs:element name="item" maxOccurs="1">
i 运行浏览器中的xml(两项),它仍然打印出xml,那么xsd的用法是什么?
i run the xml in the browser (two items), it still print out the xml, so what is the usage of the xsd?
XSD 或 XML 模式定义,为 XML 提供定义。要利用这样的定义,您必须使用验证处理器,例如 Xerces(存在许多其他处理器)。
它的典型用例是在两方之间交换数据。为了确保数据符合他们约定的格式,他们用某种模式语言描述了 XML 结构的格式:
- DTD, dtd,这是 XML 本身的一部分
- XSD, xsd,可以说是最常用的标准
- RelaxNG, relax-ng,一种流行且更灵活的 XSD 替代方法
- Schematron, schematron,一种基于规则的验证语言
最常见的情况是,您会在使用 SOAP 协议编写 Web 服务时发现这种情况,该协议通常在 XSD 中定义其类型。如果通过其定义文件(通常是 WSDL)导入 Web 服务,它可以自动转换为现成的 POJO 或 POCO 类(代表对象模型的类),即对创建业务逻辑有很大帮助。
对于任何有数据发送端和接收端的场景,如果接收方能够在继续之前验证 XML 是否符合模式中定义的数据模型,那将是一个很大的优势。假设您有一个字段 birth-date
,并且发送方发送 "fourth of july 1977"
,验证器可以引发错误,警告用户出现问题并允许系统 gracefully degrade.
浏览器与模式无关,至少在向用户报告它的意义上是这样,因为他们试图在接受的内容上保持自由,因为很多人创建 HTML 非-符合,但仍应以某种方式显示。
另一个明显的用途是标准化。例如,DocBook 格式 is defined in RelaxNG as is the OpenDocument format. These are defined in XSD: XSLT (albeit non-normative), XSD itself, WSDL, SMIL, all Office Open XML formats, all parts of SOAP (here is the envelope schema)。等等,名单很长。