[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

XPath Best Practice: Getting the processor to detect

Subject: XPath Best Practice: Getting the processor to detect misspelled tag names in XPath expressions [Was: Is an XPath processor responsible for catching misspelled tag names when there is an associated Schema?]
From: "Costello, Roger L." <costello@xxxxxxxxx>
Date: Fri, 22 Feb 2008 07:54:48 -0500
 XPath Best Practice: Getting the processor to detect
Hi Folks,

Below is a summary of our discussions.  Comments welcome.  /Roger

Consider this XML document:

<?xml version="1.0"?>
<Book>
    <Title>My Life and Times</Title>
    <Author>Paul McCartney</Author>
    <Date>1998</Date>
    <ISBN>1-56592-235-2</ISBN>
    <Publisher>McMillan Publishing</Publisher> </Book>

Here is an XPath expression to count the number of <Author> elements:

    count(/Book/Authr)

Notice that Author has been accidentally misspelled in the XPath
expression.

The XML document conforms to this XML Schema:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           elementFormDefault="qualified">
    <xs:element name="Book">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="Title" />
                <xs:element ref="Author"  minOccurs="0"
maxOccurs="unbounded" />
                <xs:element ref="Date" />
                <xs:element ref="ISBN" />
                <xs:element ref="Publisher" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="Title" type="xs:string"/>
    <xs:element name="Author" type="xs:string"/>
    <xs:element name="Date" type="xs:string"/>
    <xs:element name="ISBN" type="xs:string"/>
    <xs:element name="Publisher" type="xs:string"/> </xs:schema>

Note that it is particularly important to design the XPath in such a
way that the processor catches the misspelled tag name, since the XML
Schema declares the number of occurrences of the <Author> element to be
0-to-unbounded.  The XPath count function may return a result of 0,
which is a legitimate value and so the misspelling error may go
undetected for a long time.

It should be possible for the XPath processor to detect, by consulting
the XML Schema, that Authr is not a legal child of Book and generate an
error or warning.

And it is possible.  However, it cannot be accomplished entirely within
XPath; features from the host language must be utilized.

For example, if the host language is XSLT then first create a variable
for the <Book> element and use the XSLT variable declaration capability
to specify its type, using the "as" attribute:

    <xsl:variable name="bk" select="/Book" as="schema-element(Book)" />

Then use the variable in the XPath expression:

    count($bk/Authr)

Now the processor will generate an error or warning message.  SAXON
generates this warning: "The complex type of element Book does not
allow a child element named Authr"

Using features from the host language is not an ideal situation.  The
consequence of using host-language-specific features is that the XPath
is not portable: for each host language the XPath must be redesigned
using capabilities from the host language.

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.