Idea Delivered

This is being delivered in XPP 9.4

xyPerl: need a better way to check the current context

In xyPerl you can easily check for the presence or get the value of an attribute from a specific ancestor using the rather elegant syntax provided (@name, ../@name, ../../@name, ancestor::element/@name, etc...)

But checking if you are in a certain context is quite cumbersome. You need to do a get_mlh or get_path and then write your own function to detect if you are currently in a certain context.
This could be done a lot easier if we could use similar syntax like for the attributes inside a new xyPerl function (get_context?)

Things could look like this:
if ($X->get_context('ancestor::chapter') ) {
} else {

Remark: one might argue that the context checking might be done inside the CSS, but sometimes you just want to avoid writing 2 similar xyPerl functions and just to the context checking inside your xyPerl function.

  • Oh and thinking about the name of the function (check_context), why not go all the way and throw in the possibility to check attributes and their values as well:

  • (ai hit the return key too quickly)

    ../../ ... ../foo
    (especially the grandparent test might be useful)

  • $X->check_context() is indeed better than $X->get_context.
    And the the proposed Xpath axis statements are fine. 
    But I would like to see the '.' possibilities as with the $X->get_attr() function:

  • This seems like a good idea to me. Granted, in a "pure" world, everything is CSS, but as we know sometimes you just need to do a little perl. Having the ability to test the current document hierarchy can simplify some of the perl and allow for sharing.

    So, I'm thinking, we can support some of the Xpath axis statements; as  $X->check_context('Xpath axis") where 'axis' is the keyword and tag name to test as:

       ancestor::foo - any ancestor node named 'foo'
       ancestor-or-self::foo - any ancestor or current node named 'foo'
       parent::foo - Current node's parent name is 'foo'
       self::foo - Current node's name is 'tag'

    A non-empty string return is TRUE, "" is FALSE

    What do you think?

  • Sounds like a good idea Bart.

    A XyPerl function that tests the current hierarchical position against a test value, which the user could present in a similar format as already implemented for the get_attr function, would be a welcome addition to the good array of functions already available to the XPP styler and help to streamline their XyPerl code.