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

  • Home
  • SEARCH
  • 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 725155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:18:18+00:00 2026-05-14T06:18:18+00:00

I am trying to get a list of elements with a specific xsd type

  • 0

I am trying to get a list of elements with a specific xsd type with lxml 2.x and I can’t figure out how to traverse the xsd for specific types.

Example of schema:

<xsd:element name="ServerOwner" type="srvrs:string90" minOccurs="0">
<xsd:element name="HostName" type="srvrs:string35" minOccurs="0">

Example xml data:

<srvrs:ServerOwner>John Doe</srvrs:ServerOwner>
<srvrs:HostName>box01.example.com</srvrs:HostName>

The ideal function would look like:

    elements = getElems(xml_doc, 'string90')

    def getElems(xml_doc, xsd_type):
      ** xpath or something to find the elements and build a dict
      return elements
  • 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-14T06:18:18+00:00Added an answer on May 14, 2026 at 6:18 am

    Really the only special support lxml has for XML Schema, as seen here, is to tell you if some document is valid according to some schema or not. Anything more sophisticated you’ll have to do yourself.

    This should be a relatively simple two-phase process, I’d think — get all the xsd:element elements in the schema that match the type you care about, and look at their names:

    def getElems(schemaDoc, xmlDoc, typeName):
        names = schemaDoc.xpath("//xsd:element[@type = $n]/@name",
                                namespaces={"xsd": 
                                            "http://www.w3.org/2001/XMLSchema"},
                                n=typeName)
    

    Then, fetch all the elements with each name from the document.

        elements = []
        for name in names: 
            namedElements = xmlDoc.xpath("//*[local-name() = $name]", name=name)
            elements.extend(namedElements)
    

    Now you have a list of elements with the names that matched the type in the schema.

        return elements
    

    Note that the xpath expression for searching the document has to look at every element, so if you can tighten that up to only look in the subsection of the document you care about it’ll go faster.

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

Sidebar

Related Questions

I'm trying to get a list of elements out of an XML document where
I'm trying to figure out how to add even elements in a list (i've
I am trying to get a list of subdirectories in a given directory using
Just trying to get my irb sessions to actually list the current line of
I'm trying to get a class memeber variable list at run time. I know
Trying to get this example working from http://www.munna.shatkotha.com/blog/post/2008/10/26/Light-box-effect-with-WPF.aspx However, I can't seem to get
I am trying to remove comments from a list of elements that were obtained
I'm trying to use Watin for testing. And I need to get a list
I'm trying to get an understanding of LINQ, and starting out on the MSDN
I'm trying get values from a GridView using the following code: foreach (GridViewRow row

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.