Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xpath expression to select the last of inner tag

Tags:

xpath

openerp

For this XML excerpt, I would like to specify "the last inner group", in this case "Reporting" but there might be more tags in that inner area, I want the last one inside "Other Information"

So how can I say "the last inner group of page string='Other Information' "?

 <page string="Other Information">
    <group>
        <group string="Sales Information" name="sales_person">
            <field name="user_id"/>
            <field name="team_id" options="{'no_create': True}"/>
            <field name="client_order_ref"/>
            <field name="company_id" options="{'no_create': True}" groups="base.group_multi_company"/>
            <field name="project_id" attrs="{'invisible':[('state','=','sale')]}" context="{'default_partner_id':partner_invoice_id, 'default_name':name}" groups="analytic.group_analytic_accounting"/>
            <field name="related_project_id" attrs="{'readonly': ['|',('project_id','!=',False),('invoice_count','!=',0),('state','=','sale')],'invisible':[('state','!=','sale')]}" context="{'default_partner_id':partner_invoice_id, 'default_name':name}" groups="analytic.group_analytic_accounting"/>
        </group>
        <group name="sale_pay" string="Invoicing">
            <field name="fiscal_position_id" options="{'no_create': True}"/>
            <field name="invoice_status" attrs="{'invisible': [('state', 'not in', ('sale','done'))]}"/>
        </group>
        <!-- ***** THIS ONE ****** -->
        <group string="Reporting" name="technical" groups="base.group_no_one">
            <field groups="base.group_no_one" name="origin"/>
        </group>
        <!-- ***** THIS ONE ****** -->
    </group>
</page>
like image 604
M.E. Avatar asked Jan 04 '23 07:01

M.E.


1 Answers

xpath to select last item is:

(somepath)[last()]

So @eLRuLL's answer is correct in the general case, but it's always better to keep some structure in xpath, and if you know your xml structure - say explicitly on what level you need to get tags, so in case when formating brakes - you'll know about that:

(//page[@string="Other Information"]/group/group)[last()]

or at least select only groups with names, not to get group that is a wrapper:

(//page[@string="Other Information"]//group[@name])[last()]
like image 112
Vitaliy Moskalyuk Avatar answered Feb 05 '23 07:02

Vitaliy Moskalyuk