Introduction to XPath Transparency No. 1 Introduction to XPath API Cheng-Chia Chen.

Slides:



Advertisements
Similar presentations
Sequence of characters Generalized form Expresses Pattern of strings in a Generalized notation.
Advertisements

Based on Java Software Development, 5th Ed. By Lewis &Loftus
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Singleton vs utility class  at first glance, the singleton pattern does not seem to offer any advantages to using a utility class  i.e., a utility class.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Road Map Introduction to object oriented programming. Classes
JAXP Transformation Package and Xalan Extensions 黃立昇
Introduction to XLink Transparency No. 1 XML Information Set W3C Recommendation 24 October 2001 (1stEdition) 4 February 2004 (2ndEdition) Cheng-Chia Chen.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Scott Grissom, copyright 2004Ch 3: Java Features Slide 1 Why Java? It is object-oriented provides many ready to use classes platform independent modern.
Initialization George Blank Subba Reddy Daka. Importance of Initialization Java classes are initialized and have predictable default values. These values.
Xpath Sources:
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
1 XML at a neighborhood university near you Innovation 2005 September 16, 2005 Kwok-Bun Yue University of Houston-Clear Lake.
XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks.
CS-434: Object-Oriented Programming Using Java Week 2
Java WWW Week 10 Version 2.1 Mar 2008 Slide Java (JSP) and XML  Format of lecture: What is XML? A sample XML file… How to use.
Representing Web Data: XML CSI 3140 WWW Structures, Techniques and Standards.
Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 1 of 37 J2EE Web Components Pre-assessment Questions 1.The _____________ attribute of a JSP page.
Representing Web Data: XML CSI 3140 WWW Structures, Techniques and Standards.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Lecture objectives  Differences between Java and C#  Understand and use abstract classes  Casting in Java: why is it important?  Master exceptions.
Processing of structured documents Spring 2002, Part 2 Helena Ahonen-Myka.
XPath. Why XPath? Common syntax, semantics for [XSLT] [XPointer][XSLT] [XPointer] Used to address parts of an XML document Provides basic facilities for.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
Utilities (Part 2) Implementing static features 1.
1 SystemVerilog Enhancement Requests Daniel Schostak Principal Engineer February 26 th 2010.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Programming in Java CSCI-2220 Object Oriented Programming.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
XML and SAX (A quick overview) ● What is XML? ● What are SAX and DOM? ● Using SAX.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
1 JAXP & XPATH. Objectives 2  XPath  JAXP Processing of XPath  Workshops.
CS 157B: Database Management Systems II February 13 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
When we create.rtf document apart from saving the actual info the tool saves additional info like start of a paragraph, bold, size of the font.. Etc. This.
1 Introduction JAXP. Objectives  XML Parser  Parsing and Parsers  JAXP interfaces  Workshops 2.
Difficult Specifications in Javari Immutability Inference Jaime Quinonez Program Analysis Group April 20, 2007.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Chapter 7 Representing Web Data:
1 Validation SAX-DOM. Objectives 2  Schema Validation Framework  XML Validation After Transformation  Workshops.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Modern Programming Tools And Techniques-I
Unit 4 Representing Web Data: XML
Road Map Introduction to object oriented programming. Classes
{ XML Technologies } BY: DR. M’HAMED MATAOUI
Data Modeling II XML Schema & JAXB Marc Dumontier May 4, 2004
Lecture 4 Using Classes Richard Gesick.
Chapter 7 Representing Web Data: XML
CSE 1030: Implementing Non-Static Features
Implementing Non-Static Features
Interfaces.
XML document processing in Java using XPath and XSLT
Presentation transcript:

Introduction to XPath Transparency No. 1 Introduction to XPath API Cheng-Chia Chen

