code-gijoe
code-gijoe

Reputation: 7234

Understanding some xpath expression

Could someone help me decrypt this xpath expression?

<xsl:template match="n1:table/@* | 
                     n1:thead/@* | 
                     n1:tfoot/@* | 
                     n1:tbody/@* | 
                     n1:colgroup/@* | 
                     n1:col/@* | 
                     n1:tr/@* | 
                     n1:th/@* | 
                     n1:td/@*">

I believe it's somewhere around:

Select all attributes from n1:table element AND all attributes from n1:thead element AND all attributes from n1:tfoot ... etc.

I'm really not sure though.

Was reading this to get understand the xpath: http://www.w3schools.com/xpath/xpath_syntax.asp

Care to give me a hint?

Thx.

Upvotes: 2

Views: 479

Answers (2)

Michael Kay
Michael Kay

Reputation: 163262

It's not actually an XPath expression - it's an XSLT pattern. The syntax of patterns is a subset of the syntax of expressions, so they are closely related, but the semantics works rather differently. In particular, whereas an XPath expression selects nodes, a pattern matches them (or doesn't, as the case may be).

Anyway, a node matches the pattern P|Q if it matches either P or Q or both, and a node matches X/@* if it is an attribute of an element named X, and that basically explains this pattern.

Upvotes: 0

Wayne
Wayne

Reputation: 60414

You've basically got it right. The | is the XPath' union set operator:

From http://www.w3.org/TR/xpath/#node-sets

The | operator computes the union of its operands, which must be node-sets

But in patterns, from http://www.w3.org/TR/xslt#patterns

In a pattern, | indicates alternatives; a pattern with one or more | separated alternatives matches if any one of the alternative matches.

So the template matches for any attributes of table, thead, tbody, etc in the namespace referenced by n1 relative to the current context node.

You'll also need to account for the presence of a namespace in your source document using something like the following:

<xsl:stylesheet 
     version="1.0" 
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
     xmlns:n1="uri:something">

More on namespaces in XSLT templates:

http://radio-weblogs.com/0118231/stories/2006/10/03/xslt10PatternMatchingTipsForSourceDocumentsWithNamespaces.html

Upvotes: 2

Related Questions