Alan Hay
Alan Hay

Reputation: 23226

JSoup Not Producing Valid XHTML

I am using JSoup to dynamically set the href attribute of a <base/> element in an HTML document. This works as expected apart from the fact the closing </base> tag is omitted from the modified HTML.

Is there any way to have JSOUP return valid XHTML?

Input:

<html><head><base href="xyz"/></head><body></body></html>

Output:

<html>
 <head>
  <base href="https://myhost:8080/myapp/"> <-- missing closing tag
 </head>
 <body></body>
</html>

Code:

  protected String modifyHtml(HttpServletRequest request, String html)
  {
    Document document = Jsoup.parse(html);
    document.outputSettings().escapeMode(EscapeMode.xhtml);
    Elements baseElements = document.select("base");

    if (!baseElements.isEmpty())
    {
      Element base = baseElements.get(0);
      base.attr("href", getBaseUrl(request));
    }

    return document.html();
  }

Upvotes: 2

Views: 480

Answers (1)

Codo
Codo

Reputation: 78835

In addition to (or instead of) the escape mode, you want to set the syntax:

document.outputSettings().syntax(Document.OutputSettings.Syntax.xml);

Upvotes: 3

Related Questions