There are some built in rules in exporting that automatically create Void elements / self closing tags for some tags:

ExportString[XMLElement["input", {}, {}], "HTMLFragment"]
"<input class=\"form-control\"/>" 

but the default is

ExportString[XMLElement["test", {"attr"->"val"}, {}], "HTMLFragment"]
"<test attr=\"val\"></test>" 

Q: Is there any "proper" way to tell ExportString to return "<test attr=\"val\">" or "<test attr=\"val\" />"?

Of course, one can do:

 ExportString[XMLElement["test", {"attr" -> "val"}, {}], "HTMLFragment"],
 "></" ~~ LetterCharacter .. ~~ ">" ~~ EndOfString -> ">"
"<test attr=\"val\">" 

but that's ugly.

Except of the general approach, nicer work arounds are also appreciated.

  • $\begingroup$ I think in your "ugly" solution you are missing the closing tag. Can you elaborate why this is important for you? Both forms that ExportString produced seem to be valid html and equivalent in meaning, so why do you favour the two tags variant? $\endgroup$ Commented Jan 15, 2016 at 10:52
  • $\begingroup$ @AlbertRetey the goal is to create svg primitives tags, which seem to use <rect spec /> syntax, but <rect spec></rect> seem to work too, so maybe I should not care? $\endgroup$
    – Kuba
    Commented Jan 15, 2016 at 11:10
  • $\begingroup$ I think you probably don't need to care. A potential problem might be that some code (presumably javascript) might want to deal with the result, e.g. put something into the empty element or otherwise manipulate it. But I'd be very surprised when that wouldn't work with either form as such code (if reasonably written) doesn't rely on the text representation but works with a DOM like structure... $\endgroup$ Commented Jan 15, 2016 at 11:52

2 Answers 2


This problem appears to be localized to HTMLFragment, possibly because HTML specifications before HTML5 did not use self-closing tags like XHTML and XML always have. Exporting the string as XML appears to solve the problem:

ExportString[XMLElement["test", {"attr" -> "val"}, {}], "XML"]

"<test attr='val' />"


At the cost of an extra space character (will be ignored in an html-context AFAIK):

 ExportString[XMLElement["input", {}, {" "}], "HTMLFragment"]
"<input class=\"form-control\"> </input>"

of course that does unlike your suggestion also create the closing tag, but you probably don't want to create non-XHTML-conform html-fragments these days, do you?

  • $\begingroup$ Nope, "input" has built in exceptions: try ExportString[XMLElement["test", {}, {" "}], "HTMLFragment"]. $\endgroup$
    – Kuba
    Commented Jan 15, 2016 at 11:09
  • 1
    $\begingroup$ Oh, neverming my comment. But the point is to get self closing tag for custom tag, not a full tag for built in. $\endgroup$
    – Kuba
    Commented Jan 15, 2016 at 11:11
  • 1
    $\begingroup$ @Kuba: The other way round seems to be even easier, as Picketts solution shows. I think I'll leave my answer anyway, then we have a workaround in both directions. I still think you might clarify that this seems only necessary for purly aesthetical reasons. If that would not be true, I think an example of when it matters would make sense as it would give the question somewhat more relevance... $\endgroup$ Commented Jan 15, 2016 at 11:56

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.