Introduction to XPath Transparency No. 2 Java API for XPath  Part of J2se5.0 and JAXP 1.3J2se5.0JAXP 1.3  included in a package : javax.xml.xpath Interfaces XPath XPathExpression XPathFunction XPathFunctionResolver XPathVariableResolver XPath XPathExpression XPathFunction XPathFunctionResolver XPathVariableResolver Classes XPathConstants XPathFactory XPathConstants XPathFactory Exceptions XPathException XPathExpressionException XPathFactoryConfigurationException XPathFunctionException XPathException XPathExpressionException XPathFactoryConfigurationException XPathFunctionException

Introduction to XPath Transparency No. 3 Using pattern of the XPath API Path xpath = XPathFactory.newInstance().newXPath(); String expression = "/widgets/widget"; InputSource inputSource = new InputSource("widgets.xml"); NodeList nodes = (NodeList) xpath.evaluate( expression, inputSource, XPathConstants.NODESET);

Introduction to XPath Transparency No. 4 XPath Expressions and Types  the XPath API allows the selected nodes to be coalesced into one of the following other data types: Boolean, Number, String.  The desired return type specified by a QName parameter in method calls of either XPathExpression.evalute(...) or one of the XPath.evaluate(...) convenience methods.  The allowed QName values are specified as constants in the XPathConstants class; they are: XPathConstants.NODESET,.NODE,  return first Node,.STRING  return string value of retruned node set.BOOLEAN  return Boolean.TRUE (FALSE),.NUMBER  return double

Introduction to XPath Transparency No. 5 XPath context  XPath location paths may be relative to a particular node in the document, known as the context node.  Consider the following XML document:  The element can be selected with the following XPath API code:

Introduction to XPath Transparency No. 6 // parse the XML as a W3C Document DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document document = builder.parse(new File("/widgets.xml")); XPath xpath = XPathFactory.newInstance().newXPath(); String expression = "/widgets/widget"; // absolute location path Node widgetNode = (Node) xpath.evaluate(expression, document, XPathConstants.NODE); // With a reference to the element, a relative XPath expression can now written to select the child element: XPath xpath = XPathFactory.newInstance().newXPath(); // relative location path needa context node String expression = "manufacturer"; Node manufacturerNode = (Node) xpath.evaluate(expression, widgetNode, XPathConstants.NODE);

Introduction to XPath Transparency No. 7 Class Summary class XPathFactory  public static newInstance( [String modelUri ]) Get a new XPathFactory instance using the specified object model. default: XPathFactory.DEFAULT_OBJECT_MODEL_URI (the W3C DOM)DEFAULT_OBJECT_MODEL_URI looking up places and order:  ClassName given by the SystemProperty DEFAULT_PROPERTY_NAME [=“javax.xml.xpathXPathFactory”] + “:” + modelURI  property given in ${java.home}/lib/jaxp.properties  file named javax.xml.xpath.XPathFactory given in META-INF/services.  Platform default XPathFactory  Throw XPathFactoryConfigurationException  getFeature(String) /setFeature(String uri, boolean)

Introduction to XPath Transparency No. 8  boolean isObjectModelSupported (String)String Is specified object model supported by this XPathFactory?  XPath newXPath() XPath Return a new XPath using the underlying object model determined when the XPathFactory was instantiated.  void setXPathFunctionResolver( XPathFunctionResolver resolver ) Establish a default function resolver. Any XPath objects constructed from this factory will use the specified resolver by default.  void setXPathVariableResolver( XPathVariableResolver resolver) Establish a default variable resolver.

Introduction to XPath Transparency No. 9 calss XPathConstants Field Summary  static String DOM_OBJECT_MODEL The URI for the DOM object model : "  static QName BOOLEAN The XPath 1.0 boolean data type. Mapped to java Boolean.  static QName NODE The XPath 1.0 NodeSet data type. Mapped to java org.w3c.dom.Node.  static QName NODESET The XPath 1.0 NodeSet data type. Mapped to java org.w3c.dom.NodeList.  static QName NUMBER The XPath 1.0 number data type. Mapped to java Double  static QName STRING The XPath 1.0 string data type. Mapped to Java String.

