3

I have a pmml file containing one line:

<DataField dataType="string" name="class" optype="categorical"><Value value="y0" /><Value value="y1" /><Value value="y2" /><Value value="y3" /><Value value= "y4" /></DataField><DataField dataType="double" name="x0" optype="continuous" /><DataField dataType="double" name="x1" optype= "continuous" /><DataField dataType="double" name="x2" optype="continuo    us" /><DataField dataType="double" name="x3" optype="continuous" /><DataField dataType="double" name="x4" optype="continuous" /><DataField dataType="double" name="x5" optype="continuous" /><DataField dataType="double" name="x6" optype="continuous" /><DataField dataType="double" name="x7" optype="continuous" /><DataField dataType="double" name="x8" optype="continuous" /><DataField dataType="double" name="x9" optype="continuous" /><DataField dataType="double" name="x10" optype="continuous" /><DataField dataType="double" name="x11" optype="continuous" /><DataField dataType="double" name="x12" optype="continuous" /><DataField dataType="double" name="x13" optype="continuous" /><DataField dataType="double" name="x14" optype="continuous" /><DataField dataType="double" name="x15" optype="continuous" /><DataField dataType="double" name="x16" optype="continuous" /><DataField dataType="double" name="x17" optype="continuous" /><DataField dataType="double" name="x18"     optype="continuous" /><DataField dataType="double" name="x19" optype="continuous" /><DataField dataType="double" name="x20" optype="continuous" /><DataField dataType="double" name="x21" optype="continuous" /><DataField dataType="double" name="x22" optype="continuous" /><DataField dataType="double" name="x23" optype="continuous" /></DerivedField></NeuralOutput></NeuralOutputs></NeuralNetwork></PMML>   

I want to insert a new line after all of the optype= "continuous" /> occurrences.

I was able to inset the new line between the first optype and the last /> using sed 's:optype.*/>:a&\n:g' test.pmml > new.pmml.

How can I do this for all occurrences of the pattern? (I prefer sed but solutions using other tools are fine as well.)

  • 3
    Have you tried formatting the file with tools like xmlstarlet or xml tidy? Because I assume this is a question about formatting of XML. – Kusalananda Jul 2 '18 at 17:25
  • No I haven't, I will give them a try. However, it would be nice to know the answer assuming it is not a pmml or those tools not working. – Daniel Rahmani Jul 2 '18 at 17:54
4

You can try this with GNU sed or other sed implementations that now also treat \n as newline in the replacement:

sed 's|optype[^>]*/>|&\n|g' test.pmml

POSIXly:

sed 's|optype[^>]*/>|&\
|g' test.pmml
0

If what you want to do is to nicely format your XML file, then something like XMLStarlet can do this for you:

xmlstarlet fo file.xml >new-file.xml

The fact that your XML file is a pmml file should not matter as long as pmml is an XML format.

XMLStarlet is a general XML tool for performing XPATH queries etc., but there are also tools specifically for formatting XML documents, such as Tidy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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