processing xml documents with oracle jdeveloper 11g · pdf fileprocessing xml documents with...

Download Processing XML documents with Oracle JDeveloper 11g · PDF fileProcessing XML documents with Oracle JDeveloper 11g ... ... We create PDF reports in JDeveloper 11g using the Oracle

If you can't read please download the document

Upload: ngobao

Post on 08-Feb-2018

266 views

Category:

Documents


4 download

TRANSCRIPT

  • Processing XML documents with Oracle JDeveloper 11g

    Deepak Vohra

    Chapter No. 4 "XPath"

  • For More Information: www.packtpub.com/processing-xml-documents-with-oracle-jdeveloper-11g/book

    In this package, you will find: A Biography of the author of the book

    A preview chapter from the book, Chapter NO.4 "XPath"

    A synopsis of the books content

    Information on where to buy this book

    About the Author Deepak Vohra is a consultant and a principal member of the NuBean software company. Deepak is a Sun Certified Java Programmer and a Web Component Developer, and has worked in the fields of XML and Java programming and J2EE for over fi ve years. Deepak is the author of the book JDBC 4.0 and Oracle JDeveloper for J2EE Development published by Packt Publishing.

    http://www.packtpub.com/processing-xml-documents-with-oracle-jdeveloper-11g/book

  • For More Information: www.packtpub.com/processing-xml-documents-with-oracle-jdeveloper-11g/book

    Processing XML documents with Oracle JDeveloper 11g While a number of books on XML are available, none covers XML support in Oracle JDeveloper. Welcome to Processing XML documents with Oracle JDeveloper 11g, a book that will teach you about using Oracle XML technologies in Oracle JDeveloper. XML is the standard medium of data exchange. Examples of data exchange using XML are web feeds, which include RSS feeds and Atom feeds, and XML messages in web services. Java is the language most commonly used to process XML. Among the IDEs, Oracle JDeveloper has the most XML features. Some of the graphical XML features in JDeveloper are an XML editor to create an XML document, an XML schema editor to model an XML schema, and an XPath explorer to process XPath expressions. JDeveloper also provides built-in support for JAXB compilation. JDeveloper includes an integrated application serverthe WebLogic Serverfor running XML-based applications. Oracle XML Developer Kit (XDK) provides a set of components, tools, and utilities for developing XML-based applications. The XDK 11g libraries are included with Oracle JDeveloper 11g.

    The objective of this book is to discuss XML development in Oracle JDeveloper. We shall use JDeveloper 11g, the latest version of JDeveloper. As developers commonly use an IDE for processing XML and developing XML applications, the book covers all aspects of XML development, which include:

    Creating an XML document

    Validating an XML document with an XML schema

    Transforming an XML document

    Addressing elements/attributes in an XML document using XPath

    We shall use the Oracle XDK 11g for Java to develop XML applications. We shall also discuss Oracle XML Publisher and Oracle Berkeley DB XML. By the end of this book, you should know everything there is to know about XML and JDeveloper.

    What This Book Covers Chapter 1: We discuss creating an XML document using Oracle's XML Developer Kit (XDK) 11g in Oracle JDeveloper. We also discuss parsing an XML document using SAX and DOM Java APIs, which are also included in XDK 11g.

    Chapter 2: We create an XML schema in the XML schema editor of Oracle JDeveloper. An XML schema represents the structure of an XML document. Subsequently, we instantiate an XML document from the XML schema.

    http://www.packtpub.com/processing-xml-documents-with-oracle-jdeveloper-11g/book

  • For More Information: www.packtpub.com/processing-xml-documents-with-oracle-jdeveloper-11g/book

    Chapter 3: We discuss validating an XML document with an XML schema using the built-in feature to validate the schema and the XDK 11g schema validation APIs. We discuss three different APIs for schema validation: the XSDValidator, the SAX parser, and the DOM parser.

    Chapter 4: We discuss XPath, which is used to address nodes in an XML document. We use XPath in the XPath Search GUI tool in Oracle JDeveloper 11g. We also use the XPath Java API in XDK 11g.

    Chapter 5: We transform an XML document using the Transformation API for XML (TrAX), which is included in XDK 11g. We also discuss the XSLT extension functions.

    Chapter 6: We parse and transform XML using the JSTL XML tag library in JDeveloper 11g.

    Chapter 7: We load, save, and filter an XML document using the DOM 3.0 Load and Save APIs, which are provided in XDK 11g.

    Chapter 8: We construct and validate an XML document using the DOM 3.0 Validation API, which is also included in XDK 11g.

    Chapter 9: We discuss another built-in feature of JDeveloper 11g, the JAXB 2.0 compiler. We bind an XML schema to Java classes using the JAXB 2.0 compiler. Subsequently, we unmarshal an XML document and marshal an XML document using the compiled Java classes.

    Chapter 10: We compare XML documents using the XMLDiff Java API included in XDK 11g.

    Chapter 11: We convert an XML document to a PDF document using the Apache FOP Java API in JDeveloper.

    Chapter 12: We create an MS Excel spreadsheet from an XML document in JDeveloper using the Apache POI Java API.

    Chapter 13: We store an XML document in Oracle Berkeley DB XML, and subsequently query and update the XML document using both the Berkeley DB XML command shell and the Berkeley DB XML Java API. The Berkeley DB XML API is used in JDeveloper 11g.

    Chapter 14: We create PDF reports in JDeveloper 11g using the Oracle XML Publisher Java API. We also merge PDF documents. We also create an XML report from a database table using the Data Engine API.

    http://www.packtpub.com/processing-xml-documents-with-oracle-jdeveloper-11g/book

  • For More Information: www.packtpub.com/processing-xml-documents-with-oracle-jdeveloper-11g/book

    XPathAs mentioned in the earlier chapters, XML documents can be used for the transfer of data. The data in an XML document may be retrieved either with the JAXP (Java API for XML Processing) DOM and SAX APIs, or with the JAXP XPath API. Addressing an XML document with XPath has the advantage that a single node may be selected directly without iterating over a node set. With SAX and DOM APIs, node lists have to be iterated over to access a particular node. Another advantage of navigating an XML document with XPath is that an attribute node may be selected directly. With DOM and SAX APIs, an element node has to be selected before an element attribute can be selected. In this chapter we shall discuss XPath support in JDeveloper.

    What is XPath? XPath is a language for addressing an XML document's elements and attributes. As an example, say you receive an XML document that contains the details of a shipment and you want to retrieve the element/attribute values from the XML document. You don't just want to list the values of all the nodes, but also want to output the values of specifi c elements or attributes. In such a case, you would use XPath to retrieve the values of those elements and attributes. XPath constructs a hierarchical structure of an XML document, a tree of nodes, which is the XPath data model. The XPath data model consists of seven node types. The different types of nodes in the XPath data model are discussed in the following table:

    Node Type DescriptionRoot Node The root node is the root of the DOM tree. The document element

    (the root element) is a child of the root node. The root node also has the processing instructions and comments as child nodes.

    Element Node This represents an element in an XML document. The character data, elements, processing instructions, and comments within an element are the child nodes of the element node.

    http://www.packtpub.com/processing-xml-documents-with-oracle-jdeveloper-11g/book

  • For More Information: www.packtpub.com/processing-xml-documents-with-oracle-jdeveloper-11g/book

    XPath

    [ 110 ]

    Node Type DescriptionAttribute Node This represents an attribute other than the xmlns-prefi xed

    attribute, which declares a namespace.Text Node The character data within an element is a text node. A text

    node has at least one character of data. A whitespace is also considered as a character of data. By default, the ignorable whitespace after the end of an element and before the start of the following element is also a text node. The ignorable whitespace can be excluded from the DOM tree built by parsing an XML document. This can be done by setting the whitespace-preserving mode to false with the setPreserveWhitespace(boolean flag) method.

    Comment Node This represents a comment in an XML document, except the comments within the DOCTYPE declaration.

    Processing Instruction Node

    This represents a processing instruction in an XML document except the processing instruction within the DOCTYPE declaration. The XML declaration is not considered as a processing instruction node.

    Namespace Node This represents a namespace mapping, which consists of a xmlns:-prefi xed attribute such as xmlns:xsd="http://www.w3.org/2001/XMLSchema". A namespace node consists of a namespace prefi x (xsd in the example) and a namespace URI (http://www.w3.org/2001/XMLSchema in the example).

    Specifi c nodes including element, attribute, and text nodes may be accessed with XPath. XPath supports nodes in a namespace. Nodes in XPath are selected with an XPath expression. An expression is evaluated to yield an object of one of the following four types: node set, Boolean, number, or string.

    For an introduction on XPath refer to the W3C Recommendation for XPath (http://www.w3.org/TR/xpath). As a brief review, expression evaluation in XPath is performed with respect to a context node. The most commonly used type of expression in XPath is a location path . XPath defi nes two types of location paths: relative location paths and absolute location paths. A relative location path is defi ned with respect to a context n