Subject: [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 07:54:48 -0500 |
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 |
---|
|
<- Previous | Index | Next -> |
---|---|---|
RE: [xsl] Re: Is an XPath processor, Michael Kay | Thread | Re: [xsl] XPath Best Practice: Gett, Florent Georges |
[xsl] RE : RE: [xsl] Re: Is an XPat, Florent Georges | Date | [xsl] comparing sequences that cont, Andrew Welch |
Month |