0

I need to process some XSD for performing operations, and I need to process them as normal XML files. I want to take every element of the XSD and process them (for example by printing them and their attributes).

This is a little sample:

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="name" type="xs:string"/>
</xs:schema>

I've tried to follow this post for retrieving data, but without success. This is my piece of code

void XSDReader::getStructure() const {
  QFile xsdFile(m_filePath.string().c_str());
  if (!xsdFile.open(QFile::ReadOnly | QFile::Text)) {
    throw Exception("Cannot read file " + m_filePath.string() + ". Error is: " + xsdFile.errorString().toStdString());
  }
  QXmlStreamReader reader(&xsdFile);
  std::stringstream ss;
  while (reader.readNextStartElement())
  {
    ss << "Found tag: " << reader.name().toString().toStdString() << "text: " << reader.text().toString().toStdString() << "token: " << reader.tokenString().toStdString();
    for (auto& attribute : reader.attributes())
    {
      ss << "attribute name: " << attribute.name().toString().toStdString() << ", attribute value: " << attribute.value().toString().toStdString();
    }
    reader.readNext();
    ss << "tag value:" << reader.text().toString().toStdString();
    reader.skipCurrentElement();
  }
  auto s = ss.str();
}

The string s after the processing is:

Found tag: schematext: token: StartElementtag value:

It does not contain anything regarding xs:string or its attributes.

How can I process correctly the XSD in order to print all of its data?

Jepessen
  • 11,744
  • 14
  • 82
  • 149
  • "I need to process some XSD for performing operations" is far too vague. Please be specific about the purpose of your program. – kimbert May 03 '20 at 19:42
  • "I need to process them as normal XML files" is a statement about your current solution. It is not a technical requirement (unless you want to disagree). I think you need to perform operations on an XSD for some reason that you have not yet explained. Better information, better answers! – kimbert May 03 '20 at 19:43
  • I need to retrieve name and attribute of every element. Once that I have them I can use them in my program. in the code I print name and attributes just as an example for retrieve those values. I ask at the end how can I do to print those data. – Jepessen May 03 '20 at 20:18

1 Answers1

-1

I want to take every element of the XSD and process them (for example by printing them and their attributes).

An XML Schema has a complex structure which is difficult to handle if you treat the XSDs as XML documents. If you try anyway, you are quite likely to get it wrong.

Code libraries exist which will load an XSD (or a set of XSDs) into an in-memory model. Those libraries were written by experts who understand XML Schema in detail. But unfortunately, C++ is not well served with tools for handling XSDs, as indicated by this thread: XML Schema to C++ Classes

I have done a lot of XSD processing in Java using the EMF model. It is a fully-featured set of libraries and supports the entire XSD specification. There is a learning curve, but that will be true whatever technology you use; the XSD data model is complex.

You may say '...but my XSDs are simple'. If so, feel free to go ahead with your XML-based approach. But it is likely to produce a fragile solution which will not be easy to maintain.

kimbert
  • 2,376
  • 1
  • 10
  • 20
  • If I need to use xsd for validating something I'll use some existing like xerces or the Qt schema validator. I need to do something else so I need to read xsd as XML, since it's a XML file. The question is related for a xsd but it's the same for any XML file. – Jepessen May 04 '20 at 00:07
  • Not sure why you mention 'validating something' - that is not your requirement. You are trying to process the declarations in the XSD. I know from experience how difficult that is _if_ you persist in trying to do it this way. – kimbert May 04 '20 at 09:46
  • I've the XML file and I want to retrieve elements and element attributes for creating a GUI. The XML is a xsd but it's the same thing. In order to perform my task I need to retrieve those elements. If I use them for a gui, for print or for validation is not important – Jepessen May 04 '20 at 12:24