Introduction to XPath Transparency No. 10 interface javax.xml.xpath.XPath  provides access to the XPath evaluation environment and expressions.  Required properties for evaluating an xpath expression: ContextNode :  no ContextNode given  Empty DocumentNode provided  DocumentFragment treated the same as Documents. XPathVariableResolver for resolving (finding object bound to) varaibles used in the expression. XPathFunctionResolver for resolving (finding XPathFunciton bound to) fucntions used in the expression. NamespaceContext used to resolve QNames used in the expression.

Introduction to XPath Transparency No. 11  Methods: get/set NamespaceContext, XathVariableResolver and XPathFunctionResolver. getNamespaceContext() /setNamespaceContext(NamespaceContext) getXPathFunctionResolver() /setXPathFunctionResolver(XPathFunctionResolver) getXPathVariableResolver() /setXPathVariableResolver(XPathVariableResolver)  void Reset() reset the xpath to the same state as when it was created with XPathFactory.newXPath().  XPathExpression compile(String expression) XPathExpressionString Compile an XPath expression for later evaluation.

Introduction to XPath Transparency No. 12  Object evaluate(String expr, Object cxt, QName returnType) ObjectStringObjectQName Evaluate an XPath expression in the specified context and return the result as the specified type. expr or returntype null  NullPointerException cxt null  an empty doc is given as the context.  String evaluate(String expr, Object cxt)StringObject = evaluate(expr, cxt, XPathConstants.STRING )  Object evaluate(String expr, InputSource src, QName returnType) ObjectString QName Evaluate an XPath expression in the context of the specified InputSource and return the result as the specified type. expr or returntype null  NullPointerException  String evaluate(String expr, InputSource src)String = evaluate(expr, src, XPathConstants.STRING )

Introduction to XPath Transparency No. 13 Interface XPathExpression  provides access to compiled XPath expressions.  Object evaluate( Object cxt, QName returnType) Object QName Evaluate the XPathExpression in the specified context and return the result as the specified type. returntype null  NullPointerException cxt null  an empty doc is given as the context.  String evaluate( Object cxt)Object = evaluate(cxt, XPathConstants.STRING )  Object evaluate( InputSource src, QName returnType) ObjectQName Evaluate the XPathExpression in the context of the specified InputSource and return the result as the specified type. returntype null  NullPointerException  String evaluate( InputSource src) = evaluate( src, XPathConstants.STRING )

Introduction to XPath Transparency No. 14 XPathFunctionResolver  public interface XPathFunctionResolver  provides access to the set of user defined XPathFunctions.  XPath functions are resolved by name and arity. Resolver not needed for XPath built-in functions and the resolver cannot be used to override those functions.  The resolver is only called for functions in an another namespace (functions with an explicit prefix). cannot use the Resolver to implement specifications like XML-Signature Syntax and Processing which extend the function library of XPath 1.0 in the same namespace.  XPathFunction resolveFunction( QName functionName, int arity) Find a function in the set of available functions.

Introduction to XPath Transparency No. 15 XPathFunction  public interface XPathFunction provides access to XPath (external or built-in ) functions. Functions are identified by QName and arity in XPath.  Method Summary Object evaluate(List args) Evaluate the function with the specified arguments.

Introduction to XPath Transparency No. 16 XPathVariableResolver  public interface XPathVariableResolver provides access to the set of user defined XPath variables.  The XPathVariableResolver and the XPath evaluator must adhere to a contract that cannot be directly enforced by the API. Although variables may be mutable, that is, an application may wish to evaluate the same XPath expression more than once with different variable values, in the course of evaluating any single XPath expression, a variable's value must be immutable.  Object resolveVariable(QName variableName) ObjectQName Find a variable in the set of available variables. If variableName is null, then a NullPointerException is thrown.

