Re: [xsl] big hairy Xpath (Solved!)

Subject: Re: [xsl] big hairy Xpath (Solved!)
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Mon, 20 Apr 2009 11:56:54 -0400
Russ,

You will understand this better if you revise your understanding of what a predicate (bracketed expression) does. Another name for a predicate (and arguably a more proper way in XPath 2.0, in which its operation is generalized to all sequences of items, not just sets of nodes) is a "filter expression".

So:

1. child::entry - selects all 'entry' element children of the context node

2. child::entry[true()] - selects all 'entry' children, and filters them with
the expression 'true()'. Since 'true()' is always true, the sequence of nodes
returned will be the same as 1.


3. child::entry[false()] - by the inverse logic, returns an empty sequence.
   'false()' is never true, so all 'entry' children are filtered out. (Only
   elements in the set for which the expression is true are kept.)

4. entry[@n='1'] - entry children that have an @n attribute with the string
   value "1". There may be any number of these, or none at all. Note that
   each 'entry' element is used separately as the evaluation context for the
   expression "@n='1'", which is how some of them pass and others fail.

5. Description[.='Special'] - 'Description' element children whose string value
   = "Special". Again, how many you get depends on your data and your context
   node (where you started the path).

6. favorite[@owner='zyx'][@rating > 3] - 'favorite' element children that have an
@owner attribute = "zyx", and also a @rating with numeric value greater than 3.


7. favorite[@owner='zyx' and @rating > 3] - exactly the same thing, except
expressed somewhat differently: instead of two predicates (the second of which
filters the results of the first filter), only one predicate is used, with the
conditions paired together using a Boolean 'and' operator.


8. child::child[child::grandchild] - 'child' element children that have
   'grandchild' element children. This works because the expression
   'child::grandchild' will cast to Boolean true() for any 'child' element
   children that have 'grandchild' children.

9. child[grandchild] - the same as 8., except in short (abbreviated) syntax.

You can read more in a good reference to XPath.

I hope this helps,
Wendell


At 07:14 PM 4/17/2009, you wrote:



Hi All,

I ended using a variation of Wendells sugggest of:

1) //GenTable[count(.//entry

2) [Emphasis[@type='bold']

3) [(.='Address Offset') or

4) (.='Physical Address') or

5) (.='Instance') or

6) (.='Description') or

7) (.='Type') ] )

8) >= 4]


changing the entry to .//entry as entry IS a descendant of GenTable and not a direct child. To see if i got my head around this now. WRT to the lines numbered above:

1) Go to a GenTable child node. Within that element (that's what the '['
at this point means, right?) go to a descendant entry element.
2) Go to a child Emphasis element, looking for the type attribute set to
'bold'.
3) (Does the opening '[' at this point signal an AND being applied wrt
to...) Within this same element see if it has the value of 'Address
Offset'.
4) - 7) Of if the current element has any of these values, closing the
checking with the ']'
8) if the number of these is >= 4 then return this  GenTable.

Is that about right. The multiple predicates in this situation act like
and AND?


======================================================================
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
======================================================================

Current Thread