Without going into too much detail we are looking to use XML as meta-data to describe constraints on properties (This is a cutdown example and XSD did not support our proposed complex model), there are two options being considered, which of the following XML strucutures makes better sense?
在没有详细介绍的情况下,我们希望使用XML作为元数据来描述属性约束(这是一个缩减示例,XSD不支持我们提出的复杂模型),有两个选项正在考虑,以下哪个XML结构更有意义吗?
Option 1)
<?xml version="1.0" encoding="us-ascii"?>
<Properties xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Property type="string">
<name>quanitity</name>
<contraints>
<contraint type="isRequired">
<value>true</value>
</contraint>
<contraint type="regex">
<value>^[0-9]$</value>
</contraint>
<contraint type="regex">
<value>^[a-zA-Z]$</value>
</contraint>
</contraints>
</Property>
</Properties>
Option 2)
<?xml version="1.0" encoding="us-ascii"?>
<Properties xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Property type="string">
<name>quantity</name>
<IsRequired>true</IsRequired>
<Regex>^[0-9]$</Regex>
<Regex>^[a-zA-Z]$</Regex>
</Property>
</Properties>
4 个解决方案
#1
I would probably go for option #2 as the XML job does a better job of describing the data (which is after all what XML is supposed to do), and is therefore easier to read and less verbose. Option #1 is a little close to having <name>
and <value>
tags for my liking.
我可能会选择#2选项,因为XML作业可以更好地描述数据(这毕竟是XML应该做的事情),因此更易于阅读和更简洁。选项#1与我喜欢的
#2
For flexibility, I would go with #1. This will allow you to add many different types of constraints and custom rules.
为了灵活性,我会选择#1。这将允许您添加许多不同类型的约束和自定义规则。
#3
It depends on how you want to parse/process the XML and if you want to be able to validate it.
这取决于您希望如何解析/处理XML以及是否希望能够验证它。
From a validation point of view (if you are considering automatic validation via an XSD at compile time) Option 2 is the better choice, as the content of the tags will have a certain type (e.g., a regular expression), while the <value>
tag could not be checked for correct content.
从验证的角度来看(如果您正在考虑在编译时通过XSD进行自动验证),选项2是更好的选择,因为标签的内容将具有某种类型(例如,正则表达式),而 <值无法检查> 标记的内容是否正确。
Also, I would probably go with Option 2 concerning the parsing and processing if you are using a SAX parser, as you could switch states based on the tags.
此外,如果您使用的是SAX解析器,我可能会选择有关解析和处理的选项2,因为您可以根据标记切换状态。
#4
Personally, I'd go with the first one, I suspect it'd be easier to write code to parse and do error checking in. Plus it makes more sense semantically to my brain.
就个人而言,我会选择第一个,我怀疑编写代码进行解析并进行错误检查会更容易。另外,对于我的大脑来说,语义更有意义。
That said, I think you'll find it's 'personal choice', either is fine.
也就是说,我认为你会发现这是'个人选择',要么就是好的。
#1
I would probably go for option #2 as the XML job does a better job of describing the data (which is after all what XML is supposed to do), and is therefore easier to read and less verbose. Option #1 is a little close to having <name>
and <value>
tags for my liking.
我可能会选择#2选项,因为XML作业可以更好地描述数据(这毕竟是XML应该做的事情),因此更易于阅读和更简洁。选项#1与我喜欢的
#2
For flexibility, I would go with #1. This will allow you to add many different types of constraints and custom rules.
为了灵活性,我会选择#1。这将允许您添加许多不同类型的约束和自定义规则。
#3
It depends on how you want to parse/process the XML and if you want to be able to validate it.
这取决于您希望如何解析/处理XML以及是否希望能够验证它。
From a validation point of view (if you are considering automatic validation via an XSD at compile time) Option 2 is the better choice, as the content of the tags will have a certain type (e.g., a regular expression), while the <value>
tag could not be checked for correct content.
从验证的角度来看(如果您正在考虑在编译时通过XSD进行自动验证),选项2是更好的选择,因为标签的内容将具有某种类型(例如,正则表达式),而 <值无法检查> 标记的内容是否正确。
Also, I would probably go with Option 2 concerning the parsing and processing if you are using a SAX parser, as you could switch states based on the tags.
此外,如果您使用的是SAX解析器,我可能会选择有关解析和处理的选项2,因为您可以根据标记切换状态。
#4
Personally, I'd go with the first one, I suspect it'd be easier to write code to parse and do error checking in. Plus it makes more sense semantically to my brain.
就个人而言,我会选择第一个,我怀疑编写代码进行解析并进行错误检查会更容易。另外,对于我的大脑来说,语义更有意义。
That said, I think you'll find it's 'personal choice', either is fine.
也就是说,我认为你会发现这是'个人选择',要么就是好的。