Home > Articles > Web Services > XML

XML Schema: XPath and Xpointer

  • Print
  • + Share This
Learn the basics about XPath and XPointer, and learn how to locate elements that represent schema components.
This chapter is from the book

This chapter is from the book

The XPath Recommendation describes a language for specifying sets of elements in an XML document. The XPath Recommendation provides the foundation for the XPointer Recommendation, XML Pointer Language (XPointer), which extends the XPath language to allow for specifying any part of an XML document. XPointers specifically support incorporating fragment identifiers into URI references. The XPointer Recommendation can be found at http://www.w3.org/TR/xptr/.

XML schemas use a subset of the XPath location paths for identity constraints. The XPaths specified by identity constraints (see Chapter 13) serve to locate nodes (specifically elements and attributes) within a corresponding XML instance. An XML validator examines the values of these elements and attributes to ensure uniqueness or referential integrity.

An XPointer provides the location of an entire XML schema document or merely a set of schema components. An XML validator nominally assembles an entire XML schema from a collection of XML schema documents. In fact, any XML schema document may assemble individual components from a variety of XML documents (although they do not have to be XML schema documents) by using XPointers. Specifically, an XPointer can point to a set of schema components embedded in a non-schema XML document.

This chapter is not a comprehensive tutorial on XPath or XPointer. The respective Recommendations provide lots of detail and many examples. The book presents a more complete XPath tutorial—restricted to XPath usage in identity constraints—in Chapter 13. Section 4.2 presents a number of representative examples that demonstrate locating elements that represent schema components.

4.1 XPath

Fundamentally, an XPath is an expression. Evaluating an XPath expression results in one of the following:

  • A node set
  • A Boolean
  • A floating-point number
  • A string of Unicode characters

The scope of XPath is far more extensive than the constructs covered in this section. For example, many constructs are designed to support XSLT. This section covers only those portions of XPath pertinent to XML schemas. Specifically, identity constraints require the resultant node set to contain only elements or attributes. Fragment identifiers (see Section 4.2) further restrict the resultant node set to contain only elements. Therefore, the Boolean, floating-point number, and string values are mostly not relevant in the context of XML schemas (see Section 4.1.4 for a discussion of the exceptions).

Because the remainder of this chapter primarily discusses node set results, subsequent discussion focuses on the subset of an expression known as a location path. A location path always identifies a node set.

4.1.1 XPath Location Paths

Location paths nominally provide the grammar for typical XPath expressions for XML schemas. In an XML schema, all location paths are either relative to an enclosing component (for identity constraints) or relative to an entire XML document (for locating schema components). One of the general features of a location path is the ability to navigate along a number of axes. An axis specifies a direction of movement in the node tree. For example, you might specify a child node, an attribute node, an ancestor node, or a descendant node.

The XPath Recommendation defines 13 axes. An identity constraint is limited to containing only the axes child, attribute, and descendant-or-self. Furthermore, an identity constraint can only use the shortcut notation for these axes. Table 4.1 portrays these shortcuts (Table 4.3 contains the entire list of axes).

One schema document can locate another (or even parts of another) with an XPointer. An XPointer may specify any of the 13 axes in a predicate (see Sections 4.1.2 and 4.2 for more detail). Although technically feasible, not all the axes are particularly valuable with respect to schema document locations. Table 4.3 illuminates the axes with dubious merit.

4.1.2 Predicates

Predicates are very powerful, but slightly confusing when first encountered. A predicate is strictly a filter. A predicate filters out desired nodes from a node set.

TIP

In an XML schema, an XPath expression always results in a node set. An expression with a predicate also results in a node set. Specifically, an expression with a predicate results in a subset of the node set that corresponds to the same expression without the predicate.

The easiest way to demonstrate a predicate is to discuss two similar expressions along multiple axes. For demonstration purposes, consider a catalog consisting of several parts:

<catalog>
   <partNumber SKU="S1234">P1234</partNumber>
   <partNumber>P2222</partNumber>
   <partNumber SKU="Sabcd">Pabcd</partNumber>
</catalog>

The ensuing example returns a node set that contains all partNumber elements in a document:

//partNumber 

The corresponding node set contains the partNumber elements whose values are 'P1234', 'P2222', and 'Pabcd'.

An addition to the location path along an attribute axis results in a node set that contains all SKU attributes of partNumber elements:

//partNumber/@SKU 

The corresponding node set contains the SKU attributes whose values are 'S1234' and 'Sabcd'.

The predicate in the next example, on the other hand, results in a subset of the //partNumber node set. In particular, the subsequent example returns partNumber elements that have SKU attributes, not the attributes as in the previous example.

