我总是觉得有点难以向其他人解释:为什么存在XML命名空间?我们什么时候应该使用它们,何时不应该使用它们 在XML中使用命名空间时常见的陷阱是什么?
另外,它们如何与XML模式相关?XSD架构是否应始终与命名空间相关联?
我经常需要为不同的基于XML的导入例程设计XML模式.很明显,XML模式会随着时间的推移而发展,或者它们可能包含要修复的错误,因此捕获模式的版本并使用某种机制来绑定特定版本非常重要.
目前我有两种情况:
该错误在架构中找到,所有架构实例必须符合固定版本.
模式已升级,应该被视为首选,但也应支持旧模式.
最后,我想出了在架构的命名空间中存储版本信息:
targetNamespace="http://schemas.company.com/Geodesy/2010/River.xsd"
Run Code Online (Sandbox Code Playgroud)
在修复错误时我将其修复到同一个命名空间中,但如果我要升级模式,那么我需要创建一个新的命名空间但添加了升级月份:
targetNamespace="http://schemas.company.com/Geodesy/2010/01/River.xsd"
Run Code Online (Sandbox Code Playgroud)
如果我在一个月内进行了多次升级,那么也只需追加一天:
targetNamespace="http://schemas.company.com/Geodesy/2010/01/17/River.xsd"
Run Code Online (Sandbox Code Playgroud)
你知道更好的方法吗?
问题如下:
我有以下XML片段:
<time format="minutes">11:60</time>
Run Code Online (Sandbox Code Playgroud)
问题是我无法同时添加属性和限制.属性格式只能包含分钟,小时和秒.时间有限制模式\d{2}:\d{2}
<xs:element name="time" type="timeType"/>
...
<xs:simpleType name="formatType">
<xs:restriction base="xs:string">
<xs:enumeration value="minutes"/>
<xs:enumeration value="hours"/>
<xs:enumeration value="seconds"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="timeType">
<xs:attribute name="format">
<xs:simpleType>
<xs:restriction base="formatType"/>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
Run Code Online (Sandbox Code Playgroud)
如果我创建一个复杂类型的timeType,我可以添加一个属性,但不能添加限制,如果我创建一个简单类型,我可以添加限制但不添加属性.有没有办法解决这个问题.这不是一个非常奇怪的限制,或者是它?
在XML模式中,您可以将元素标记为nillable可以采用显式NULL值.有关详细说明,请参阅nillable和minOccurs XSD元素属性.
我很好奇的是为什么叫它nillable?我总是看到nillable并认为这是一个错字!
编辑
我很欣赏这nil是一个同义词null.我想知道的是为什么nil被选中,而不是更常见(在计算机科学中)null.特别是它应该是真的nilable(注意单L)!
我有一个DTD,我需要转换为XSD(XML架构)文件.有没有免费的实用工具或简单的方法来实现这一目标?
使用JAXB生成XML绑定类.
该架构基于一组遗留XML文件,并包含以下代码段:
<xs:complexType name="MetaType">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:string" name="Name" />
<xs:attribute type="xs:string" name="Scheme" />
<xs:attribute type="xs:string" name="Value" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
Run Code Online (Sandbox Code Playgroud)
'Value'属性与'value'属性冲突,xs:string代码生成失败并出现错误:
com.sun.istack.SAXParseException2: Property "Value" is already defined. Use <jaxb:property> to resolve this conflict.
Run Code Online (Sandbox Code Playgroud) Eclipse 3.5.2抛出了一个XML模式警告消息:
No grammar constraints (DTD or XML schema) detected for the document.
Run Code Online (Sandbox Code Playgroud)
application.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<application
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/application_5.xsd"
version="5">
</application>
Run Code Online (Sandbox Code Playgroud)
我不想禁用警告.如何让Eclipse正确验证XML文档?
是否有YAML的架构验证语言?我用谷歌搜索,但找不到任何有用的东西.
XSD格式之类的东西,使用语言本身来描述模式,在我的情况下是最好的选择.
有没有办法指定XSD中需要2个属性中的一个?
例如,我有这样的定义:
<xs:attribute name="Name" type="xs:string" use="optional" />
<xs:attribute name="Id" type="xs:string" use="optional" />
Run Code Online (Sandbox Code Playgroud)
我希望能够定义至少需要其中一个.那可能吗?
以下帖子询问如何指示元素是XML模式中的根元素:
我已经关注了XML Schema的w3schools教程,但有些事情仍然不明确.考虑来自http://www.w3schools.com/schema/schema_example.asp的示例模式2
(为方便起见,在下面转载).这段代码如何表明这<shiporder>
是根元素?是不是说所有元素都作为根元素有效的例子?
------------------ instance ------------------------------- ---
<?xml version="1.0" encoding="ISO-8859-1"?>
<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</xample saying that all elements are valid as root elements?quantity>
<price>9.90</price>
</item>
</shiporder>
Run Code Online (Sandbox Code Playgroud)
----------------------- schema ------------------------
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- definition of simple elements -->
<xs:element name="orderperson" type="xs:string"/>
<xs:element name="name" type="xs:string"/>
<xs:element name="address" type="xs:string"/> …Run Code Online (Sandbox Code Playgroud) xsd ×10
xml ×4
java ×2
schema ×2
dtd ×1
eclipse ×1
java-ee ×1
jaxb ×1
namespaces ×1
restriction ×1
validation ×1
versioning ×1
yaml ×1