RE: [xsl] 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?]

Subject: RE: [xsl] 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 12:46:04 -0500
Ah, well stated.  Now I understand.  Thanks Mukul!

/Roger

-----Original Message-----
From: Mukul Gandhi [mailto:gandhi.mukul@xxxxxxxxx]
Sent: Friday, February 22, 2008 12:03 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] 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?]

If the XML Schema defines the following structure, /Book/Author and
not, /Book/Authr , then supplying the XPath expression /Book/Authr to
the XPath processor is not an error as per XPath. It just returns an
empty sequence.

As you rightly said, while using XPath expressions in some host
environment, for e.g., XSLT, we can leverage the Schema aware features
of XSLT (2.0) to detect wrong XPath expressions ...

On Fri, Feb 22, 2008 at 6:24 PM, Costello, Roger L.
<costello@xxxxxxxxx> wrote:
> 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.



--
Regards,
Mukul Gandhi

Current Thread