//partNumber[@SKU] 

The corresponding node set contains the partNumber elements whose values are 'P1234' and 'Pabcd'. The partNumber element whose value is 'P2222' is not included, because this element has no SKU attribute.

An XPath expression, in general, can return any node set, a Boolean value, a string, or a floating-point number. In an XML schema, the results of an XPath expression (in an identity constraint), as well as the results of an XPointer expression (in a schemaLocation attribute value), must result in a node set. Because of this, this chapter does not go into detail describing the other result types. However, a predicate can refine a node set in an XPointer expression. Therefore, Table 4.4, which appears later in this chapter, provides a few examples of XPointers that contain predicates that return values that are not node sets. Because very few schemas contain XPointers, and even fewer contain complex XPointers, this chapter does not provide a comprehensive tutorial on predicates.

TIP

The Schema Recommendation does not support the use of predicates within identity constraints. Within an XML schema, predicates are valid only as part of an XPointer expression, which can be used only in conjunction with schema document (and part-of-document) locations.

4.1.3 Node IDs

An XPath expression may include any number of "functions" that either extract information from an XML document or help to restrict the resultant node set (via a predicate). Identity constraints may not contain functions. In practice, XPath pointers to (parts of) schema documents rarely contain functions. Therefore, this chapter does not include a tutorial on the XPath functions or the corresponding return types. There is one function, however, supported—and even promoted by the XPointer Recommendation—in an XPointer location path expression: the id function.

The XPath id function is a great way to locate a specific node, assuming that a node specifies an ID. Note that the XPointer Recommendation surmises that IDs are "most likely to survive document change." The thought is that the structure of the XML document might change: Elements might "move" relative to one another. Consequently, a location path may no longer find the desired element, whereas the ID of the node is much more likely to be stable.

NOTE

The id function is part of the XPath Recommendation, which is why the discussion about this function appears in Section 4.1. With respect to XML schemas, however, an XPointer (in a schemaLocation value) is the only expression that can access this function. The id function cannot express any part of an identity constraint.

To demonstrate the use of the id function, the following URI locates the catalogEntryDescriptionType complex type (whose ID is 'catalogEntryDescriptionType.catalog.cType') in the thematic catalog schema: http://www.XMLSchemaReference.com/theme/catalog.xsd#xpointer (id("catalogEntryDescriptionType.catalog.cType"))

To encourage the use of IDs, the XPointer Recommendation permits an XPointer to consist of a shortcut, which is just the bare name of the ID:

http://www.XMLSchemaReference.com/theme/ 
   catalog.xsd#catalogEntryDescriptionType.catalog.cType 

4.1.4 Using XPath with Identity Constraints

An identity constraint may reference only three of the axes specified by the XPath Recommendation: child, attribute, and descendant-or-self. Furthermore, an identity constraint may refer to these axes only via a shortcut. Table 4.1 lists all the shortcuts available (the three axes and a wildcard) in an identity constraint.

Table 4.2 provides a few examples that demonstrate how to locate elements in an XML document. Chapter 13, "Identity Constraints," provides a much more complete discussion.

Listing 13.3 covers the complete grammar for location paths, as pertains to identity constraints. Likewise, Table 13.2 provides a number of detailed XPath examples.

TABLE 4.1 Identity Constraint Shortcuts

Shortcut

Meaning

Example

(No axis following '/')

Implied child axis

/a/b

 

 

Selects a b that is a subelement of an a, which is a subelement of the document root.

@

attribute axis

a/@b

 

 

The b attribute of the a subelement element of the current node.

//

descendant-or-self axis

a//b

 

 

Any b element that is a descendant of a.

*

A wildcard representing all immediate subelements

a/*/b

Any b that is a subelement of any immediate subelement of a.


TABLE 4.2 Identity Constraint Examples

Example

Meaning

//bulked

This expression locates all bulkID elements along the descendant-or-self axis of the root element. Therefore, this locates all bulkID elements in the entire document.

//bulkID/description

The expression locates all description elements along the child axis of the previous example. Therefore, this locates all description elements that are immediate descendants of any bulkID element.

/catalog/*/partNumber

This expression locates all partNumber elements that are direct descendants of any (hence the '*') direct descendant of catalog. Note that each element reference is along the child axis.

/catalog//partNumber

This expression locates all partNumber elements that are descendants (but not necessarily immediate descendants) of catalog.

/catalog//@employeeAuthorization

This expression selects all employeeAuthorization attributes of any descendant of the catalog element.


  • + Share This
  • 🔖 Save To Your Account

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020