XSD 对具有跳过行为的属性进行版本控制
我希望能够对我的 XML 文件进行版本控制。我希望旧的 xsd 文件能够验证收到的 XML 文件的新版本。
为此,我将使用版本属性保护未知的 xml 标记。
问题:如何让 xsd 根据版本属性跳过部分 XML?在下面的示例中,我希望 xsd 能够验证版本 1 和 2 但不验证版本 3 的任何标签。
场景:
<MYXML>
<SOME_XML version="1">
<SOME_VERSION_1_DATA>this_is_data_only_for_version_1</SOME_VERSION_1_DATA>
</SOME_XML>
<SOME_XML version="2">
<SOME_VERSION_2_DATA>this_is_data_only_for_version_2</SOME_VERSION_2_DATA>
</SOME_XML>
<SOME_XML version="3">
<SOME_VERSION_3_DATA>this_is_data_only_for_version_3</SOME_VERSION_3_DATA>
</SOME_XML>
</MYXML>
<xs:complexType name="SOME_XML">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="1" name="SOME_VERSION_1_DATA" type="xs:string"/>
<xs:element minOccurs="0" maxOccurs="1" name="SOME_VERSION_2_DATA" type="xs:string"/>
</xs:sequence>
<xs:attribute ref="version"/>
</xs:complexType>
<xs:attribute name="version">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:enumeration value="1"/>
<xs:enumeration value="2"/>
</xs:restriction>
<xs:anyAttribute processContents="skip"/> <-- SKIP the attribute if NOT 1 ro 2?
</xs:simpleType>
</xs:attribute>
I want to be able to version control my XML file. I want an older xsd file to be able to validate newer versions of the XML file received.
To do this I will protect unknown xml tag with version attributes.
The question: how do I get the xsd to skip part of the XML based on a version attribute? In the example below I want the xsd to be able to validate any tags with version 1 and 2 but not 3.
The scenario:
<MYXML>
<SOME_XML version="1">
<SOME_VERSION_1_DATA>this_is_data_only_for_version_1</SOME_VERSION_1_DATA>
</SOME_XML>
<SOME_XML version="2">
<SOME_VERSION_2_DATA>this_is_data_only_for_version_2</SOME_VERSION_2_DATA>
</SOME_XML>
<SOME_XML version="3">
<SOME_VERSION_3_DATA>this_is_data_only_for_version_3</SOME_VERSION_3_DATA>
</SOME_XML>
</MYXML>
<xs:complexType name="SOME_XML">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="1" name="SOME_VERSION_1_DATA" type="xs:string"/>
<xs:element minOccurs="0" maxOccurs="1" name="SOME_VERSION_2_DATA" type="xs:string"/>
</xs:sequence>
<xs:attribute ref="version"/>
</xs:complexType>
<xs:attribute name="version">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:enumeration value="1"/>
<xs:enumeration value="2"/>
</xs:restriction>
<xs:anyAttribute processContents="skip"/> <-- SKIP the attribute if NOT 1 ro 2?
</xs:simpleType>
</xs:attribute>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定如何直接回答您的问题,即使用跳过 XML 中的特定详细信息。
如果您的环境可行,我建议 xslt 转换原始 XML,其中版本 3 文件将被跳过,并验证 XLST 生成的输出。
XSLT 复制整个文件,同时丢弃 @atribute version == 3 的节点
注意,XSLT 按原样编写,未针对给定输入进行测试。
I'm not sure how to directly answer your problem, the use of skipping specific details from the XML.
If it's possible in your environment, I suggest xslt to transform original XML where version 3 files will be skipped, and validated XLST generated output.
XSLT to copy entire file while discarding nodes with @atribute version == 3
note, XSLT written as is, it wasn't tested for given input.
我不会为此使用属性,因为属性不能“改变”内容模型。如果您考虑使您的应用程序以“向前兼容”的方式处理 XML(即使用旧的 XSD 来处理新内容),那么我宁愿使用强制版本元素,后跟可选的 xsd:any - 全部与一些混合设计规则。
长话短说,您需要这种设置才能处理唯一的粒子属性约束(或者换句话说,解析器无法向前确定它在 XSD 中的“位置”的约束) 。
最初,您甚至可以在没有强制版本标签的情况下开始,只需在可扩展性“位置”所在的位置粘贴可选的 xsd:any 即可。随着后续版本精炼内容,您可能必须添加此强制版本标记(同样是为了处理 UPA),然后是精炼内容,最后是可选的 xsd:any。
一般来说,还需要考虑其他事情,例如在 XSD 中使用类型扩展/限制以及它可能对前向兼容性方案(例如我上面刚刚描述的方案)产生的影响(请阅读这个)。
I wouldn't use attributes for this since attributes cannot "alter" content models. If you think about making your application that is processing XML in a "forward compatible" way (i.e. use old XSDs for new content), then I would rather use a mandatory version element, followed by an optional xsd:any - all mixed with some design rules.
To make a long story short, you will need this kind of setup to be able to handle the unique particle attribution constraint (or in other words, the constraint that a parser cannot look ahead to figure out "where" it is in the XSD).
Initially you can even start without a mandatory version tag, just stick an optional xsd:any where your extensibility "place" is. As subsequent versions refine content, you may have to add this mandatory version marker (again, to deal with UPA), followed by refined content, followed again by optional xsd:any.
In general, there are other things to consider as well, such as using type extensions/restrictions in your XSD and the impact it may have on forward compatibility schemes such as the one I've just described above (please read this on SO).