QXMLStreamReader cannot read attribute

169 Views Asked by At

I want to parse the following xml (it's a xsd):

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
  <xs:element name = "elementName">
    <xs:simpleType>
      <xs:restriction base = "xs:string">
      </xs:restriction>
    </xs:simpleType>
  </xs:element>
</xs:schema>

At some point I invoke following method, that should read the base attribute of restriction:

void String::getFromBase() const {
  QXMLStreamReader m_node; // in true code it's a class member already initialized
  m_node.readNextStartElement();
  auto name = stripNamespace(m_node.name().toString()); // convert xs:restriction to restriction
  if (name != "restriction") {
    throw Exception("Restriction is not found when reading string data");
  }
  bool hasBase{false};
  auto x = m_node.attributes().size(); // x is zero but it should be one
  for (const auto& it : m_node.attributes()) {
    auto attrName = stripNamespace(it.name().toString());
    if (attrName == "base") {
      hasBase = true;
      break;
    }
  }
  if (!hasBase) {
    throw Exception("Restriction needs 'base' attribute");
  }
  auto attribute = stripNamespace(m_node.attributes().value(BaseLabel).toString());
  if (attribute == "string") {
    int i = 0;
  }
}

The method checks that the actual element in QXMLStreamReader is restriction, and this check passes, but then the size of attributes() is zero, so the reader does not find any attribute. Since I've only one restriction in my testing XSD, attributes should be there. How can I read the base attribute value correctly?

0

There are 0 best solutions below