Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6601687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:47:10+00:00 2026-05-25T18:47:10+00:00

I wonder if it’s possible to make conditional statments connected with a tree.findall(…) statement

  • 0

I wonder if it’s possible to make conditional statments connected with a tree.findall(“…”) statement in lxml library?

I have the following xml structure in a file

    <sss version="1.2">
    <date>2011-09-23</date>
    <time>12:32:29</time>
    <origin>OPST</origin>
    <user></user>
    <survey>
        <name>Test</name>
        <version>2011-09-02 15:50:10</version>
        <record ident="A">
            <variable ident="10" type="quantity">
                <name>no_v</name>
                <label>Another question</label>
                <position start="23" finish="24"/>
                <values>
                    <range from="0" to="32"/>
                </values>
            </variable>
            <variable ident="11" type="quantity">
                <name>v_683</name>
                <label>another totally another Question</label>
                <position start="25" finish="26"/>
                <values>
                    <range from="0" to="33"/>
                </values>
            </variable>
            <variable ident="12" type="quantity">
                <name>v_684</name>
                <label>And once more Question</label>
                <position start="27" finish="29"/>
                <values>
                    <range from="0" to="122"/>
                </values>
            </variable>
            <variable ident="20" type="single">
                <name>v_684</name>
                <label>Question with alternatives</label>
                <position start="73" finish="73"/>
                <values>
                    <range from="1" to="6"/>
                    <value code="1">Alternative 1</value>
                    <value code="2">Alternative 2</value>
                    <value code="3">Alternative 3</value>
                    <value code="6">Alternative 4</value>
                </values>
            </variable>
        </record>
    </survey>
</sss>

What I want to do now is to get only survey/record/variable/name .text and survey/record/variable/values/value .text if the name starts with “v_”

So far i have the first part

from lxml import etree as ET
tree = ET.parse('scheme.xml')
[elem.text for elem in tree.getiterator(tag='name') if elem.text.startswith('v_')]

But how can I get the survey/record/variable/values/value .text of the SAME element…and use survey/record/variable/name .text like a filter?
Thanks a lot!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:47:10+00:00Added an answer on May 25, 2026 at 6:47 pm
    [(elem.text,elem.getparent().xpath('values/value/text()')) 
     for elem in tree.getiterator(tag='name') if elem.text.startswith('v_')]
    

    yields

    [('v_683', []),
     ('v_684', []),
     ('v_684',
      ['Alternative 1', 'Alternative 2', 'Alternative 3', 'Alternative 4'])]
    

    elem is a name element. So to get the the associated values, you can first find its parent (variable), then search for the values child, and then the value subchild elements.


    An alternative that removes the getparent call, but uses a slightly more complicated XPath is:

    [(elem.text,elem.xpath('following-sibling::values/value/text()')) for elem in tree.getiterator(tag='name')  if elem.text.startswith('v_')]
    

    following-sibling:: tells xpath to generate all siblings of name.

    following-sibling::values tells xpath to generate all siblings of name that are values elements.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

wonder whether someone can help me with the following one... I have a struct
Wonder how I could do the following wit jquery - I have a url
I wonder how it would be possible to implement a positional inverted index in
I wonder whether it is possible to change the display server IP of a
I wonder if it is possible to supress errors in PHP like when redeclaring
I wonder about shared preferences security. Is it possible to get access to sharedpreferences,
Wonder if someone could give me a quick hand. I have 2 select queries
Wonder if this possible. Saw many posts on adding watermark after the pdf is
wonder if anyone can help; I have an a two dimensional array which I
I wonder if it is possible to scale text in the x-axis?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.