FANDOM


Parser

Identifying the implementation of JAXP in use

      System.out.printf("%1$s = %2$s\n",
            "javax.xml.parsers.SAXParserFactory", System.getProperty("javax.xml.parsers.SAXParserFactory"));
      System.out.printf("%1$s = %2$s\n",
            "javax.xml.parsers.DocumentBuilderFactory", System.getProperty("javax.xml.parsers.DocumentBuilderFactory"));
      System.out.printf("%1$s = %2$s\n",
            "javax.xml.transform.TransformerFactory", System.getProperty("javax.xml.transform.TransformerFactory"));
      System.out.printf("%1$s = %2$s\n",
            "javax.xml.xpath.XPathFactory", System.getProperty("javax.xml.xpath.XPathFactory"));
      System.out.printf("%1$s = %2$s\n",
            "javax.xml.validation.SchemaFactory", System.getProperty("javax.xml.validation.SchemaFactory"));      

      System.out.printf("SaxParserFactory implementation = %1$s\n",
            SAXParserFactory.newInstance().getClass().getName());
      System.out.printf("DocumentBuilderFactory implementation = %1$s\n",
            DocumentBuilderFactory.newInstance().getClass().getName());
      System.out.printf("TransformerFactory implementation = %1$s\n",
            TransformerFactory.newInstance().getClass().getName());
      System.out.printf("XPathFactory implementation = %1$s\n",
            XPathFactory.newInstance().getClass().getName());
      System.out.printf("SchemaFactory implementation = %1$s\n",
            SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).getClass().getName());


Schema

XQuery

Applying aggregation functions on XQuery's query

When using the aggregation function such as count with for clause, apply the function to the entire for clause, not return.

The following query returns 10 lines of 1.

FOR $x IN (1 TO 10) RETURN COUNT($x)

Maybe, if you want 10, the following query will be right.

COUNT(FOR $x IN (1 TO 10) RETURN ($x))

Other functions for sequence, such as index-of or distinct-values can be used in the same way.

Useful or intelligent expressions of XQuery

Think over the following expressions. What are they? These are from the book "XSLT Coookbook" by Sal Mangano.

(50,45,40,34,32,29,-1)[(index-of((('XXL', 'XL', 'L', 'M', 'S', 'XS')), size), 7)[1]]

The following iterates just n times not n2 times.

FOR $pos IN 1 TO COUNT($sequence), $item IN $sequence[$pos] RETURN $item , $pos

Applying XQuery on HTML

Saxon can't parse HTML file directly. But using TagSoup along with Saxon, you can parse HTML and apply XPath or XQuery to access data. The sample below shows the overall programming.

  1. package thirdstage.exercise.xml.saxon;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.net.URL;
  6.  
  7. import javax.xml.parsers.DocumentBuilder;
  8. import javax.xml.transform.Source;
  9. import javax.xml.transform.dom.DOMSource;
  10. import javax.xml.transform.sax.SAXSource;
  11.  
  12. import net.sf.saxon.Configuration;
  13. import net.sf.saxon.dom.DocumentBuilderImpl;
  14. import net.sf.saxon.lib.ParseOptions;
  15. import net.sf.saxon.lib.Validation;
  16. import net.sf.saxon.s9api.Processor;
  17. import net.sf.saxon.s9api.XQueryCompiler;
  18. import net.sf.saxon.s9api.XQueryEvaluator;
  19. import net.sf.saxon.s9api.XQueryExecutable;
  20. import net.sf.saxon.s9api.XdmNode;
  21. import net.sf.saxon.s9api.XdmValue;
  22.  
  23. import org.ccil.cowan.tagsoup.Parser;
  24. import org.testng.annotations.Test;
  25. import org.w3c.dom.Document;
  26. import org.xml.sax.InputSource;
  27. import org.xml.sax.XMLReader;
  28.  
  29. ...
  30.  
  31.   @Test
  32.   public void testXqueryOnHtmlAlongWithTagSoup() throws Exception{
  33.  
  34.     Configuration cfg = new Configuration();
  35.     cfg.setSchemaValidationMode(Validation.LAX);
  36.     cfg.setValidation(false);
  37.     cfg.setValidationWarnings(true);
  38.     Processor proc = new Processor(cfg);
  39.  
  40.     URL url = ClassLoader.getSystemResource("thirdstage/exercise/xml/saxon/krx-stock-code-only-10.html");
  41.     XMLReader xr = new org.ccil.cowan.tagsoup.Parser();
  42.     //xr.setFeature(Parser.namespacesFeature, false);
  43.     Source src = new SAXSource(xr, new InputSource(new FileInputStream(new File(url.toURI()))));
  44.     net.sf.saxon.s9api.DocumentBuilder db = proc.newDocumentBuilder();
  45.     XdmNode input = db.build(src);
  46.  
  47.     String qr = new StringBuilder()
  48.       .append("declare default element namespace \"http://www.w3.org/1999/xhtml\";\n")
  49.       .append("for $x in /html/body[1]/table[1]/tr/td[1]/child::text() return $x").toString();
  50.     XQueryCompiler xqc = proc.newXQueryCompiler();
  51.     XQueryExecutable xqe = xqc.compile(qr);
  52.     XQueryEvaluator xqev = xqe.load();
  53.     xqev.setSource(input.asSource());
  54.  
  55.     XdmValue result = xqev.evaluate();
  56.  
  57.     System.out.printf("The result of query contains %1$d items.\n", result.size());
  58.     System.out.printf(result.toString());
  59.   }
  60.  
  61. ...