[xml] How to get default value attribute defined in xsd ? [libxml2-2.7.6]



Hi,

I would like to get default attribute value defined by following xsd.
I'm using reader API and
set  XML_SCHEMA_VAL_VC_I_CREATE value but it doesn't work.

What's wrong in my code ?

Thanks


--
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema";
elementFormDefault="qualified">
  <xs:element name="config">
    <xs:complexType mixed="true">
      <xs:sequence>
        <xs:element ref="attribute"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="attribute">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="linguistic">
          <xs:complexType>
            <xs:attribute name="item" default="US"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
--

<config>
    <attribute>
        <linguistic></linguistic>
    </attribute>
</config>

--
int schemaValidation(char *docUrl, char *schemaUrl) {
    ...
    if ((pctxt = xmlSchemaNewParserCtxt(schemaUrl)) == NULL) return 1;
    if ((schema = xmlSchemaParse(pctxt))==NULL) return 1;
    if ((vctxt = xmlSchemaNewValidCtxt(schema)) == NULL) return 1;

    xmlSchemaSetValidOptions(vctxt,XML_SCHEMA_VAL_VC_I_CREATE); //
populate xml tree with default values !?
    xmlSchemaSetValidErrors(vctxt, (xmlSchemaValidityErrorFunc) fprintf,
(xmlSchemaValidityWarningFunc) fprintf, stderr);
    reader = xmlNewTextReaderFilename(docUrl);
    xmlTextReaderSetSchema(reader, schema);
    xmlTextReaderSetErrorHandler(reader,
(xmlTextReaderErrorFunc)readerErr, NULL);
    xmlTextReaderSetStructuredErrorHandler(reader,
(xmlStructuredErrorFunc)structErr, NULL);
    while (xmlTextReaderRead(reader)) {
        xmlChar *name=xmlTextReaderName(reader);
    
        if (xmlTextReaderNodeType(reader)==XML_ELEMENT_NODE) {
            xmlNodePtr node=xmlTextReaderExpand(reader);
            xmlBufferPtr buffer=xmlBufferCreate();
            if (strcmp(name,"linguistic")==0) {
   ==> no attribute !           printf("linguistic: %d
attr:%s\n",xmlTextReaderHasAttributes(reader),xmlTextReaderReadAttributeValue(reader));
            }
            xmlNodeDump(buffer, node->doc,node,0,0);
   ==> no attribute !               
printf("node=[%s]\n",xmlBufferContent(buffer));
        }
    }
  }
  printf("Done.Is Valid?: %i", xmlTextReaderIsValid(reader));
  ...
}





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]