XML / XSD con espacio de nombres

I have a transformed XML that I've been trying to validate using the XSD it was created from.

The transformed XML is:

<?xml version="1.0" encoding="UTF-8"?>
<ifp:Widget xmlns:ifp="Widget.xsd">
    <ifp:Foo>foo</ifp:Foo>
    <ifp:Bar>bar</ifp:Bar>
 </ifp:Widget>

The XSD it was created from (based on raw xml) is:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
 <xs:element name="Widget">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="Foo" type='xs:string'/>
            <xs:element name="Bar" type='xs:string'/>
        </xs:sequence>
    </xs:complexType>
 </xs:element>
</xs:schema>

I'm getting an error: cvc-elt.1: Cannot find the declaration of element 'ifp:Widget'.

preguntado el 01 de febrero de 12 a las 22:02

4 Respuestas

Your schema makes no mention of any namespace called Widget.xsd. The XML should have no namespaces at all, i.e. just <Widget>

Whatever generated that XML needs looking at, it's broken.

Estoy adivinando Widget.xsd is actually the filename of your schema, but that doesn't belong as a namespace declaration in your XML.

Respondido 02 Feb 12, 02:02

To properly have your XML refer to an XSD use something like this:

<?xml version="1.0" encoding="UTF-8"?>
<Widget xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.yourdomain.com Widget.xsd>
    <Foo>foo</Foo>
    <Bar>bar</Bar>
</Widget>

If you really needed to define a namespace aliased as "ifp" do something like this:

<?xml version="1.0" encoding="UTF-8"?>
<ifp:Widget xmlns:ifp="http://www.yourdomain.com/actualNamespace">
    <ifp:Foo>foo</ifp:Foo>
    <ifp:Bar>bar</ifp:Bar>
</ifp:Widget>

You can use both together, of course.

Respondido 02 Feb 12, 02:02

"The XSD it was created from (based on raw xml) is:" is a bit confusing.... From the context it's suggesting that there was an XSD (that you list), from which the XML was generated but I don't understand what "based on raw xml" means... Are you saying that the XSD was generated from something? Or that the XSD was based on the "raw xml"... In other words, the XSD maybe is supposed to validate the XML used as input into your transformation?

As one more option to add to the already provided answers, here is the XSD that matches your transformed XML; the difference here is that the schema has a target namespace that matches the namespace used by your "transformed XML"...

<?xml version="1.0" encoding="utf-8"?>
<!--W3C Schema generated by QTAssistant/W3C Schema Refactoring Module (http://www.paschidev.com)-->
<xsd:schema xmlns="Widget.xsd" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="Widget.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="Widget">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="Foo" type="xsd:string" />
        <xsd:element name="Bar" type="xsd:string" />
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

Respondido 02 Feb 12, 04:02

El string xmlns:ifp="Widget.xsd" may be right. See wiki: The specification is not entirely prescriptive about the precise rules for namespace names (it does not explicitly say that parsers must reject documents where the namespace name is not a valid Uniform Resource Identifier), and many XML parsers allow any character string to be used.

But no element in your XSD file is in that namespace. Rigth Esquema element in your XSD file:

    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="Widget.xsd"
    elementFormDefault="qualified"> 

Respondido el 11 de junio de 13 a las 10:06

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.