Robert Bienert: Datei gegen Schema validieren (Namensraum-Problem)

Servus und Hallo!

Für eine Anwendung, die XML-Daten verarbeitet, habe ich eine XML Schema Definition geschrieben. Für die Verarbeitung der Daten spielt dieses Schema momentan keine Rolle, es dient bislang lediglich der Information/Dokumentation. Wenn ich nun rein probeweise eine Beispiel-XML-Datei mit einem XML-Parser (ich nehme xmllint, welches zur libxml gehört) validiere, erhalte ich folgende Fehlermeldung:

layout.xml:3: element favicon: Schemas validity error : Element '{http://laymansys.sf.net/rdf/layout}favicon': This element is not expected. Expected is one of ( favicon, style, nav ).

Vorher wird allerdings die komplette XML-Datei ausgegeben:

<?xml version="1.0"?>  
<layout xmlns="http://laymansys.sf.net/rdf/layout" xmlns:schemaLocation="../def/layout.xsd">  
        <favicon type="image/x-icon" href="ico.ico"/>  
        <style type="text/css" media="screen">style.css</style>  
        <style type="text/css" media="print">print.css</style>  
        <nav type="text/html" src="nav"/>  
</layout>

Die zugehörige XML Schema Definition sieht folgendermaßen aus:

<?xml version="1.0" encoding="UTF-8" ?>  
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"  
        targetNamespace="http://laymansys.sf.net/rdf/layout"  
        xmlns="http://laymansys.sf.net/rdf/layout">  
  
<xsd:element name="layout" type="LayoutDefinition" />  
  
<xsd:complexType name="LayoutDefinition">  
        <xsd:sequence>  
                <xsd:element name="favicon" type="FaviconFile"  
                        minOccurs="0" maxOccurs="1" />  
                <xsd:element name="style" type="StyleFile" minOccurs="0"  
                        maxOccurs="unbounded" />  
                <xsd:element name="nav" type="NavFile" minOccurs="0"  
                        maxOccurs="1" />  
        </xsd:sequence>  
        <xsd:attribute name="name" type="xsd:string">  
        </xsd:attribute>  
</xsd:complexType>  
  
<xsd:simpleType name="MIMEType">  
        <xsd:restriction base="xsd:token">  
                <xsd:pattern value="(application|image|text)/[\S]+" />  
        </xsd:restriction>  
</xsd:simpleType>  
  
<xsd:complexType name="FaviconFile">  
        <xsd:simpleContent>  
        <xsd:extension base="xsd:anyURI">  
                <xsd:attribute name="href" type="xsd:anyURI" />  
                <xsd:attribute name="type" type="MIMEType" />  
        </xsd:extension>  
        </xsd:simpleContent>  
</xsd:complexType>  
  
<xsd:complexType name="StyleFile">  
        <xsd:simpleContent>  
        <xsd:extension base="xsd:anyURI">  
                <xsd:attribute name="href" type="xsd:anyURI" />  
                <xsd:attribute name="type" type="MIMEType" use="required" />  
                <xsd:attribute name="media" type="xsd:string" />  
                <xsd:attribute name="title" type="xsd:string" />  
                <xsd:attribute name="charset" type="xsd:string" />  
        </xsd:extension>  
        </xsd:simpleContent>  
</xsd:complexType>  
  
<xsd:complexType name="NavFile">  
        <xsd:simpleContent>  
        <xsd:extension base="xsd:anyURI">  
                <xsd:attribute name="src" type="xsd:anyURI" />  
                <xsd:attribute name="type" type="NavTypes" use="required" />  
                <xsd:attribute name="position" type="NavPositions"  
                        default="after-heading" />  
        </xsd:extension>  
        </xsd:simpleContent>  
</xsd:complexType>  
  
<xsd:simpleType name="NavTypes">  
        <xsd:restriction base="MIMEType">  
                <xsd:enumeration value="text/html" />  
                <xsd:enumeration value="text/html" />  
                <xsd:enumeration value="application/x-httpd-php" />  
        </xsd:restriction>  
</xsd:simpleType>  
  
<xsd:simpleType name="NavPositions">  
        <xsd:restriction base="xsd:token">  
                <xsd:enumeration value="first-of-all" />  
                <xsd:enumeration value="after-heading" />  
                <xsd:enumeration value="in-front-of-foot" />  
                <xsd:enumeration value="last" />  
                <xsd:enumeration value="user-defined" />  
        </xsd:restriction>  
