<%@ taglib uri="http://jakarta.apache.org/taglibs/xtags-1.0" prefix="xtags" %> <%@ page import="java.io.*" %> <%@ page import="java.net.*" %> <%@ page import="java.util.*" %> <%@ page import="org.dom4j.*" %> <%@ page import="org.dom4j.io.*" %> <%@ page import="org.apache.taglibs.xtags.util.StringHelper" %>

XPath evaluator

This will evaluate an XPath expression on a given document (which can be any valid URL to an XML document) and an optional context XPath expression. The context XPath expression allows an XPath expression to be applied to a certain part of the document.


<% String uri = request.getParameter("uri"); if ( uri == null ) { //uri = "http://p.moreover.com/cgi-local/page?c=Java%20news&o=rss"; uri = "/much_ado.xml"; } String contextPath = request.getParameter("contextPath"); if ( contextPath == null ) { contextPath = "."; } String path = request.getParameter("path"); if ( path == null ) { path = "//STAGEDIR"; } String sortText = request.getParameter("sort"); boolean sort = false; if ( sortText == null ) { sortText = ""; } else { sort = sortText.equalsIgnoreCase( "true" ); } String sortPath = request.getParameter("sortPath"); if ( sortPath == null ) { sortPath = "."; } String distinctText = request.getParameter("distinct"); boolean distinct = false; if ( distinctText == null ) { distinctText = ""; } else { distinct = distinctText.equalsIgnoreCase( "true" ); } %>
Document URI
Context XPath expression
XPath expression to process
Sort results /> Sort by XPath expression Distinct values (when sorting) />

<% String fullURI = uri; boolean isFullURL = true; if ( uri != null && uri.length() > 0 ) { // if no absolute URL is being used if ( uri.indexOf( ':' ) < 0 ) { isFullURL = false; String root = request.getContextPath(); if ( root != null ) { fullURI = root + uri; } } String asXmlUrl = ""; if ( path != null && path.length() > 0 ) { asXmlUrl = "xpath.xml?uri=" + java.net.URLEncoder.encode(uri) + "&path=" + path + "&contextPath=" + contextPath; if ( sortPath != null ) { asXmlUrl += "&sortPath=" + sortPath; } if ( sort ) { asXmlUrl += "&sort=true"; } if ( distinct ) { asXmlUrl += "&distinct=true"; } } %>

View source XML

View results as XML


Results

<% System.out.println( "About to parse: " + uri ); if ( isFullURL ) { %> <% } else { %> <% } %> <% String text = ""; if ( result instanceof String ) { text = (String) result; } else if ( result instanceof Node ) { Node node = (Node) result; text = StringHelper.encodeHTML(node.asXML()); } else if ( result != null ) { text = result.toString(); } %> <% } %>
<%= text %>