[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: Trouble with namespaces and running identity trans
James,
On the test documents you provided yesterday, this will also work: <xsl:template match="h:h1|h:h2|h1|h2"> <p> LOOK MA! <xsl:apply-templates select="@*|node()"/> NO BRAINS! </p> </xsl:template> (Quibble: you probably don't want to select "@*" here, unless you know what you're doing.) Let's consider briefly why "h:h1 | h1" works as a match, and *[local-name()='h1']" works as a match, but neither "h:h1" nor "h1" work to match your four cases. First off: the presence or absence of a DOCTYPE declaration will have no impact on how elements are matched. (When a DOCTYPE declaration is present and the parser validates the document before passing it to the XSLT engine, it can affect attribute values, for which a DTD can provide defaults; but it won't affect element matching.) So your four cases collapse into two. The two cases hinge on whether a namespace declaration is present in your source: <html xmlns="http://www.w3.org/1999/xhtml"> This basically says "the null [unprefixed] namespace is bound to the URI "http://www.w3.org/1999/xhtml" (or URL, or whatever parlance you prefer for that thingie). Accordingly, the complete name of any unprefixed name in this namespace's scope includes this URI. So an h1, for example, is "really" named "{http://www.w3.org/1999/xhtml}h1" (using a more-or-less random notation). In order to match a namespaced element in a stylesheet, you must use a prefixed (qualified) name. This is why match="h:h1" works for you when you have a namespace declaration in the source file, and the "h:" prefix is bound in the stylesheet to the same namespace. When there is no namespace declaration in the source file, however, the name is not qualified, so you need match="h1" in your stylesheet. match="h:h1 | h1" catches both cases (again assuming the "h:" prefix is bound properly) in the same match pattern. match="*[local-name()='h1'] also matches both elements, since the local name (the part of the name not including the namespace qualification) is the same, "h1". I hope this helps. Namespaces can be tricky in XSLT. Another way of dealing with this whole problem is to preprocess your input with a namespace normalizer -- an identity transform that outputs the input document with only its namespaces tweaked. Then all your inputs are the same, and your stylesheet needs to worry only about one or a known set of namespaces. Not that this is always a good idea (it rather goes against the whole point of namespaces). I leave such a transform as an exercise for the reader. ;-) Cheers, Wendell When there is no such At 04:49 PM 3/9/2004, you wrote: I've been working on a stylesheet to "massage" some XHTML. One variation of a test stylesheet is here: ====================================================================== Wendell Piez mailto:wapiez@xxxxxxxxxxxxxxxx Mulberry Technologies, Inc. http://www.mulberrytech.com 17 West Jefferson Street Direct Phone: 301/315-9635 Suite 207 Phone: 301/315-9631 Rockville, MD 20850 Fax: 301/315-8285 ---------------------------------------------------------------------- Mulberry Technologies: A Consultancy Specializing in SGML and XML ====================================================================== XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|