</xsd:simpleType>  
  
</xsd:schema>

Diese XSD soll, als eine Art BNF geschrieben, XML-Daten in folgender Form ermöglichen:

layout ::= favicon?, style*, nav?

Ich vermute, dass mein Problem im Zusammenhang mit einer falschen Verwendung der XML-Namensraum-Angabe steht, allerdings bin ich selbst nach dem Studium der offiziellen XSD-Spezifikation als auch einiger Beispiel-Schemata auf den W3C-Seiten immer noch ratlos. Das Problem ist, wie gesagt, nicht gravierend, aber mich interessiert trotzdem, wie es richtig sein müsste.

Viele Grüße,
Robert

  1. Moin!

    Ich rufe xmllint wie folgt auf:

    xmllint --schema /pfad/zum/schema.xsd layout.xml

    Viele Grüße,
    Robert

  2. Hi,

    Kann es sein, dass du deinen Namenraum nicht mit angibst ;-)

    also statt

    <xsd:element name="layout" type="LayoutDefinition" />

    muesste es lauten:

    <xsd:element name="layout" type="xsd:LayoutDefinition" />

    Das kommt bei dir häufig vor, dass du den namensraum für den type nicht angibst.

    Gruß
    Holger

    1. Moin!

      Kann es sein, dass du deinen Namenraum nicht mit angibst ;-)

      Wie jetzt?

      also statt

      <xsd:element name="layout" type="LayoutDefinition" />

      muesste es lauten:

      <xsd:element name="layout" type="xsd:LayoutDefinition" />

      Das glaube ich nicht, denn ändere ich dies (xsd: vor sämtlichen Typ-Namen und Definitionen beschwert sich der Parser:

      layout.xsd:21: element complexType: Schemas parser error : Element '{http://www.w3.org/2001/XMLSchema}complexType', attribute 'name': 'xsd:LayoutDefinition' is not a valid value of the atomic type 'xs:NCName'.
      layout.xsd:48: element simpleType: Schemas parser error : Element '{http://www.w3.org/2001/XMLSchema}simpleType', attribute 'name': 'xsd:MIMEType' is not a valid value of the atomic type 'xs:NCName'.
      layout.xsd:72: element complexType: Schemas parser error : Element '{http://www.w3.org/2001/XMLSchema}complexType', attribute 'name': 'xsd:FaviconFile' is not a valid value of the atomic type 'xs:NCName'.
      layout.xsd:88: element complexType: Schemas parser error : Element '{http://www.w3.org/2001/XMLSchema}complexType', attribute 'name': 'xsd:StyleFile' is not a valid value of the atomic type 'xs:NCName'.
      layout.xsd:108: element complexType: Schemas parser error : Element '{http://www.w3.org/2001/XMLSchema}complexType', attribute 'name': 'xsd:NavFile' is not a valid value of the atomic type 'xs:NCName'.
      layout.xsd:125: element simpleType: Schemas parser error : Element '{http://www.w3.org/2001/XMLSchema}simpleType', attribute 'name': 'xsd:NavTypes' is not a valid value of the atomic type 'xs:NCName'.
      layout.xsd:136: element simpleType: Schemas parser error : Element '{http://www.w3.org/2001/XMLSchema}simpleType', attribute 'name': 'xsd:NavPositions' is not a valid value of the atomic type 'xs:NCName'.

      Der Standard-Namensraum ist ja auch "http://laymansys.sf.net/rdf/layout", d.h. alle Typen, die ich selbst definiere, gehören hierzu (soweit ich XSD verstanden habe).

      Verwende ich lediglich xsd: vor den Typ-Angaben, wie in deinem Beispiel, erhalte ich:

      layout.xsd:19: element element: Schemas parser error : element decl. '{http://laymansys.sf.net/rdf/layout}layout', attribute 'type': The QName value '{http://www.w3.org/2001/XMLSchema}LayoutDefinition' does not resolve to a(n) type definition.

      Liegt das Problem daran, dass ich in der XML-Datei kein Schema referenziere?

      Viele Grüße,
      Robert

      1. Oh, hatte wo habe ich denn da hingeschaut ;-) na ja, egal. so duerfte ein Schuh draus werden ;-) die layout Location kannst du dann noch ändern

        <?xml version="1.0"?>
        <layout xmlns="http://laymansys.sf.net/rdf/layout"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://laymansys.sf.net/rdf/layout ../def/layout.xsd">
             <favicon type="image/x-icon" href="ico.ico"/>
            <style type="text/css" media="screen">style.css</style>
            <style type="text/css" media="print">print.css</style>
            <nav type="text/html" src="nav"/>
        </layout>

        <?xml version="1.0" encoding="UTF-8" ?>
        <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
                        targetNamespace="http://laymansys.sf.net/rdf/layout"
                        xmlns="http://laymansys.sf.net/rdf/layout"
                        elementFormDefault="qualified">

        <xs:element name="layout" type="LayoutDefinition" />

        <xs:complexType name="LayoutDefinition">
                        xs:sequence
                                <xs:element name="favicon" type="FaviconFile"  minOccurs="1" maxOccurs="unbounded" />
                                <xs:element name="style" type="StyleFile" minOccurs="0"  maxOccurs="unbounded" />
                                <xs:element name="nav" type="NavFile" minOccurs="0"  maxOccurs="1" />
                        </xs:sequence>
                            <xs:attribute name="name" type="xs:string">
                            </xs:attribute>

        </xs:complexType>

        <xs:simpleType name="MIMEType">
                        <xs:restriction base="xs:token">
                                <xs:pattern value="(application|image|text)/[\S]+" />
                        </xs:restriction>
                </xs:simpleType>

        <xs:complexType name="FaviconFile">
                        xs:simpleContent
                                <xs:extension base="xs:anyURI">
                                        <xs:attribute name="href" type="xs:anyURI" />
                                        <xs:attribute name="type" type="MIMEType" />
                                </xs:extension>
                        </xs:simpleContent>
                </xs:complexType>

        <xs:complexType name="StyleFile">
                        xs:simpleContent
                                <xs:extension base="xs:anyURI">
                                        <xs:attribute name="href" type="xs:anyURI" />
                                        <xs:attribute name="type" type="MIMEType" use="required" />
                                        <xs:attribute name="media" type="xs:string" />
                                        <xs:attribute name="title" type="xs:string" />
                                        <xs:attribute name="charset" type="xs:string" />
                                </xs:extension>
                        </xs:simpleContent>
                </xs:complexType>

        <xs:complexType name="NavFile">
                        xs:simpleContent
                                <xs:extension base="xs:anyURI">
                                        <xs:attribute name="src" type="xs:anyURI" />
                                        <xs:attribute name="type" type="NavTypes" use="required" />
                                        <xs:attribute name="position" type="NavPositions"
                                                default="after-heading" />
                                </xs:extension>
                        </xs:simpleContent>
                </xs:complexType>

        <xs:simpleType name="NavTypes">
                        <xs:restriction base="MIMEType">
                                <xs:enumeration value="text/html" />
                                <xs:enumeration value="text/html" />
                                <xs:enumeration value="application/x-httpd-php" />
                        </xs:restriction>
                </xs:simpleType>

        <xs:simpleType name="NavPositions">
                        <xs:restriction base="xs:token">
                                <xs:enumeration value="first-of-all" />
                                <xs:enumeration value="after-heading" />
                                <xs:enumeration value="in-front-of-foot" />
                                <xs:enumeration value="last" />
                                <xs:enumeration value="user-defined" />
                        </xs:restriction>
                </xs:simpleType>

        </xs:schema>

        1. Moin!

          Oh, hatte wo habe ich denn da hingeschaut ;-) na ja, egal. so duerfte ein Schuh draus werden ;-)

          Erst neugierig machen und dann die Klappe halten gilt nicht ;-)

          die layout Location kannst du dann noch ändern

          Bitte was? Oder meinst du die Verwendung von xsi:schemaLocation="…"?

          <?xml version="1.0" encoding="UTF-8" ?>
          <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
                          targetNamespace="http://laymansys.sf.net/rdf/layout"
                          xmlns="http://laymansys.sf.net/rdf/layout"
                          elementFormDefault="qualified">

          Genau das reicht schon, elementFormDefault="qualified" fehlte noch, jetzt erhalte ich:

          layout.xml validates

          Warum hast du allerdings die komplette XSD-Datei noch einmal aufgeführt, der einzige Unterschied war der Namensraum-Präfix?

          Vielen Dank auf jeden Fall und viele Grüße,
          Robert

          1. Moin back,

            Bitte was? Oder meinst du die Verwendung von xsi:schemaLocation="…"?

            jepp, wo deine layout.xsd abgelegt ist.

            Warum hast du allerdings die komplette XSD-Datei noch einmal aufgeführt, der einzige Unterschied war der Namensraum-Präfix?

            »»

            Weil ich es damit getestet hatte. Aber du kannst weiterhin deinen xsd: namespace benutzen ;-)

            Gruß zurück

            1. Nabend!

              Warum hast du allerdings die komplette XSD-Datei noch einmal aufgeführt, der einzige Unterschied war der Namensraum-Präfix?

              Weil ich es damit getestet hatte. Aber du kannst weiterhin deinen xsd: namespace benutzen ;-)

              Ob das W3C xsd präferiert ist mir relativ schnuppe (soll wohl angeblich so sein), aber das Syntax-Highlighting in meinem VIm mag xsd lieber, ebenso wie xsl für XSL-Dateien.

              Viele Grüße,
              Robert

  3. Hallo Robert,

    Du bindest das Schema schon mal falsch ein. Du deklarierst ein Namensraum-Prefix "schmaLocation".
    Du müsstest das Schema wohl in etwa so einbinden:

      
    <layout  
      xmlns="http://laymansys.sf.net/rdf/layout"  
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
      xsi:schemaLocation="http://laymansys.sf.net/rdf/layout  
          ../def/layout.xsd">  
    
    

    Grüße

    Daniel

    1. Moin!

      Du bindest das Schema schon mal falsch ein. Du deklarierst ein Namensraum-Prefix "schmaLocation".

      Habe ich so in einer (normativen) XSD-Datei beim W3C gesehen, deshalb dachte ich (mangels Idee und Ahnung), dass ich das einmal ausprobieren könnte.

      Du müsstest das Schema wohl in etwa so einbinden:

      <layout
        xmlns="http://laymansys.sf.net/rdf/layout"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://laymansys.sf.net/rdf/layout
            ../def/layout.xsd">

        
      Gute Idee, die allerdings etwas Vergleichbares wie die Angabe einer DTD bewirkt: Hiermit wird lediglich bereits in der XML-Datei ein bestimmtes Schema zum gewählten Namensraum ausgewählt, allerdings zeigt der Parser immer noch  
        
      
      > layout.xml:6: element favicon: Schemas validity error : Element '{http://laymansys.sf.net/rdf/layout}favicon': This element is not expected. Expected is one of ( favicon, style, nav ).  
        
      [hhs Vorschlag ist die Lösung](https://forum.selfhtml.org/?t=131577&m=851276).  
        
      Vielen Dank für die Mühe,  
      Robert
      
      1. Hallo Robert,

        Dass die Angabe bei Dir keine Rolle spielt, liegt daran, dass Du beim Aufruf des Prozessors das Schema mit angibst.
        Die Fehlermeldung hatte ich irgendwie überlesen, aber das Problem hat ja hh jetzt gelöst.

        Grüße

        Daniel

        1. Moin!

          Dass die Angabe bei Dir keine Rolle spielt, liegt daran, dass Du beim Aufruf des Prozessors das Schema mit angibst.

          Dann nur noch mal zur Bestätigung:

          Die Angabe xsi:schemaLocation="Namensraum /pfad/zum/schema.xsd" ist vergleichbar mit der DOCTYPE-Deklaration bei der Verwendung von DTDs?

          Viele Grüße,
          Robert

          1. Hallo Robert,

            Die Angabe xsi:schemaLocation="Namensraum /pfad/zum/schema.xsd" ist vergleichbar mit der DOCTYPE-Deklaration bei der Verwendung von DTDs?

            Ja, die Regeln bei Schemata scheinen da allerdings etwas umfangreicher zu sein. Ich hab' mich mit Schemata noch nicht eingehend befasst, kann Dir also nicht genau sagen, was da dann genau abläuft. Es ist z.B. auch möglich, da mehrere Schemata auch an mehreren Stellen des Dokuments anzugeben.

            Grüße

            Daniel