XSD模式没有按预期工作

本文关键字:工作 模式 XSD | 更新日期: 2023-09-27 18:17:57

我已经创建了这个xsd模式:

 <?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="stringtype">
  <xs:restriction base="xs:string"/>
</xs:simpleType>
<xs:simpleType name="inttype">
  <xs:restriction base="xs:positiveInteger"/>
</xs:simpleType>
<xs:simpleType name="dectype">
  <xs:restriction base="xs:decimal"/>
</xs:simpleType>
<xs:complexType name="RelativeText">
    <xs:attribute name="name" type="stringtype" use="required"/>
    <xs:attribute name="flow" type="stringtype" use="required"/>
    <xs:attribute name="amount" type="inttype"  use="required"/>
</xs:complexType>
<xs:complexType name="LineText">
    <xs:attribute name="name" type="stringtype" use="required"/>
</xs:complexType>
<xs:complexType name="BoxText">
    <xs:attribute name="width" type="dectype" use="required" />
    <xs:attribute name="height" type="dectype" use="required" />
    <xs:attribute name="x" type="dectype" use="required" />
    <xs:attribute name="y" type="dectype" use="required" />
</xs:complexType> 
<xs:complexType name="templatecontenttype">
  <xs:sequence>
    <xs:element name="line-text"        type="LineText" minOccurs="0" maxOccurs="unbounded"/>
    <xs:element name="box-text"         type="BoxText"  minOccurs="0" maxOccurs="unbounded"/> 
    <xs:element name="relative-text"    type="RelativeText" minOccurs="0" maxOccurs="unbounded"/>
  </xs:sequence>
  <xs:attribute name="output-directory" type="stringtype" use="required"/>
</xs:complexType>
<xs:element name="template-content" type="templatecontenttype"  />
</xs:schema>

对于这个xml:

    <?xml version='1.0'?>  
  <template-content output-directory='D:''output'>
<line-text name='a' />
<relative-text name='b' flow='above' amount='1'/>
<box-text name='c' x='1' y='2' width='2' height='2' />
</template-content>

上面写着:

Line: 5, Position: 2 "元素'template-content'的子元素'box-tex '无效t’。期望的可能元素列表:'relative-text'。"

c#代码:

 XmlWriterSettings ws = new XmlWriterSettings();
            ws.Indent = true;
            XmlReaderSettings rs = new XmlReaderSettings();
            rs.ValidationType = ValidationType.Schema;
            rs.ValidationEventHandler += new System.Xml.Schema.ValidationEventHandler(rs_ValidationEventHandler);
            rs.Schemas.Add(null, xsdFilePath);
            rs.CloseInput = true;
             rs.ValidationFlags =
                            XmlSchemaValidationFlags.ReportValidationWarnings |
                            XmlSchemaValidationFlags.ProcessIdentityConstraints |
                            XmlSchemaValidationFlags.ProcessInlineSchema |
                            XmlSchemaValidationFlags.ProcessSchemaLocation;
            StringReader r = new StringReader(xmlString);
            using (XmlReader reader = XmlReader.Create(r, rs))
            {
                // Parse the file and display each of the nodes.
                while (reader.Read())
                {
                    try
                    {
                        switch (reader.NodeType)
                        {
                            case XmlNodeType.Element:
                                {
                                    if (reader.Name == "relative-text")
                                    {
                                        //Console.WriteLine("we found custom-text");
                                        //Console.WriteLine(reader["name"]);
                                        //Console.WriteLine(reader["flow"]);
                                        //Console.WriteLine(reader["amount"]);
                                    }
                                    else if (reader.Name == "line-text")
                                    {
                                       // Console.WriteLine(reader["names"]);
                                    }
                                    else if (reader.Name == "box-text")
                                    {
                                        //Console.WriteLine("x" + reader["x"]);
                                        //Console.WriteLine("y" + reader["y"]);
                                        //Console.WriteLine("width" + reader["width"]);
                                        //Console.WriteLine("height" + reader["height"]);
                                    }
                                }
                                break;
                            case XmlNodeType.Text:
                                break;
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                }
            }

我做错了什么?

XSD模式没有按预期工作

元素顺序错误。您已将顺序定义为line-text, box-textrelative-text,而不是示例中的line-text, relative-text, box-text

所以要么把你的模板xml改成:

<?xml version='1.0'?>  
<template-content output-directory='D:''output'>
    <line-text name='a' />
    <box-text name='c' x='1' y='2' width='2' height='2' />
    <relative-text name='b' flow='above' amount='1'/>
</template-content>

或者在模式中使用<xs:all />而不是<xs:sequence />

<xs:all>
    <xs:element name="line-text" type="LineText" minOccurs="0" maxOccurs="unbounded"/>
    <xs:element name="box-text" type="BoxText"  minOccurs="0" maxOccurs="unbounded"/> 
    <xs:element name="relative-text" type="RelativeText" minOccurs="0" maxOccurs="unbounded"/>
</xs:all>  

编辑
我想我误解了你的图式。对于<xs:all />,它将允许每个元素以任意顺序出现一个。但从你的模式来看,你似乎想要任意数量的元素以任意顺序排列。为此,您必须使用<xs:choice maxOccurs="unbound" />

<xs:choice maxOccurs="unbound">
    <xs:element name="line-text" type="LineText" />
    <xs:element name="box-text" type="BoxText" />
    <xs:element name="relative-text" type="RelativeText" />
</xs:choice>