简体   繁体   中英

s4s-att-invalid-value: Invalid attribute value for 'name' in element 'element'

XML

<ns2:Response xmlns:ns2="http://test.com/" Id="122212">
  <Infos size="1">
     <Info>
        <name>test</name>
     </Info>
  </Infos>
</ns2:Response>

Generated XSD

 <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
     <!-- XML Schema Generated from XML Document on Mon Feb 20 2017 23:20:03 GMT+0530 (India Standard Time) -->
     <!-- with XmlGrid.net Free Online Service http://xmlgrid.net -->
     <xs:element name="ns2:Response">
           <xs:complexType>
                 <xs:sequence>
                       <xs:element name="Infos">
                             <xs:complexType>
                                   <xs:sequence>
                                         <xs:element name="Info">
                                               <xs:complexType>
                                                     <xs:sequence>
                                                           <xs:element name="name" type="xs:string"></xs:element>
                                                     </xs:sequence>
                                               </xs:complexType>
                                         </xs:element>
                                   </xs:sequence>
                                   <xs:attribute name="size" type="xs:int"></xs:attribute>
                             </xs:complexType>
                       </xs:element>
                 </xs:sequence>
                 <xs:attribute name="xmlns:ns2" type="xs:string"></xs:attribute>
                 <xs:attribute name="Id" type="xs:int"></xs:attribute>
           </xs:complexType>
     </xs:element>

Error

SAX Exception: s4s-att-invalid-value: Invalid attribute value for 'name' in element 'element'. Recorded reason: cvc-datatype-valid.1.2.1: 'ns2:Response' is not a valid value for 'NCName'.

There are several changes required for your XML and XSD, including:

  • Change <xs:element name="ns2:Response"> to <xs:element name="Response"> because element name declarations must be non-colonized names (NCNAMEs).
  • Delete <xs:attribute name="xmlns:ns2"... /> because namespaces cannot be declared as attributes.
  • Add a targetNamespace to the XSD that matches the namespace of the root element in the XML document.
  • Import a separate XSD for those elements that you wish to be in no namespace (given that your root elements is in a namespace). You must use a separate XSD to accomplish this.

Altogether, your XML,

<?xml version="1.0" encoding="UTF-8"?>
<ns2:Response xmlns:ns2="http://test.com/"
              Id="122212">
  <Infos size="1">
    <Info>
      <name>test</name>
    </Info>
  </Infos>
</ns2:Response>

will validate successfully against these XSD:

Main

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           elementFormDefault="qualified"
           attributeFormDefault="unqualified"
           xmlns:ns="http://test.com/"
           targetNamespace="http://test.com/">
  <xs:import schemaLocation="Infos.xsd"/>
  <xs:element name="Response">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="Infos"/>
      </xs:sequence>
      <xs:attribute name="Id" type="xs:int"></xs:attribute>
    </xs:complexType>
  </xs:element>
</xs:schema>

Imported (Infos.xsd)

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Infos">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Info">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="name" type="xs:string"></xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
      <xs:attribute name="size" type="xs:int"></xs:attribute>
    </xs:complexType>
  </xs:element>
</xs:schema>

I was able to get a parsed file by, as the above answer suggests, adding a target namepsace to the schema and by adding the ns prefix to all elements.

<?xml version="1.0" encoding="UTF-8"?>
<ns2:Response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://test.com testq1.xsd"     xmlns:ns2="http://test.com" Id="122212">
<ns2:Infos size="1">
    <ns2:Info>
        <ns2:name>test</ns2:name>
    </ns2:Info>
</ns2:Infos>

and the schema:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
       targetNamespace="http://test.com"
       elementFormDefault="qualified"
       attributeFormDefault="unqualified"> 

<xs:element name="Response">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="Infos">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="Info">
                            <xs:complexType>
                                <xs:sequence>
                                    <xs:element name="name" type="xs:string"></xs:element>
                                </xs:sequence>
                            </xs:complexType>
                        </xs:element>
                    </xs:sequence>
                    <xs:attribute name="size" type="xs:int"></xs:attribute>
                </xs:complexType>
            </xs:element>
        </xs:sequence>

        <xs:attribute name="Id" type="xs:int"></xs:attribute>
    </xs:complexType>
</xs:element>
</xs:schema>

You can approach this in 2 ways.

Approach 1 - multiple schemas

The more typical approach is to have a schema one for each namespace used. So you end up with schemas that look like this

SampleXml0.xsd

<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid Studio 2017 (https://www.liquid-technologies.com) -->
<xs:schema xmlns:ns2="http://test.com/" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://test.com/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:import schemaLocation="C:\Temp\StackOverflow\42351409\SampleXml1.xsd" />
  <xs:element name="Response">
    <xs:complexType>
      <xs:sequence>
        <xs:element minOccurs="0" ref="Infos" />
      </xs:sequence>
      <xs:attribute name="Id" type="xs:unsignedInt" use="optional" />
    </xs:complexType>
  </xs:element>
</xs:schema>

在此输入图像描述

SampleXml1.xsd

<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid Studio 2017 (https://www.liquid-technologies.com) -->
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Infos">
    <xs:complexType>
      <xs:sequence>
        <xs:element minOccurs="0" name="Info">
          <xs:complexType>
            <xs:sequence>
              <xs:element minOccurs="0" name="name" type="xs:string" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
      <xs:attribute name="size" type="xs:unsignedByte" use="optional" />
    </xs:complexType>
  </xs:element>
</xs:schema>

在此输入图像描述

Approach 2 - changing elementFormDefault

Now this is quite specific to your example, but as its only the root element that is qualified with a namespace its possible to change elementFormDefault to unqualified . This has the effect of forcing the elements defined as root element in the schema to have a namespace qualification, while the other elements do not.

<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid Studio 2017 (https://www.liquid-technologies.com)-->
<xs:schema xmlns:ns2="http://test.com/" attributeFormDefault="unqualified" elementFormDefault="unqualified" targetNamespace="http://test.com/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="Response">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Infos" minOccurs="0">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="Info" minOccurs="0">
                                <xs:complexType>
                                    <xs:sequence>
                                        <xs:element name="name" type="xs:string" minOccurs="0" />
                                    </xs:sequence>
                                </xs:complexType>
                            </xs:element>
                        </xs:sequence>
                        <xs:attribute name="size" type="xs:unsignedByte" use="optional" />
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
            <xs:attribute name="Id" type="xs:unsignedInt" use="optional" />
        </xs:complexType>
    </xs:element>
</xs:schema>

在此输入图像描述

I would probably recommend creating multiple schemas as elementformdefault is typically overlooked by client implementations and ignored.

Why three <xs:sequence> elements? do you really need that? Could you explain better how your code is organized? Usually it is pretty simple, like this:

<xsd:element name="ns2:Response">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="name" type="xs:string" />
            </xsd:sequence>
        </xsd:complexType>
</xsd:element>

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