Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I just created gist at: <a href="https://gist.github.com/akhikhl/8070808" rel="noreferrer">https://gist.github.com/akhikhl/8070808</a> to demonstrate how such transformation is done with Groovy and JDOM2.</p> <p>Important notes:</p> <ol> <li>Groovy technically allows using any java libraries. If something cannot be done with Groovy JDK, it can be done with other library.</li> <li>jaxen library (implementing XPath) should be included explicitly (via @Grab or via maven/gradle), since it's an optional dependency of JDOM2.</li> <li>The sequence of @Grab/@GrabExclude instructions fixes the quirky dependence of jaxen on JDOM-1.0.</li> <li>XPathFactory.compile also supports variable binding and filters (see online javadoc).</li> <li>XPathExpression (which is returned by compile) supports two major functions - evaluate and evaluateFirst. evaluate always returns a list of all XML-nodes, satisfying the specified predicate, while evaluateFirst returns just the first matching XML-node.</li> </ol> <p><strong>Update</strong></p> <p>The following code:</p> <pre><code>new XMLOutputter().with { format = Format.getRawFormat() format.setLineSeparator(LineSeparator.NONE) output(doc, System.out) } </code></pre> <p>solves a problem with preserving whitespaces and line separators. getRawFormat constructs a format object that preserves whitespaces. LineSeparator.NONE instructs format object, that it should not convert line separators.</p> <p>The gist mentioned above contains this new code as well.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload