JavaBlog.fr / Java.lu DEVELOPMENT,Java,WEB Java/XSL: XPATH presentation and JAXP, JDOM examples

Java/XSL: XPATH presentation and JAXP, JDOM examples

Here, a mini-post to present briefly the recommandation XSL XPATH and some examples.

This article is a part of a serie of posts concerning the XSL recommendations.
From the W3C website about the The Extensible Stylesheet Language Family (XSL) http://www.w3.org/Style/XSL/:
XSL is a family of recommendations for defining XML document transformation and presentation. It consists of three parts:

  • XSL Transformations (XSLT): a language for transforming XML; An XSLT stylesheet specifies the presentation of a class of XML documents by describing how an instance of the class is transformed into an XML document that uses a formatting vocabulary, such as (X)HTML or XSL-FO. XSLT is developed by the W3C XSLT Working Group (members only) whose charter is to develop the next version of XSLT. XSLT is part of W3C’s XML Activity, whose work is described in the XML Activity Statement.
  • The XML Path Language (XPath): an expression language used by XSLT (and many other languages) to access or refer to parts of an XML document; XPath is developed jointly by the XQuery and XSLT Working Groups.
  • XSL Formatting Objects (XSL-FO): an XML vocabulary for specifying formatting semantics. XSL-FO is now developed by the XML Print and Page Layout Working Group.


Presentation
XPath is a language (not XML) to locate a portion of an XML document and it has rapidly been adopted by developers as a query language easy to use. More, XSLT uses XPath to designate a portion of an XML tree.

XPath is a specification that provides a syntax to allow you to select one or more elements in an XML document. There are seven different types of elements:

  • root,
  • element,
  • text,
  • attribute,
  • comment,
  • processing instruction,
  • name space,

In this post, we will present only the basic functionality of XPath. As an XML document may be represented as a tree consisting of nodes. Due to a special notation XPath allows to precisely locate a component of the tree:

  • the separator is the slash “/”,
  • to specify a path from the root (absolute path), it must begin with a “/”
  • two points “..” can specify the parent element of the current element,
  • a single point “.” used to specify the current element,
  • an “@” symbol is used to specify an attribute of an element,
  • use of brackets to specify the index of an element,

At last, XPath allows filtering of elements on based of different criteria in addition to their name:

  • @attribute1= “value1” search an attribute “attribute1” whose value is “value1”,
  • a vertical bar | is used to specify two values

So, in the following examples, we will use the xml File xmlFile.xml:

<?xml version="1.0" encoding="UTF-8"?>
<HUOXml>
	<creationDate>2010-09-24 07:50:57</creationDate>
	<rootTag>
		<number>32458</number>
		<docType>
			<memo>rr</memo>
			<myRQ>JAVABLOG</myRQ>
		</docType>
	</rootTag>
	<version>
		<myCode>NOHUO-</myCode>
		<languageCouples>
			<languageCouple sourceLanguage="fr" targetLanguage="bg" inform="Yes">
				<sourceLanguage>fr</sourceLanguage>
				<targetLanguage>bg</targetLanguage>
			</languageCouple>
			<languageCouple sourceLanguage="fr" targetLanguage="cs" inform="No">
				<sourceLanguage>fr</sourceLanguage>
				<targetLanguage>cs</targetLanguage>
			</languageCouple>
			<languageCouple sourceLanguage="fr" targetLanguage="it" inform="Yes">
				<sourceLanguage>fr</sourceLanguage>
				<targetLanguage>it</targetLanguage>
			</languageCouple>
			<languageCouple sourceLanguage="fr" targetLanguage="de" inform="Yes">
				<sourceLanguage>fr</sourceLanguage>
				<targetLanguage>de</targetLanguage>
			</languageCouple>
			<languageCouple sourceLanguage="fr" targetLanguage="en" inform="No">
				<sourceLanguage>fr</sourceLanguage>
				<targetLanguage>en</targetLanguage>
			</languageCouple>
			<languageCouple sourceLanguage="fr" targetLanguage="et" inform="No">
				<sourceLanguage>fr</sourceLanguage>
				<targetLanguage>et</targetLanguage>
			</languageCouple>
			<languageCouple sourceLanguage="fr" targetLanguage="fi" inform="No">
				<sourceLanguage>fr</sourceLanguage>
				<targetLanguage>fi</targetLanguage>
			</languageCouple>
			<languageCouple sourceLanguage="fr" targetLanguage="tr" inform="No">
				<sourceLanguage>fr</sourceLanguage>
				<targetLanguage>tr</targetLanguage>
			</languageCouple>
		</languageCouples>
	</version>
</HUOXml>

Example 1: API JAXP
In the below examples, we will use XPATH with the standard use of the API JAXP (included in JDK). From http://en.wikipedia.org/wiki/Java_API_for_XML_Processing:
The Java API for XML Processing, or JAXP ( /ˈdʒækspiː/ JAKS-pee), is one of the Java XML programming APIs. It provides the capability of validating and parsing XML documents. The three basic parsing interfaces are:

  • the Document Object Model parsing interface or DOM interface
  • the Simple API for XML parsing interface or SAX interface
  • the Streaming API for XML or StAX interface (part of JDK 6; separate jar available for JDK 5)

In addition to the parsing interfaces, the API provides an XSLT interface to provide data and structural transformations on an XML document.

private static String executeXpath(String entryFile, String xpathExpression){
	String result = null;
	try{
		System.out.println("------ File: '"+entryFile+"'");
		System.out.println("------ Xpath Expression: '"+xpathExpression+"'");
	
		// 1. Instantiate an XPathFactory.
		javax.xml.xpath.XPathFactory factory = javax.xml.xpath.XPathFactory.newInstance();
				  
		// 2. Use the XPathFactory to create a new XPath object
		javax.xml.xpath.XPath xpath = factory.newXPath();
				  
		// 3. Compile an XPath string into an XPathExpression
		javax.xml.xpath.XPathExpression expression = xpath.compile(xpathExpression.toString());
				  
		// 4. Evaluate the XPath expression on an input document
		result = expression.evaluate(new org.xml.sax.InputSource(entryFile));
		System.out.println("Return : " + result);
	} catch (Throwable e) {
		e.printStackTrace();
	}
	return result;
}

….so, here examples of XPATH expression with their results:

System.out.println(" -------------------- Example N1 -----------------------------");
xpathExpression = new StringBuffer("/HUOXml/rootTag/docType[memo!='AA' and memo!='aa']");
executeXpath(entryFile, xpathExpression.toString());
 -------------------- Example N1 -----------------------------
------ File: 'file:./src/com/ho/test/xpath/xmlFile.xml'
------ Xpath Expression: '/HUOXml/rootTag/docType[memo!='AA' and memo!='aa']'
Return : 
			rr
			JAVABLOG

System.out.println(" -------------------- Example N2 -----------------------------");
xpathExpression = new StringBuffer("/HUOXml[./rootTag/docType[memo!='AA' and memo!='aa'] and ./version/languageCouples/languageCouple/targetLanguage='bg']");
executeXpath(entryFile, xpathExpression.toString());
		
 -------------------- Example N2 -----------------------------
------ File: 'file:./src/com/ho/test/xpath/xmlFile.xml'
------ Xpath Expression: '/HUOXml[./rootTag/docType[memo!='AA' and memo!='aa'] and ./version/languageCouples/languageCouple/targetLanguage='bg']'
Return : 
	2010-09-24 07:50:57
	
		32458
		
			rr
			JAVABLOG
		
	
	
		NOHUO-
		
			
				fr
				bg
			
			
				fr
				cs
			
			
				fr
				it
			
			
				fr
				de
			
			
				fr
				en
			
			
				fr
				et
			
			
				fr
				fi
			
			
				fr
				tr

System.out.println(" -------------------- Example N3 -----------------------------");
xpathExpression = new StringBuffer("/HUOXml/rootTag/docType[myRQ = 'JAVABLOG']");
executeXpath(entryFile, xpathExpression.toString());
 -------------------- Example N3 -----------------------------
------ File: 'file:./src/com/ho/test/xpath/xmlFile.xml'
------ Xpath Expression: '/HUOXml/rootTag/docType[myRQ = 'JAVABLOG']'
Return : 
			rr
			JAVABLOG

System.out.println(" -------------------- Example N4 -----------------------------");
xpathExpression = new StringBuffer("/HUOXml/version/languageCouples/languageCouple[./targetLanguage = 'en']");
executeXpath(entryFile, xpathExpression.toString());
 -------------------- Example N4 -----------------------------
------ File: 'file:./src/com/ho/test/xpath/xmlFile.xml'
------ Xpath Expression: '/HUOXml/version/languageCouples/languageCouple[./targetLanguage = 'en']'
Return : 
				fr
				en

System.out.println(" -------------------- Example N5 -----------------------------");
xpathExpression = new StringBuffer("/HUOXml[./rootTag/docType[memo!='RRRR' and memo!='rrrr' ] " +
" and ./version/languageCouples/languageCouple[(./@targetLanguage='IT' or ./@targetLanguage='it') and (./@inform='Yes')] " +
" and ./version[( ( starts-with(./myCode,'huo-') or  starts-with(./myCode,'HUO-') )=false )]]");
executeXpath(entryFile, xpathExpression.toString());
	
 -------------------- Example N5 -----------------------------
------ File: 'file:./src/com/ho/test/xpath/xmlFile.xml'
------ Xpath Expression: '/HUOXml[./rootTag/docType[memo!='RRRR' and memo!='rrrr' ]  and ./version/languageCouples/languageCouple[(./@targetLanguage='IT' or ./@targetLanguage='it') and (./@inform='Yes')]  and ./version[( ( starts-with(./myCode,'huo-') or  starts-with(./myCode,'HUO-') )=false )]]'
Return : 
	2010-09-24 07:50:57
	
		32458
		
			rr
			JAVABLOG
		
	
	
		NOHUO-
		
			
				fr
				bg
			
			
				fr
				cs
			
			
				fr
				it
			
			
				fr
				de
			
			
				fr
				en
			
			
				fr
				et
			
			
				fr
				fi
			
			
				fr
				tr

Example 2: API JDOM
In the below examples, we will use XPATH with the standard use of the API JDOM (jdom.jar).
From http://en.wikipedia.org/wiki/JDOM:
JDOM is an open source Java-based document object model for XML that was designed specifically for the Java platform so that it can take advantage of its language features. JDOM integrates with Document Object Model (DOM) and Simple API for XML (SAX), supports XPath and XSLT. It uses external parsers to build documents.

We have used the JDOM with JAXEN which is an open source XPath library written in Java. It is adaptable to many different object models, including DOM, XOM, dom4j, and JDOM. Is it also possible to write adapters that treat non-XML trees such as compiled Java byte code or Java beans as XML, thus enabling you to query these trees with XPath too.

So, this method uses the jdom.jar(v. 1.1.1) and jaxen.jar (v. 1.1.1) libraries.

private static void executeXpath(String entryFileName, String xpathExpression){
	try{
		File entryFile = new File(entryFileName);
			
		System.out.println("------ File: '"+entryFileName+"'");
		System.out.println("------ Xpath Expression: '"+xpathExpression+"'");

		org.jdom.Document document = null;
	
		/* Create instance of SAXBuilder */
		SAXBuilder sxb = new SAXBuilder();
		document = sxb.build(entryFile);
			
		/* Initializes a new element with the root element of document. */
		Element racine = document.getRootElement();
		XPath xpa = XPath.newInstance(xpathExpression.toString());
	
		List results = xpa.selectNodes(racine);
		Iterator iter = results.iterator();
		Element noeudCourant = null;
		if(iter.hasNext()) {
			while (iter.hasNext()) {
				noeudCourant = (Element) iter.next();
				System.out.println("Value : " + noeudCourant.getValue());
			}
		}else{
			System.out.println("Value : No found!!");
				
		}
		
	} catch (Throwable e) {
		e.printStackTrace();
	}
}

