Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

With regex how would I delete all the same elements except the first element in javascript?

How would I delete all the <Document> elements except the first one ?
With a regular expression it would be suitable but don’t know how to do that

\<?xml version="1.0"?\>
\<kml xmlns="http://www.opengis.net/kml/2.2"\>  
\<Document\>        
\<name\>Traverse path TRV_1_PATH_1\</name\>
\<Placemark\>   
\<name\>Traverse path TRV_1_PATH_1\</name\>             
\<LineString\>              
\<coordinates\>                     
\-13.701582,29.043733,0 -13.702719,29.043939,0 -13.704522,29.043846,0 -13.704886,29.043939,0 -13.705208,29.043733,0 -13.705723,29.043827,0 -13.705852,29.04362,0 -13.706088,29.043789,0 -13.70656,29.043489,0 -13.707612,29.043902,0 -13.708019,29.043827,0 -13.708534,29.044296,0 -13.709564,29.044221,0 -13.710144,29.04469,0 -13.71141,29.045177,0 -13.712161,29.04514,0 -13.712611,29.045515,0 -13.713255,29.045177,0 -13.714392,29.044877,0 -13.714457,29.044446,0 -13.715315,29.044033,0
\</coordinates\>                    
\</LineString\>                 
\</Placemark\>  
\<Document\>        
\<name\>Traverse path TRV_1_PATH_2\</name\>         
\<Placemark\>           
\<name\>Traverse path TRV_1_PATH_2\</name\>             
\<LineString\>              
\<coordinates\>                     
\-13.715379,29.043996,0 -13.716795,29.044465,0 -13.718061,29.044202,0 -13.718662,29.043902,0 -13.718619,29.043433,0 -13.71922,29.04347,0 -13.719907,29.043001,0 -13.7204,29.042213,0
\</coordinates\>
\</LineString\>
\</Placemark\>  
\<Document\>        
\<name\>Traverse path TRV_1_PATH_3\</name\>         
\<Placemark\>           
\<name\>Traverse path TRV_1_PATH_3\</name\>             
\<LineString\>              
\<coordinates\>                     
\-13.7204,29.042138,0 -13.720615,29.041407,0 -13.721237,29.041144,0 -13.721838,29.041275,0 -13.722396,29.040994,0 -13.723104,29.041613,0 -13.725228,29.042945,0 -13.727052,29.043977,0 -13.729327,29.044521,0 -13.731387,29.044352,0 -13.732653,29.043414,0 -13.733554,29.04197,0
\</coordinates\>                    
\</LineString\>                 
\</Placemark\>  
\<Document\>        
\<name\>Traverse path TRV_1_PATH_4\</name\>         
\<Placemark\>           
\<name\>Traverse path TRV_1_PATH_4\</name\>             
\<LineString\>              
\<coordinates\>                     
\-13.733683,29.041913,0 -13.734305,29.041763,0 -13.734434,29.042026,0 -13.73394,29.043076,0 -13.733554,29.044202,0 -13.733447,29.045252,0
\</coordinates\>                    
\</LineString\>                 
\</Placemark\>
\</Document\>
\</kml\>

I tried with posite lookahead but didnt work :

let test = myxml.replace((/(?!^)<Document>/g, '')); console.log(test);

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Any idea ?

>Solution :

Although your xml format is valid. I try to delete all beside the first.

var documentCount = 0;
var str = `\<?xml version="1.0"?\>
\<kml xmlns="http://www.opengis.net/kml/2.2"\>  
\<Document\>        
\<name\>Traverse path TRV_1_PATH_1\</name\>
\<Placemark\>   
\<name\>Traverse path TRV_1_PATH_1\</name\>             
\<LineString\>              
\<coordinates\>                     
\-13.701582,29.043733,0 -13.702719,29.043939,0 -13.704522,29.043846,0 -13.704886,29.043939,0 -13.705208,29.043733,0 -13.705723,29.043827,0 -13.705852,29.04362,0 -13.706088,29.043789,0 -13.70656,29.043489,0 -13.707612,29.043902,0 -13.708019,29.043827,0 -13.708534,29.044296,0 -13.709564,29.044221,0 -13.710144,29.04469,0 -13.71141,29.045177,0 -13.712161,29.04514,0 -13.712611,29.045515,0 -13.713255,29.045177,0 -13.714392,29.044877,0 -13.714457,29.044446,0 -13.715315,29.044033,0
\</coordinates\>                    
\</LineString\>                 
\</Placemark\>  
\<Document\>        
\<name\>Traverse path TRV_1_PATH_2\</name\>         
\<Placemark\>           
\<name\>Traverse path TRV_1_PATH_2\</name\>             
\<LineString\>              
\<coordinates\>                     
\-13.715379,29.043996,0 -13.716795,29.044465,0 -13.718061,29.044202,0 -13.718662,29.043902,0 -13.718619,29.043433,0 -13.71922,29.04347,0 -13.719907,29.043001,0 -13.7204,29.042213,0
\</coordinates\>
\</LineString\>
\</Placemark\>  
\<Document\>        
\<name\>Traverse path TRV_1_PATH_3\</name\>         
\<Placemark\>           
\<name\>Traverse path TRV_1_PATH_3\</name\>             
\<LineString\>              
\<coordinates\>                     
\-13.7204,29.042138,0 -13.720615,29.041407,0 -13.721237,29.041144,0 -13.721838,29.041275,0 -13.722396,29.040994,0 -13.723104,29.041613,0 -13.725228,29.042945,0 -13.727052,29.043977,0 -13.729327,29.044521,0 -13.731387,29.044352,0 -13.732653,29.043414,0 -13.733554,29.04197,0
\</coordinates\>                    
\</LineString\>                 
\</Placemark\>  
\<Document\>        
\<name\>Traverse path TRV_1_PATH_4\</name\>         
\<Placemark\>           
\<name\>Traverse path TRV_1_PATH_4\</name\>             
\<LineString\>              
\<coordinates\>                     
\-13.733683,29.041913,0 -13.734305,29.041763,0 -13.734434,29.042026,0 -13.73394,29.043076,0 -13.733554,29.044202,0 -13.733447,29.045252,0
\</coordinates\>                    
\</LineString\>                 
\</Placemark\>
\</Document\>
\</kml\>`.replace(/<Document>/g, ()=>{
return documentCount++===0? '<Document>':''
})
console.log(str);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading