<suite>
<testcase>...</testcase>
<testpackage>...</testpackage>
</suite>一个suite节点下必须有一个到多个的testcase或者testpackage
如何用定制这个需求的XMLSchema?

解决方案 »

  1.   


    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified" xmlns="http://tempuri.org">
    <xs:element name="suite">
    <xs:complexType>
    <xs:sequence>
    <xs:element name="testcase" minOccurs="1" maxOccurs="unbounded"></xs:element>
    <xs:element name="testpackage" minOccurs="1" maxOccurs="unbounded"></xs:element>
    </xs:sequence>
    </xs:complexType>
    </xs:element>
    </xs:schema>
      

  2.   

    如果你还要在xml中引用xsd进行验证,就必须增加一个属性<?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org" elementFormDefault="qualified" attributeFormDefault="unqualified" targetNamespace="http://hmsuccess.com.cn">
    <xs:element name="suite">
    <xs:complexType>
    <xs:sequence>
    <xs:element name="testcase" maxOccurs="unbounded"/>
    <xs:element name="testpackage" maxOccurs="unbounded"/>
    </xs:sequence>
    </xs:complexType>
    </xs:element>
    </xs:schema>
      

  3.   

    <?xml version="1.0" encoding="UTF-8"?>
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <xsd:complexType name="SuiteType">
        <xsd:sequence>
          <xsd:element name="testcase" minOccurs="1" maxOccurs="unbounded" type="TestCaseType"/>
          <xsd:element name="testpackage" minOccurs="1" maxOccurs="unbounded" type="TestCaseType"/>
        </xsd:sequence>
        <xsd:attribute name="name" type="xsd:normalizedString" use="required"/>
        <xsd:attribute name="class" type="xsd:normalizedString" use="required"/>
      </xsd:complexType>
      <xsd:element name="suite" type="SuiteType"/>
    </xsd:schema>根据你的内容我的xmlschema的格式是这样子的,不过好像我的xml文档当只有testpackage时通不过验证。
    以下是我的XML文档,我希望的是在这种文档中,testpackage和testcase的节点是等同的。
    suite下面至少要有一getestpackage或者一个testcase的节点。<?xml version="1.0" encoding="UTF-8"?>
    <suite name="sample" class="sample.FirstSuite">
    <testpackage name="Test Case1">
    <method name="testMethod1">
    <param type="int">1</param>
    <expected type="int">1</expected>
    </method>
    <method name="testMethod1">
    <param type="int">0</param>
    <expected>-1</expected>
    </method>
    </testpackage>
    <testpackage name="Test Case2">
    <method name="testMethod1">
    <expected type="int">1</expected>
    </method>
    </testpackage>
    </suite>
      

  4.   

    使得一个testpackage或者testcase下也是必须有至少一个的method的<?xml version="1.0" encoding="UTF-8"?>
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <xsd:complexType name="VariableType">
        <xsd:simpleContent>
          <xsd:extension base="xsd:string">
            <xsd:attribute name="type" type="xsd:normalizedString" use="optional"/>
            <xsd:attribute name="handler" type="xsd:normalizedString" use="optional"/>
          </xsd:extension>
        </xsd:simpleContent>
      </xsd:complexType>  <xsd:complexType name="ParameterType">
        <xsd:simpleContent>
          <xsd:extension base="VariableType">
            <xsd:attribute name="name" type="xsd:normalizedString" use="optional"/>
          </xsd:extension>
        </xsd:simpleContent>
      </xsd:complexType>
      
      <xsd:complexType name="MethodType">
        <xsd:sequence>
          <xsd:element name="param" minOccurs="0" maxOccurs="unbounded" type="ParameterType"/>
          <xsd:choice>
            <xsd:element name="expected" minOccurs="1" type="VariableType"/>
            <xsd:element name="exception" minOccurs="1" type="VariableType"/>
          </xsd:choice>
        </xsd:sequence>
        <xsd:attribute name="name" type="xsd:normalizedString" use="required"/>
      </xsd:complexType>  <xsd:complexType name="TestCaseType">
        <xsd:sequence>
          <xsd:element name="method" minOccurs="1" maxOccurs="unbounded" type="MethodType"/>
        </xsd:sequence>
        <xsd:attribute name="name" type="xsd:normalizedString" use="required"/>
      </xsd:complexType>
      
      <xsd:complexType name="SuiteType">
        <xsd:sequence>
           <xsd:element name="testcase" minOccurs="1" maxOccurs="unbounded" type="TestCaseType"/>
           <xsd:element name="testpackage" minOccurs="1" maxOccurs="unbounded" type="TestCaseType"/>
        </xsd:sequence>
        <xsd:attribute name="name" type="xsd:normalizedString" use="required"/>
        <xsd:attribute name="class" type="xsd:normalizedString" use="required"/>
      </xsd:complexType>
      <!--
        The root element for the XML spec.
      -->
      <xsd:element name="suite" type="SuiteType"/>
    </xsd:schema>上面的testpackage和testcase出的定义有问题。不知道应该改成什么。
      

  5.   

    什么问题,我测试一下你的xml文件是好的<?xml version="1.0" encoding="UTF-8"?>
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns="http://www.w3schools.com" elementFormDefault="qualified" 
    attributeFormDefault="unqualified" 
    targetNamespace="http://www.w3schools.com">
      
      <xsd:element name="suite" type="SuiteType"></xsd:element>
      
      <xsd:complexType name="SuiteType">
        <xsd:choice>
          <xsd:element name="testcase" minOccurs="1" maxOccurs="unbounded" type="TestCaseType"/>
          <xsd:element name="testpackage" minOccurs="1" maxOccurs="unbounded" type="TestCaseType"/>
        </xsd:choice>
        <xsd:attribute name="name" type="xsd:normalizedString" use="required"/>
        <xsd:attribute name="class" type="xsd:normalizedString" use="required"/>
      </xsd:complexType>
      
      <xsd:complexType name="TestCaseType">
      <xsd:sequence>
      <xsd:element name="method" maxOccurs="unbounded" minOccurs="1" type="MethodType"/> 
      </xsd:sequence>
      <xsd:attribute name="name" type="xsd:normalizedString" use="required"/>
      </xsd:complexType>
      
      <xsd:complexType name="MethodType">
      <xsd:sequence>
      <xsd:element name="param" minOccurs="0" maxOccurs="unbounded" type="ParameterType"/>
    <xsd:choice>
    <xsd:element name="expected" minOccurs="1" type="VariableType"/>
    <xsd:element name="exception" minOccurs="1" type="VariableType"/>
    </xsd:choice>
    </xsd:sequence>
    <xsd:attribute name="name" type="xsd:normalizedString" use="required"/>
      </xsd:complexType>
      
    <xsd:complexType name="ParameterType">
    <xsd:simpleContent>
    <xsd:extension base="VariableType">
    <xsd:attribute name="name" type="xsd:normalizedString" use="optional"/>
    </xsd:extension>
        </xsd:simpleContent>
      </xsd:complexType> <xsd:complexType name="VariableType">
    <xsd:simpleContent>
    <xsd:extension base="xsd:string">
    <xsd:attribute name="type" type="xsd:normalizedString" use="optional"/>
    <xsd:attribute name="handler" type="xsd:normalizedString" use="optional"/>
    </xsd:extension>
    </xsd:simpleContent>
      </xsd:complexType>
      
    </xsd:schema>
      

  6.   

    private final static String URI_VALIDATION =
    "http://xml.org/sax/features/validation";

    private final static String URI_VALIDATION_SCHEMA =
    "http://apache.org/xml/features/validation/schema";
    private final static String URI_SCHEMA_LOCATION =
    "http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation";
    // Create XML reader object
    private XMLReader createReader() throws SAXException, SpecException {
    XMLReader reader = XMLDriverManager.createXMLReader(); String type = XMLDriverManager.getDriverType();
    Logger logger = Framework.getLogger();
    if (XMLDriverManager.XERCES.equals(type)) {
    try {
    reader.setProperty(URI_SCHEMA_LOCATION, “我的Schema文件名”);
    reader.setFeature(URI_VALIDATION_SCHEMA, true);
    reader.setFeature(URI_VALIDATION, true);
    } catch (SAXNotRecognizedException e) {
    logger.log(Logger.WARNING, "Old Xerces found, no schema validation will be done to the spec file.");
    } catch (SAXNotSupportedException e) {
    logger.log(Logger.WARNING, "Old Xerces found, no schema validation will be done to the spec file.");
    }
    } else {
    logger.log(Logger.WARNING, "No Xerces parser found, no schema validation will be done to the spec file.");
    }

    return reader;
    }这是获得XMLReader的方法,
    我把你上面的Schema加入后的出错信息:TargetNamespace.2: Expecting no namespace, but the schema document has a target namespace of 'http://www.w3schools.com'.
    于是我把targetNamespace去掉。然后出错信息是:src-resolve.4.2: Error resolving component 'SuiteType'. It was detected that 'SuiteType' is in namespace 'http://www.w3schools.com', but components from this namespace are not referenceable from schema document 'null'. If this is the incorrect namespace, perhaps the prefix of 'SuiteType' needs to be changed. If this is the correct namespace, then an appropriate 'import' tag should be added to 'null'.
    我的XML文档是这个:<?xml version="1.0" encoding="UTF-8"?>
    <suite name="sample" class="sample.FirstSuite">
    <testpackage name="Test Case2">
    <method name="testMethod1">
    <param type="int">1</param>
    <expected type="int">1</expected>
    </method>
    <method name="testMethod1">
    <param type="int">0</param>
    <expected>-1</expected>
    </method>
    </testpackage>
    <testcase name="Test Random1">
    <method name="testMethod1">
    <param handler="random">1 ~ 100</param>
    <expected type="int">1</expected>
    </method>
    </testcase>
    </suite>
      

  7.   

    其实这就需求在xml解析时要对名称空间进行处理,你用的那一个解析器,我经常用dom4j
      

  8.   

    你这个xml不行,如果xml文件需要xsd来验证,就必须在xml中引入xsd
      

  9.   

    用的是xerces.jar org.apache.xerces.parsers.SAXParser不是在代码中已经指定了验证了吗?
     reader.setFeature(URI_VALIDATION_SCHEMA, true);
     reader.setFeature(URI_VALIDATION, true);
      

  10.   

    我有一个疑问,代码中设置了这个属性:private final static String URI_SCHEMA_LOCATION =
            "http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation";...
    ...
    reader.setProperty(URI_SCHEMA_LOCATION, “我的Schema文件名”);是不是因为这个导致添加targetNamespace时出错?
    如果是的话,那个地址应该怎么改?
      

  11.   

    是的,你的xml文件需要指出名称空间
      

  12.   


    <suite xmlns="http://www.w3schools.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3schools.com suite.xsd" name="sample" class="sample.FirstSuite">你开头这么写
      

  13.   

    目前我的程序能通过验证了,主要是我的xerces版本太旧了,我更新了下就能用来检验了。
    不过目前我的文档中的testpackage和testcase只能有一种存在。
    就是要么全用testpackage要么全用testcase。不过这个也可以满足要求,不过就是有没有什么办法能使他们能混合着使用,又能有那种必须存在一个的约束?
      

  14.   

    可以啊,因为定义是可选的,choice,解析时相应处理就行了
      

  15.   


    File file = new File(filename/*xml file name*/);
    fins = new FileInputStream(file);
    ...try {
    XMLReader reader = createReader();

    topEntity = new SuiteEntity();
    entities.clear();

    reader.setContentHandler(this);
    reader.setErrorHandler(this);
    reader.setEntityResolver(this);

    reader.parse(new InputSource(fins));// parse的时候出现异常
    return topEntity;
         } catch (SAXException e) { // 在这里出现异常的
    Exception orig = e.getException();
    if (orig == null) {
    throw new SpecException(e.getMessage());

         }//报的异常是:cvc-complex-type.2.4.a: Invalid content was found starting with element 'testcase'. 
    //One of '{testpackage}' is expected.代码中的处理是否有错?
      

  16.   

    哦,知道了,因为testpackage和'testcase'是可选的,两者只能出现一个
    不好意思,我已经改过了,<?xml version="1.0" encoding="UTF-8"?>
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.w3schools.com" targetNamespace="http://www.w3schools.com" elementFormDefault="qualified" attributeFormDefault="unqualified">

    <xsd:element name="suite" type="SuiteType"/>

    <xsd:complexType name="SuiteType">
    <xsd:all>
    <xsd:element name="testcase" type="TestCaseType" minOccurs="0"/>
    <xsd:element name="testpackage" type="TestCaseType" minOccurs="0"/>
    </xsd:all>
    <xsd:attribute name="name" type="xsd:normalizedString" use="required"/>
    <xsd:attribute name="class" type="xsd:normalizedString" use="required"/>
    </xsd:complexType>
    <xsd:complexType name="TestCaseType">
    <xsd:sequence>
    <xsd:element name="method" type="MethodType" maxOccurs="unbounded"/>
    </xsd:sequence>
    <xsd:attribute name="name" type="xsd:normalizedString" use="required"/>
    </xsd:complexType>
    <xsd:complexType name="MethodType">
    <xsd:sequence>
    <xsd:element name="param" type="ParameterType" minOccurs="0" maxOccurs="unbounded"/>
    <xsd:choice>
    <xsd:element name="expected" type="VariableType"/>
    <xsd:element name="exception" type="VariableType"/>
    </xsd:choice>
    </xsd:sequence>
    <xsd:attribute name="name" type="xsd:normalizedString" use="required"/>
    </xsd:complexType>
    <xsd:complexType name="ParameterType">
    <xsd:simpleContent>
    <xsd:extension base="VariableType">
    <xsd:attribute name="name" type="xsd:normalizedString" use="optional"/>
    </xsd:extension>
    </xsd:simpleContent>
    </xsd:complexType>
    <xsd:complexType name="VariableType">
    <xsd:simpleContent>
    <xsd:extension base="xsd:string">
    <xsd:attribute name="type" type="xsd:normalizedString" use="optional"/>
    <xsd:attribute name="handler" type="xsd:normalizedString" use="optional"/>
    </xsd:extension>
    </xsd:simpleContent>
    </xsd:complexType>
    </xsd:schema>]