-1

I'm having an issue with some XML when processing it with my Javascript, because the Node modules (libxslt & libxmljs) don't know how to handle a self closing tag. Through some different testing I have narrowed the problem down to XML elements that self close, like the center element in the example below:

var string = 

"<head>
    <body>
       <example />
    </body>
</head>"

Simply put, I need a way of removing

<example /> 

entirely; without knowing the position prior, because there are multiple in a document, and without addressing the tag name directly, because the self closing tags vary from document to document.

If .replace() obtains the location ID of the parameter, it could be used with a function as the second parameter. Something like this:

string.replace('/>', function(match){
    //search from match back for the closest '<' and remove that substring.
})
Mohammad Sadiqur Rahman
  • 5,379
  • 7
  • 31
  • 45
Naxlin
  • 1
  • 2
  • 7
    find an XML parser that _does_ understand self-closing tags and forget trying to use regexs on XML content. – Alnitak Dec 08 '16 at 09:10
  • 2
    "the Node modules (libxslt & libxmljs) don't know how to handle a self closing tag" — They do. There must be something wrong with how you are using them. – Quentin Dec 08 '16 at 09:11
  • "in the example below" — That won't compile. *SyntaxError: Invalid or unexpected token* – Quentin Dec 08 '16 at 09:12
  • `There must be something wrong with how you are using them` - perhaps the OP could show how he's using them and thus avoid the temptation of [summoning zalgo](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) – Jaromanda X Dec 08 '16 at 09:39

2 Answers2

0

Thanks all for the advice; particularly to @Tonioyoyo, his led to solving my question, solution below:

//Xml with random element tags
var xml = "<head><body><example1 /><example2 /><example3 /></body></head>"

//Convert to string
xml = xml.toString();

//Create pattern variable to match self-closing elements
var myRegexp = /.*?(\<\w+\s*\/\>).*/

//Removing all problem elements
var match = myRegexp.exec(xml);
while (match != null && match[1] != null) {
    xml = xml.replace(match[1], '')
    match = myRegexp.exec(xml);
}

//Log result
console.log(xml);

However, the real problem turned out to be a comma getting added, like so:

<opti,ons/> 

When porting from SQL to Node.js using node package 'mssql', (the comma was not in the source SQL), which produced the mismatching tags error. Using:

xml.toString();
xml.replace(<opti,ons/>, ''); //Fixes the mismatch tags error.

This means that @Quentin is correct the Node modules libxslt & libxmljs do know how to deal with self closing tags, as the added comma was the problem not the tags.

Naxlin
  • 1
  • 2
-1

You can write your own regular expression to capture either self closing tags or code between classic tags.

For instance, if you do:

var string = 
"<head>
    <body>
       <example />
    </body>
</head>"

var pattern = /<(.*) \/>/;
var result = string.replace(pattern, '');

You will end up with your string value equals to:

<head>
    <body>

    </body>
</head>

And if you want to test your regular expression online, you may want to visit https://regex101.com/ (you can test for Javascript language)

Hope this helps :)

Antoine
  • 1,393
  • 4
  • 20
  • 26
  • Oh dear - [he comes](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) – Jaromanda X Dec 08 '16 at 09:51