• 0.19

How to apply XSL to XML

You need two classes: XMLDocument and XSLDocument which implement XML and XSL interfaces respectfully. They work with your default DOM implementation. For example:

import com.jcabi.xml.XML;
import com.jcabi.xml.XMLDocument;
import com.jcabi.xml.XSL;
import com.jcabi.xml.XSLDocument;
public class Main {
  public void main(String[] args) {
    XML first = new XMLDocument(
      "<orders><order id="4">Coffee to go</order></orders>"
    );
    String xsl = "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='2.0'>",
      + "<xsl:template match='/'>"
      + "  <name><xsl:value-of select='/orders/order[@id=4]'/></name>",
      + "</xsl:template>"
      + "</xsl:stylesheet>";
    XML second = new XSLDocument(xsl).transform(first);
    System.out.println(second.toString());
  }
}

Prints:

<name>Coffee to go</name>