Perl Cd Bookshelf [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Perl Cd Bookshelf [Electronic resources] - نسخه متنی

Mark V. Scardina, Ben ChangandJinyu Wang

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید






Navigating XML with XPath


The XML Path (XPath) language provides a way to address parts of an XML document and some basic functionality for the manipulation of strings, numbers, and Booleans. XPath is also a W3C standard, currently at version 1.0 but soon to be 2.0. However, as distinct from other standards, it is designed to be used by host languages. XSLT, XPointer, and the new XQuery standards use it. Because the use of XPath with XSLT is well documented in XSLT books, we do not provide an extensive discussion here. However, to help with the examples, we provide a brief summary.

XPath models an XML document as a tree of nodes. These nodes are of the following types: root nodes, element nodes, text nodes, attribute nodes, namespace nodes, processing instruction nodes, and comment nodes. As you can see, these are the same nodes used by DOM.

XPath defines a declarative syntax to navigate these nodes to point to a document subtree or item. Referring to our sample XML document in the previous section, paths are expressed in a similar fashion to how files and URLs are expressed: the XPath / refers to the entire document, where /booklist/book/title refers to the <title> element that is a child of the book element. Attributes can also be selected using the @ symbol prepended to their name. For example, /booklist/book[@id] refers to the id attribute on the book element.

XPath also defines a way to compute a string-value for each type of node. For some node types, the string-value is part of the node; for other types of node, the string-value is computed from the string-value of descendant nodes. For nodes that have a name (for example, element nodes), XPath models these as an expanded name—that is, a name-value pair, consisting of a local name and a namespace value.


XPath Expressions


XPath expressions are the heart of XPath’s functionality and are used extensively in XSLT. When an XPath expression is evaluated, the result is an object of one of the types: node-set (an unordered collection of nodes without duplicates), Boolean (true or false), number (a floating-point number), or string (a sequence of UCS characters). Expression evaluation occurs with respect to a context, and according to a set of rules and the order in which to apply them. Since XPath is dependent on the host language, XSLT and XPointer each specify how this context is determined relative to their use in their respective languages.

The example paths in the previous sections are actually expressions when used in an XSLT stylesheet. Taking a look at our identity stylesheet, we see the following expression:

Match="*|@*|comment()|processing-instruction()|text()"

This is evaluated as matching a set of all XML items where the | signifies “or.” Therefore, it matches elements as the first *, attributes as @*, comments as comment(), processing instructions as processing-instruction, and text as text().

Expressions can be constructed not only with path syntax but also by using a rich array of functions that support operations on node sets, numbers, strings, and Booleans. Table 3-1 provides examples that could be used in a stylesheet that accepts our booklist.xml file as input.




































Table 3-1: Example Xpath Expressions

Example


Description


//book


Selects all of the <book> elements in the document


//book[@id=”121”]


Selects the <book> element whose id is 121


Book[position()=1]


Selects the first <book> in the document


//title | //author


Selects all <title> and <author> elements


number sum(//book/price)


Returns the sum of all <price> elements whose parent is <book>


string concat (//book/title,

//book/author)


Returns a concatenated list of titles and authors


Count (/booklist)


Returns a count of all books, which is 2


ancestor::book


Selects the ancestor node of <book>, which is <booklist>


These expressions are used as predicates within the XSLT language to activate a template or perform an operation on selected XML content. They also serve as parameters to XSLT functions, which are distinct from XPath functions.

/ 218