Converting SymbolicXML into Notebook Format

An XML Example Using Mathematica 4.2

The first step in creating a notebook from an XML document is to read in the XML data as SymbolicXML.  This is done using Import[ ].

newdata = Import["ritterxml.xml"];

There is no one way to create a notebook from any XML document.  The user must have some knowledge of the structure of the data and how it should best be represented in notebook format.  This example uses the XML version of the Ritter Catalog of Cataclysmic Variable Stars (Ritter, H., & Kolb, U., 1998, A&AS, 129, 83) created and used in the previous two notebooks.  The next cell begins to describe how the notebook should be structured.  First, the "Document" and "Catalog" tags should be ignored.  It was decided that each object of the catalog would be displayed as a section heading and all of the attributes of the object would be displayed as a GridBox within that section.

RitterToNotebook[XMLObject["Document"][_, 
            data_XMLElement, _]] := RitterToNotebook[data];
RitterToNotebook[XMLElement["CATALOG", _, data_]] := 
        Notebook[RitterToNotebook /@ data];
RitterToNotebook[XMLElement["OBJECT", _, attr_]] := 
        CellGroupData[{Cell[GetObjectName[attr], "Section"], 
            Cell[BoxData[RitterAttributesToGridBox[attr]]]}, 
          Closed];

The code above requires several additional definitions.  First, we need a way to extract the name of a particular object.

GetObjectName[xml_] := 
        Cases[xml, XMLElement["NAME", _, {name_String}] -> name, 
          Infinity];

Now we construct the GridBox from the attributes.

RitterAttributesToGridBox[attrs_] := 
        GridBox[RitterAttributeToRow /@ attrs, 
          ColumnAlignments -> {Right, Left, Left}];

Then we define a function that places the name of a particular attribute, with a colon appended, in the first column of the GridBox.  In the event that an object has no attributes, they are left blank.

RitterAttributeToRow[att_] := 
        att /. XMLElement[name_, 
              atts_, {value_}] :> {StyleBox[name <> ":", 
                FontWeight -> "Bold"], value, 
              ParameterAttributesToText[name, atts]};
RitterAttributeToRow[XMLElement["NAME", __]] := 
        Sequence[];

Finally, we need to decide how the actual values of the attributes should be displayed in notebook format.  In our case, the decision was that each case would be handled individually.

Off[General::spell1]
ParameterAttributesToText[name_, atts_] := 
        StringJoin[(ParameterAttributeToText[
                    name, #] <> "  ") &  /@  atts];
\(\(ParameterAttributeToText["ORBPER", "spectroscopic" -> "true"] := "Spectroscopic";\)\)
\(\(ParameterAttributeToText[_, "valuetype" -> "max"] := "Maximum Value";\)\)
\(\(ParameterAttributeToText[_, "valuetype" -> "min"] := "Minimum Value";\)\)
\(\(ParameterAttributeToText[_, "uncertain" -> "true"] := "Uncertain";\)\)
ParameterAttributeToText[_, "filter" -> 
            filterkind_] := "(" <> 
          ToString[filterkind] <> " filter)";
ParameterAttributesToText[_, _] := Sequence[];

We are now ready to construct the notebook.  It should be noted that this notebook creates a notebook with the Default style sheet.  The goal was to create a structured notebook and allow the user to apply his/her own styles as a separate task.

NotebookPut@RitterToNotebook[newdata];