1

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

Kuba
  • 136,707
  • 13
  • 279
  • 740
Bendesarts
  • 1,099
  • 5
  • 12
  • 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 ? – Bendesarts May 12 '15 at 08:29
  • 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 ? – Bendesarts May 12 '15 at 08:49

1 Answers1

1

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
Kuba
  • 136,707
  • 13
  • 279
  • 740
  • Thank you for your help. It sounds good but it didn't manage yet to use it. – Bendesarts May 12 '15 at 10:32
  • 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 – Bendesarts May 12 '15 at 10:42
  • I'm sorry but as I didn't manage to use it properly. May you send or write me the complete code ? – Bendesarts May 12 '15 at 10:52
  • in private message in stackexchange. is that ok for you? – Bendesarts May 12 '15 at 11:02
  • or directly in your answer – Bendesarts May 12 '15 at 11:02
  • @Bendesarts there are no priv msgs in se. I will add code here. – Kuba May 12 '15 at 11:04
  • Perfect Thank you it is perfect !!! I only need to keep for the XMLElements called TextHeading those which have a specific style in their properties. How can I do to this complementary limitation ? – Bendesarts May 12 '15 at 11:14
  • @Bendesarts Please take tour. And about the code, I'm glad it works. If you want to narrow it down, add better patterns to XMLNote. – Kuba May 12 '15 at 11:19
  • OK; it is done. There is only this 'XMLNote[XMLElement[___], ___] := ## &[];' that i have difficulties to understand. I would be great if you could add me some comments in order to help me to understand the structure of this code. – Bendesarts May 12 '15 at 13:30
  • @Bendesarts Take a look here: http://mathematica.stackexchange.com/q/78910/5478 – Kuba May 12 '15 at 13:37