[Xml-compile] adding xml:lang attribute

Mark Overmeer solutions at overmeer.net
Mon Mar 2 08:53:11 GMT 2009


* Petar Shangov (pshangov at yahoo.com) [090228 16:19]:
> I still cannot get this to work properly. The value of $lang seems
> to get lost and is always undef during the 'after' hook. Admittedly,
> I am not very good with closures and such. Here is what I have tried:

To start with: I see that your schema cleanly refes to the xml:lang
as attribute, so I see no reason for this hook at all.  If you add
a 'lang' key to the hash, it should be used.  Otherwise, it is a
bug.  Send me code (doesn't need to be small) which I can use to
reproduce your problem.

But to respond to your search for a solution as well:

> # version 1
> { 
>     my $lang;
> 
>     $schema->addHook(
>         path   => qr(tuv),
>         before => \&before_sub,
>         after  => \&after_sub,
>     );    
> }

Your hook is based on the component 'tuv' in the path.  This means that
the hook is also selected for sub-elements of the one you would like
to process.  These sub-elements do not contain the 'lang' attribute,
so when processing those, the $lang is cleared.  Back on the level
where you want it to be added, the value has disappeard.

> In both cases I get the xml:lang attribute where needed but its
> value is empty. I ended up using the following:
>
> $schema->addHook(
>   path   => qr'tuv$',
                    ^^^ this is doing the trick.

> Second question: this is probably something I have not done
> properly, but I can't get the 'type' selector for adding hoods to work
> properly. Here is some of the options I tried (instead of 'path'):
> 
> type   => 'tuv'

No.  Your element is named "tuv", and the type of that element is
an unnamed complexType.  So, you can not use the type selector.

The idea is that you will need the hook on the type, everywhere where
the same type is used.

> type   => '{tmx}tuv' # 'tmx' is defined as a prefix

When you use XML::Compile::Cache i.s.o. XML::Compile::Schema, you can
use 'tmx:tuv'  to address elements or types for predefined namespace
prefix tmx.

> ...
> and none triggers the hook (using the 'path' selector as specified
> above works OK).

As expected.
I could add a selector "name" or "element" and "attribute".  However,
what would require a very different place in the code to also support
hooks: on the moment, it is the type handler, not the element constructor.
-- 
               MarkOv

------------------------------------------------------------------------
       Mark Overmeer MSc                                MARKOV Solutions
       Mark at Overmeer.net                          solutions at overmeer.net
http://Mark.Overmeer.net                   http://solutions.overmeer.net




More information about the Xml-compile mailing list