….so, here examples of XPATH expression with their results:

System.out.println(" -------------------- Example N1 -----------------------------");
StringBuffer xpathExpression = new StringBuffer("/HUOXml/rootTag/docType[myRQ = 'JAVABLOG']");
TestXPath_JDOMChecker.executeXpath(entryFileName, xpathExpression.toString());
 -------------------- Example N1 -----------------------------
------ File: './src/com/ho/test/xpath/xmlFile.xml'
------ Xpath Expression: '/HUOXml/rootTag/docType[myRQ = 'JAVABLOG']'
Value : 
			rr
			JAVABLOG

System.out.println(" -------------------- Example N2 -----------------------------");
StringBuffer xpathExpression = new StringBuffer("/HUOXml/rootTag/docType[myRQ = 'JAVA' or myRQ = 'JAVABLOG']");
TestXPath_JDOMChecker.executeXpath(entryFileName, xpathExpression.toString());
 -------------------- Example N2 -----------------------------
------ File: './src/com/ho/test/xpath/xmlFile.xml'
------ Xpath Expression: '/HUOXml/rootTag/docType[myRQ = 'JAVA' or myRQ = 'JAVABLOG']'
Value : 
			rr
			JAVABLOG

System.out.println(" -------------------- Example N3 -----------------------------");
StringBuffer xpathExpression = new StringBuffer("/HUOXml/rootTag/docType[myRQ != 'TUTU' and myRQ != 'TODO']");
TestXPath_JDOMChecker.executeXpath(entryFileName, xpathExpression.toString());
 -------------------- Example N3 -----------------------------
------ File: './src/com/ho/test/xpath/xmlFile.xml'
------ Xpath Expression: '/HUOXml/rootTag/docType[myRQ != 'TUTU' and myRQ != 'TODO']'
Value : 
			rr
			JAVABLOG

System.out.println(" -------------------- Example N4 -----------------------------");
StringBuffer xpathExpression = new StringBuffer("/HUOXml/version/languageCouples/languageCouple[@targetLanguage = 'fr']");
TestXPath_JDOMChecker.executeXpath(entryFileName, xpathExpression.toString());
 -------------------- Example N4 -----------------------------
------ File: './src/com/ho/test/xpath/xmlFile.xml'
------ Xpath Expression: '/HUOXml/version/languageCouples/languageCouple[@targetLanguage = 'fr']'
Value : No found!!

System.out.println(" -------------------- Example N5 -----------------------------");
StringBuffer xpathExpression = new StringBuffer("/HUOXml/rootTag");
xpathExpression.append("[");
xpathExpression.append("./docType/myRQ != 'TODO'");
xpathExpression.append(" and /HUOXml/version/languageCouples/languageCouple[@targetLanguage='bg' and @inform='Yes'] ");
xpathExpression.append("]/number");
TestXPath_JDOMChecker.executeXpath(entryFileName, xpathExpression.toString());
 -------------------- Example N5 -----------------------------
------ File: './src/com/ho/test/xpath/xmlFile.xml'
------ Xpath Expression: '/HUOXml/rootTag[./docType/myRQ != 'TODO' and /HUOXml/version/languageCouples/languageCouple[@targetLanguage='bg' and @inform='Yes'] ]/number'
Value : 32458

Source: test_xml_xpath.zip

That’s all!!!

Huseyin OZVEREN

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.

Related Post