简体   繁体   中英

Can't validate xml against schema due to xmlns:xsi=“…” and xsi:type=“…” on an element

I'm trying to validate a xml response against an xsd schema in JMeter, but assertion always fails with the error

Cannot resolve 'job' to a type definition for element 'content'

The xml response is:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<result>
    <state>
        <tag>value</tag>
    </state>
    <content 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:type="job">
        <status>ok</status>
    </content>
</result>

and the schema is:

<xs:schema attributeFormDefault="unqualified" 
           elementFormDefault="qualified" 
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="result">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="state">
          <xs:complexType>
            <xs:sequence>
              <xs:element type="xs:string" 
                          name="tag"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="content">
          <xs:complexType>
            <xs:sequence>
              <xs:element type="xs:string" 
                          name="status"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

I can not modify the response but i can change the schema. How could i solve this problem?

thanks !

You should change your XSD as follows:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified">
  <xs:element name="result">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="state">
          <xs:complexType>
            <xs:sequence>
              <xs:element type="xs:string" name="tag"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="content" type="content"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:complexType name="content">
  </xs:complexType>

  <xs:complexType name="job">
    <xs:complexContent>
      <xs:extension base="content">
        <xs:sequence>
          <xs:element type="xs:string" name="status"/>
        </xs:sequence>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

</xs:schema>

The xsi:type attribute is handled in a special way by the validation, as a specification of which type to use for the validation of an element that can have different types forming a hierarchy.

In the fixed schema above I created a simple hierarchy with a base content complex type and job one derived from it.

Your response is refering to a type attribute defined as { http://www.w3.org/2001/XMLSchema-instance }:type. It does not belong to the namespace of your schema, hence you can't fix the validation by changing your schema.

In essence, the response is wrong (it refers to an unknown type in the w3 schema definition See the W3 schema definition here ) so ideally the response should be fixed.

Edit after the comment :

If you could alter your response to have the type attribute in the same namespace of your response, something like:

<result xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <state>
        <tag>value</tag>
    </state>
    <content xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" type="job">
        <status>ok</status>
    </content>
</result>

Then it would validate with the following XSD:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="result">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="state">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="tag" type="xs:string"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
                <xs:element name="content">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="status" type="xs:string"/>
                        </xs:sequence>
                        <xs:attribute name="type" type="type"/>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:simpleType name="type">
        <xs:restriction base="xs:string"/>
    </xs:simpleType>
</xs:schema>

You can then finetune the restrictions on your type to whatever you want.

You say you can't modify the response, but you can. You can put it through a transformation before validating it. That might be the right thing to do here; I can't tell, because I don't know what the xsi:type="job" is trying to achieve.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM