RE: [xsl] Transform XML to XML

Subject: RE: [xsl] Transform XML to XML
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Fri, 26 Aug 2005 10:10:47 -0400
At 09:43 AM 8/26/2005, you wrote:

I'm still confused by ".", "*", "node()", etc.  I've read all the
definitions, but somehow I can't grok them.  Any thoughts?

These are all XPath expressions. In fact, they're all Location Paths (a particular type of XPath expression, which happens always to return nodes in the tree, i.e. node sets).

"." is an abbreviation for the XPath expression "self::node()"

"*" is (by itself) an abbreviation for "child::*"

"node()" by itself abbreviates "child::node()"

All Location Paths are made of steps, separated by slashes, as in "secret/rule" (short for "child::secret/child::rule"), a Location Path with two steps.

Each step is made of two parts, an axis specifier (which may be implicit -- these include child::, descendant::, etc) and a node test (which must be explicit). Additionally each step may have one or more predicates, which you recognize by the [ ] syntax (e.g. "secret/rule[@status='extra-mysterious']", short for "child::secret/child::rule[attribute::status='extra-mysterious'] -- now you can see why abbreviating is nice).

"*" is a "wild card" node test, that matches any node "of the primary node type of the axis" -- so "self::*" and "child::*" (or just plain "*") will match an element, while "attribute::*" (abbreviated as "@*") will match an attribute.

"node()" is also a wild card node test, but matches any node of any type whatsoever.

Location Paths that do not begin with an initial slash "/" are evaluated relative to a context node, in a way analogous to directory traversal syntax on a Unix machine (which XPath syntax is designed to mimic in some respects).

Location paths appear in arbitrary XPath expressions, for example (commonly) in select attributes, as in apply-templates or value-of instructions.

A subset of Location Paths (not all of them, for example most axes are prohibited) is allowed in Patterns (as in the match pattern that identifies when a template can be applied).

Much of the genius of XSLT, for applications for which it's well suited, comes from using these expressions (select expressions and template match patterns) in combination so that your templates provide all conditional logic necessary for the tree to be processed in the order and arrangement you need (which may not be a temporal order, BTW) -- without any explicit "control flow" instructions (xsl:for-each, xsl:if, xsl:choose) at all.

Accordingly, you'll sometimes hear an experienced XSLTer tell a beginner "never use for-each (or if, or choose) -- you don't need it". Which sounds bizarre, but has some truth to it.

I hope this helps,

====================================================================== Wendell Piez mailto:wapiez@xxxxxxxxxxxxxxxx Mulberry Technologies, Inc. 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 ======================================================================

Current Thread