Introduction to XPath Transparency No. 17 javax.xml.namespace  interface NamespaceContext Interface for read only XML Namespace context processing. Read-only version of NamespaceSupport of SAX2  class QName represents a qualified name as defined in the XML specifications. The value of a QName contains a Namespace URI, local part and prefix. The prefix is included in QName to retain lexical information when present in an XML input source.XML input source  The prefix is NOT used in QName.equals(Object) or to compute the QName.hashCode().QName.equals(Object) QName.hashCode() If not specified,  the Namespace URI is set to XMLConstants.NULL_NS_URI ( =“”)XMLConstants.NULL_NS_URI  the prefix is set to XMLConstants.DEFAULT_NS_PREFIX (=“”)XMLConstants.DEFAULT_NS_PREFIX QName is immutable.

Introduction to XPath Transparency No. 18 javax.xml.namespace.QName  Constructor: QName([String namespaceURI,] String localPart) QName(namespaceURI, localPart, String prefix )  Methods String getLocalPart(), getNamespaceURI(), String getPrefix() Get the various parts of this QName. boolean equals(Object qname) Test this QName for equality with another Object. retrun true iff localPart and namespaceURI parts of qname and this are all equal. (prefix not used) int hashCode() Generate the hash code for this QName.

Introduction to XPath Transparency No. 19 String toString() (standard) string representation of this QName. formats: namespaceURI != “”  "{" + namespaceURI + "}" + local Part. NamespaceURI==“”  localPart.  static QName valueOf(String qNameAsString) QName derived from parsing the formatted String. input must be in the form return by toString(). The prefix value CANNOT be represented in the String and will be set to XMLConstants.DEFAULT_NS_PREFIX.XMLConstants.DEFAULT_NS_PREFIX This method does not do full validation of the resulting QName.  The Namespace URI is not validated as a URI reference.URI reference  The local part is not validated as a NCName as specified in Namespaces in XML.NCName Namespaces in XML

Introduction to XPath Transparency No. 20 interface javax.xml.namespace.NamespaceContext  Two properties: namespaceURI prefix Method Summary  String getNamespaceURI(String prefix) Get Namespace URI bound to prefix in the current scope.  String getPrefix(String namespaceURI) Get a prefix bound to Namespace URI in the current scope.  Iterator getPrefixes(String namespaceURI) Get all prefixes bound to a Namespace URI in the current scope.

Introduction to XPath Transparency No. 21 uri and return prefix namespaceURI returned prefix  DEFAULT_NS_PREFIX =“”  bound Namespace URI any/all prefix bound to that uri  unbound URI null  XMLConstants.XML_NS_URI “xml”  XMLConstants.XMLNS_ATTRIBUTE_NS_URI “xmlns”  null IllegalArgumentException

Introduction to XPath Transparency No. 22 public final class javax.xml.XMLConstants  Utility class to contain basic XML values as constants.  DEFAULT_NS_PREFIX Prefix to use to represent the default XML Namespace. value: empty string “”.  NULL_NS_URI Namespace URI to use to represent that there is no Namespace. value: empty string “”.  XML_NS_PREFIX, XML_NS_URI The official XML Namespace prefix (=“xml”). The official XML Namespace name URI. =“  XMLNS_ATTRIBUTE, XMLNS_ATTRIBUTE_NS_URI The official XML attribute used for specifying XML Namespace declarations. (=“xmlns”) The official namespace URI (=“ the attribute XMLConstants.XMLNS_ATTRIBUTE.

Introduction to XPath Transparency No. 23  W3C_XPATH_DATATYPE_NS_URI W3C XPath Datatype Namespace URI. " defined in 2.0 only.  XML_DTD_NS_URI XML Document Type Declaration Namespace URI as an arbitrary value. "  FEATURE_SECURE_PROCESSING Feature for secure processing.  static String RELAXNG_NS_URI RELAX NG Namespace URI. Defined to be "  W3C_XML_SCHEMA_INSTANCE_NS_URI W3C XML Schema Instance Namespace URI. "  static String W3C_XML_SCHEMA_NS_URI W3C XML Schema Namespace URI. "