Xsd 强制元素为XML模式中的特定类型,但不包括任何名称
我可以在xsd中描述某些特定类型的元素必须出现,但它们可以有任何名称。 Visual studio不允许我省略“name”属性,如下所示:Xsd 强制元素为XML模式中的特定类型,但不包括任何名称,xsd,Xsd,我可以在xsd中描述某些特定类型的元素必须出现,但它们可以有任何名称。 Visual studio不允许我省略“name”属性,如下所示: <xs:element type="myType"/> 有办法做到这一点吗?如果不是,那么在其他XML模式语言(如RELAXNG)中是否可能实现? 谢谢XSD 1.0中的一种方法是使用替换组 BaseAny.xsd <?xml version="1.0" encoding="utf-8" ?> <xsd:schema ta
<xs:element type="myType"/>
有办法做到这一点吗?如果不是,那么在其他XML模式语言(如RELAXNG)中是否可能实现?
谢谢XSD 1.0中的一种方法是使用替换组 BaseAny.xsd
<?xml version="1.0" encoding="utf-8" ?>
<xsd:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="anyOfCType" type="myCType" abstract="true"/>
<xsd:complexType name="myCType">
<xsd:sequence>
<xsd:element name="something" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="anyOfSType" type="mySType" abstract="true"/>
<xsd:simpleType name="mySType">
<xsd:restriction base="xsd:string">
<xsd:minLength value="2"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:element name="root">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="anyOfCType"/>
<xsd:element ref="anyOfSType"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
RefBaseAny.xsd:
<?xml version="1.0" encoding="utf-8" ?>
<xsd:schema targetNamespace="http://tempuri.org/1/XMLSchema.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/1/XMLSchema.xsd" xmlns:b="http://tempuri.org/XMLSchema.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:import namespace="http://tempuri.org/XMLSchema.xsd" schemaLocation="BaseAny.xsd"/>
<xsd:element name="c.one" type="b:myCType" substitutionGroup="b:anyOfCType"/>
<xsd:element name="s.one" type="b:mySType" substitutionGroup="b:anyOfSType"/>
</xsd:schema>
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:r="http://tempuri.org/1/XMLSchema.xsd">
<r:c.one>
<something></something>
</r:c.one>
<r:s.one>12</r:s.one>
</root>
针对RefBaseAny.xsd进行验证的示例XML:
<?xml version="1.0" encoding="utf-8" ?>
<xsd:schema targetNamespace="http://tempuri.org/1/XMLSchema.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/1/XMLSchema.xsd" xmlns:b="http://tempuri.org/XMLSchema.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:import namespace="http://tempuri.org/XMLSchema.xsd" schemaLocation="BaseAny.xsd"/>
<xsd:element name="c.one" type="b:myCType" substitutionGroup="b:anyOfCType"/>
<xsd:element name="s.one" type="b:mySType" substitutionGroup="b:anyOfSType"/>
</xsd:schema>
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:r="http://tempuri.org/1/XMLSchema.xsd">
<r:c.one>
<something></something>
</r:c.one>
<r:s.one>12</r:s.one>
</root>
12
使用RELAXNG,您必须定义一个命名模式来定义您的内容模型(模仿xsd:complexType)
...
然后定义具有此结构的元素:
<element>
<anyName/>
<ref name="myType"/>
</element>
Test.rng:
<?xml version="1.0" encoding="UTF-8"?>
<grammar xmlns="http://relaxng.org/ns/structure/1.0">
<start>
<element>
<anyName/>
<ref name="myType"/>
</element>
</start>
<define name="myType">
<element name="c.one">
<interleave>
<attribute name="id">
<value>AA</value>
</attribute>
<attribute name="ref">
<text/>
</attribute>
</interleave>
<element name="s.two">
<text/>
</element>
</element>
</define>
</grammar>
AA
有效XML1:
<?xml version="1.0" encoding="UTF-8"?>
<Data>
<c.one id="AA" ref="123">
<s.two>Hello</s.two>
</c.one>
</Data>
你好
有效XML2:
<?xml version="1.0" encoding="UTF-8"?>
<Data1>
<c.one id="AA" ref="123">
<s.two>Hello</s.two>
</c.one>
</Data1>
你好
非常感谢,先生,RELAX NG解决方案正是我所需要的。但这在XSD中不可能给出任何名称吗?我发现XSD非常有限。他们首先想到的是XSD的实现者,而不是用户…@RoadBump,在XSD 1.0中,任何名称都支持通配符形式(XSD:any表示元素,XSD:anyAttribute表示属性);但是,只能对符合条件的名称空间和用于验证的处理模型进行限制—请确保与所请求的类型无关。有一个新的规范,最近批准,XSD 1.1;在这个时候,我不会对此发表评论,如果只是因为没有便宜的解析器可供所有人使用…@PetruGardea你能检查一下吗