如何在xsd中约束属性集
全部 请建议我如何在xsd模式中限制以下内容:如何在xsd中约束属性集,xsd,Xsd,全部 请建议我如何在xsd模式中限制以下内容: <root> <node action="action1" parameter="1" /> </root> 仅当定义了属性“action”时,我才需要属性“parameter” 谢谢,W3C模式没有能力表达有条件地需要的属性。 是一个很好的工具,用于验证文档是否符合有条件地需要内容的自定义验证场景 您可以在模式中将这些属性定义为可选属性,然后使用Schematron根据这些条件规则对其进行验证。我创建
<root>
<node action="action1" parameter="1" />
</root>
仅当定义了属性“action”时,我才需要属性“parameter”
谢谢,W3C模式没有能力表达有条件地需要的属性。 是一个很好的工具,用于验证文档是否符合有条件地需要内容的自定义验证场景
您可以在模式中将这些属性定义为可选属性,然后使用Schematron根据这些条件规则对其进行验证。我创建这个xsd是为了尝试解决这个问题
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="XMLSchema1"
targetNamespace="http://tempuri.org/XMLSchema1.xsd"
elementFormDefault="qualified"
xmlns="http://tempuri.org/XMLSchema1.xsd"
xmlns:mstns="http://tempuri.org/XMLSchema1.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
<xs:group name="populated">
<xs:sequence >
<xs:element name="node">
<xs:complexType>
<xs:attributeGroup ref="actionattrib" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:group>
<xs:group name="unpopulated">
<xs:sequence >
<xs:element name="node">
<xs:complexType>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:group>
<xs:attributeGroup name ="actionattrib">
<xs:attribute name="action1" type="xs:string" />
<xs:attribute name="parameter" type="xs:int" />
</xs:attributeGroup>
<xs:element name="root">
<xs:complexType>
<xs:choice minOccurs ="0">
<xs:group ref="populated" />
<xs:group ref ="unpopulated" />
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
最后在验证时出现此异常:
元素的多重定义http://tempuri.org/XMLSchema1.xsd:node'导致内容模型变得不明确。必须形成内容模型,以便在验证元素信息项序列期间,直接、间接或隐式包含在其中的粒子可以唯一地确定,从而尝试依次验证序列中的每个项,而无需检查该项的内容或属性,并且没有关于序列剩余部分中项目的任何信息
这与Mads的结果相符。他的回答应该被接受
public static void Go()
{
string nameSpace = "http://tempuri.org/XMLSchema1.xsd";
XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add(nameSpace, "XMLSchema1.xsd");
XDocument myDoc1 = new XDocument(
new XElement(XName.Get("root", nameSpace),
new XElement( XName.Get("node", nameSpace ))
)
);
myDoc1.Validate(schemas, (o, e) => { Console.WriteLine(e.Message); });
}