1
$\begingroup$

I would like to retrieve the XML Tree of a XMLObject with only specific XMLElements.

In the help of Mathematica TransformingXML, a function called XMLNote is given and enables me to retrieve the XML Tree.

Here is the mentioned function :

XMLNote[XMLElement[tag_, attributes_, data_], m_Integer] := 
  Cell[CellGroupData[{
    Cell[TextData[
      StyleBox[tag, FontFamily -> "Swiss",
       FontWeight -> "Bold", FontSize -> 15]]],
    Sequence @@ (XMLNote[#1, m] &) /@ attributes,
    Sequence @@ (XMLNote[#1, m + 30] &) /@ data
    }, Open],
  CellMargins -> {{m, Inherited}, {Inherited, Inherited}}]

XMLNote[{an_String, a_String} -> v_String, m_Integer] :=
 Cell[TextData[{
    StyleBox[an, FontColor -> Hue[0.6]], " ",
    StyleBox[a, FontWeight -> "Bold"], " = ",
    StyleBox[v, Background -> GrayLevel[0.8]]}],
  CellMargins -> {{m + 5, Inherited}, {Inherited, Inherited}}]

XMLNote[a_String -> v_String, m_Integer] :=
 Cell[TextData[{
    StyleBox[a, FontWeight -> "Bold"], " = ",
    StyleBox[v, Background -> GrayLevel[0.8]]}],
  CellMargins -> {{m + 5, Inherited}, {Inherited, Inherited}}]

XMLNote[s_String, m_Integer] :=
 Cell[s, Background -> GrayLevel[0.9],
  CellMargins -> {{m + 25, Inherited}, {Inherited, Inherited}}]

In my XMLObject, I would like to retrieve only the XML tree limited to the XMLElements called "Section" and "TextHeading".

May you help me to reduce my tree to some specific XMLElements ?

Here a example for experimenting :

'XMLObject["Document"][{}, 
 XMLElement[
  "math", {{"http://www.w3.org/2000/xmlns/", "xmlns"} -> 
    "http://www.w3.org/1998/Math/MathML"}, {XMLElement[
    "semantics", {}, {XMLElement[
      "mfrac", {}, {XMLElement["mn", {}, {"1"}], 
       XMLElement["msqrt", {}, {XMLElement["mn", {}, {"2"}]}]}], 
     XMLElement[
      "annotation-xml", {"encoding" -> "MathML-Content"}, {XMLElement[
        "section", {}, {XMLElement["times", {}, {}], 
         XMLElement["TextHeading", {"type" -> "integer"}, {"1"}], 
         XMLElement[
          "section", {}, {XMLElement["power", {}, {}], 
           XMLElement[
            "section", {}, {XMLElement["power", {}, {}], 
             XMLElement["TextHeading", {"type" -> "integer"}, {"2"}], 
             XMLElement[
              "TextHeading", {"type" -> "rational"}, {"1", 
               XMLElement["sep", {}, {}], "2"}]}], 
           XMLElement[
            "TextHeading", {"type" -> 
              "integer"}, {"-1"}]}]}]}]}]}], {}]'

Thank you for your help

$\endgroup$
2
  • $\begingroup$ I would like to add a example. The problem is the fact that my XMLObject is too long and that I can attached .nb. If you have any suggestion ? $\endgroup$
    – Bendesarts
    May 12, 2015 at 8:29
  • $\begingroup$ Unfortunately, I didn't understand yet the use of XMLElement[___]:=##&[] with "Section"|"TextHeading". I will try to make a smaller XMLObject. For that purpose, how can I delete some specific XMLElements from my XMLObjects in order to reduce it ? $\endgroup$
    – Bendesarts
    May 12, 2015 at 8:49

1 Answer 1

1
$\begingroup$

Code outline:

ClearAll[XMLNote];

    (*modified, only act on target tags*)

XMLNote[
 XMLElement[tag : "section" | "TextHeading", attributes_, data_], m_Integer
] :=   ...

    (*new, for other tags pass down XML elements*)

XMLNote[XMLElement[_, attributes_, data : {__XMLElement}], 
   m_Integer] := Sequence @@ (XMLNote[#1, m + 30] &) /@ data;

    (*new, if tag is a final root, let it vanish*)

XMLNote[XMLElement[___], ___] := ## &[];

    (*rest is as it was*)

XMLNote[{an_String, a_String} -> v_String, m_Integer] := ...    
XMLNote[a_String -> v_String, m_Integer] := ...
XMLNote[s_String, m_Integer] := ...

Now let's use you XML:

XMLNote[xml[[2]], 5] // List // Notebook // NotebookPut

enter image description here


FullCode:

ClearAll[XMLNote];

XMLNote[XMLElement[tag : "section" | "TextHeading", attributes_, 
   data_], m_Integer] :=      Cell[CellGroupData[{Cell[
     TextData[          StyleBox[tag, FontFamily -> "Swiss", FontWeight -> "Bold", 
       FontSize -> 15]]], 
    Sequence @@ (XMLNote[#1, m] &) /@ attributes, 
    Sequence @@ (XMLNote[#1, m + 30] &) /@ data}, Open], 
  CellMargins -> {{m, Inherited}, {Inherited, Inherited}}]
(*new,for other tags pass down XML elements*)
XMLNote[XMLElement[_, attributes_, data : {__XMLElement}], 
   m_Integer] := Sequence @@ (XMLNote[#1, m + 30] &) /@ data;


XMLNote[XMLElement[___], ___] := ## &[];

XMLNote[{an_String, a_String} -> v_String, m_Integer] := 
 Cell[TextData[{StyleBox[an, FontColor -> Hue[0.6]], " ", 
    StyleBox[a, FontWeight -> "Bold"], " = ", 
    StyleBox[v, Background -> GrayLevel[0.8]]}], 
  CellMargins -> {{m + 5, Inherited}, {Inherited, Inherited}}]
XMLNote[a_String -> v_String, m_Integer] := 
 Cell[TextData[{StyleBox[a, FontWeight -> "Bold"], " = ", 
    StyleBox[v, Background -> GrayLevel[0.8]]}], 
  CellMargins -> {{m + 5, Inherited}, {Inherited, Inherited}}]
XMLNote[s_String, m_Integer] := 
 Cell[s, Background -> GrayLevel[0.9], 
  CellMargins -> {{m + 25, Inherited}, {Inherited, Inherited}}]
xml = XMLObject["Document"][{}, 
   XMLElement[
    "math", {{"http://www.w3.org/2000/xmlns/", "xmlns"} -> 
      "http://www.w3.org/1998/Math/MathML"}, {XMLElement[
      "semantics", {}, {XMLElement[
        "mfrac", {}, {XMLElement["mn", {}, {"1"}], 
         XMLElement["msqrt", {}, {XMLElement["mn", {}, {"2"}]}]}], 
       XMLElement[
        "annotation-xml", {"encoding" -> 
          "MathML-Content"}, {XMLElement[
          "section", {}, {XMLElement["times", {}, {}], 
           XMLElement["TextHeading", {"type" -> "integer"}, {"1"}], 
           XMLElement[
            "section", {}, {XMLElement["power", {}, {}], 

             XMLElement[
              "section", {}, {XMLElement["power", {}, {}], 
               XMLElement[
                "TextHeading", {"type" -> "integer"}, {"2"}], 
               XMLElement[
                "TextHeading", {"type" -> "rational"}, {"1", 
                 XMLElement["sep", {}, {}], "2"}]}], 
             XMLElement[
              "TextHeading", {"type" -> 
                "integer"}, {"-1"}]}]}]}]}]}], {}];

XMLNote[xml[[2]], 5] // List // Notebook // NotebookPut
$\endgroup$
10
  • $\begingroup$ Thank you for your help. It sounds good but it didn't manage yet to use it. $\endgroup$
    – Bendesarts
    May 12, 2015 at 10:32
  • $\begingroup$ Thank you for your help. It sounds good but it didn't manage yet to use it. I didn't understand you have put m=5 in 'XMLNote[xml[[2]], 5] // List // Notebook // NotebookPut' and not 0 $\endgroup$
    – Bendesarts
    May 12, 2015 at 10:42
  • $\begingroup$ I'm sorry but as I didn't manage to use it properly. May you send or write me the complete code ? $\endgroup$
    – Bendesarts
    May 12, 2015 at 10:52
  • $\begingroup$ in private message in stackexchange. is that ok for you? $\endgroup$
    – Bendesarts
    May 12, 2015 at 11:02
  • $\begingroup$ or directly in your answer $\endgroup$
    – Bendesarts
    May 12, 2015 at 11:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.