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 6817923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:06:51+00:00 2026-05-26T21:06:51+00:00

How do I fill the default value in my XML during validation against XSD?

  • 0

How do I fill the default value in my XML during validation against XSD? If my attribute is not defined as use="require" and have default="1", it could be possible to fill these default values from the XSD to the XML.

Example:
Original XML:

<a>
 <b/>
 <b c="2"/>
</a>

XSD scheme:

<xs:element name="a">
 <xs:complexType>
  <xs:sequence>
   <xs:element name="b" maxOccurs="unbounded">
    <xs:attribute name="c" default="1"/>
   </xs:element>
  </xs:sequence>
 </xs:complexType>
</xs:element>

I want to validate the original XML using XSD and to fill all default values:

<a>
 <b c="1"/>
 <b c="2"/>
</a>

How do I get it in Python?
With validation there is no problem (e.g. XMLSchema). The problem are the default values.

  • 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-26T21:06:52+00:00Added an answer on May 26, 2026 at 9:06 pm

    To follow up on my comment, here’s some code

    from lxml import etree
    from lxml.html import parse
    
    schema_root = etree.XML('''\
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="a">
     <xs:complexType>
      <xs:sequence>
       <xs:element name="b" maxOccurs="unbounded">
        <xs:complexType>
         <xs:attribute name="c" default="1" type="xs:string"/>
        </xs:complexType>
       </xs:element>
      </xs:sequence>
     </xs:complexType>
    </xs:element>
    </xs:schema>''')
    
    xmls = '''<a>
     <b/>
     <b c="2"/>
    </a>'''
    
    schema = etree.XMLSchema(schema_root)
    parser = etree.XMLParser(schema = schema, attribute_defaults = True)
    
    root = etree.fromstring(xmls, parser)
    result = etree.tostring(root, pretty_print=True, method="xml")
    
    print result
    

    will give you

    <a>
     <b c="1"/>
     <b c="2"/>
    </a>
    

    I’ve modified your XSD slightly, wrapped xs:attribute in xs:complexType and added schema namespace. To have your defaults filled in, you need to pass attribute_defaults=True to etree.XMLParser() and it should work.

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

Sidebar

Related Questions

If I read the XML Schema specification for default attribute values correctly, the validation
I have updated certain field in my database & that field has default value
I have a form which has fields pre-filled with a default value, like this:
I have 30 buttons with nothing in default text value. What i want to
I need to fill a textbox with a value by default using the autofill
I have this selectOneMenu that asks for something, the default value is empty, because
I have created UIDatePicker and added target for event UIControlEventValueChanged . Default value of
I have to fill a combobox with month in English: January, February, etc. I
i have a CSV file : COL1;COL2;COL3;COL4 1;1;;4 6;9;;0 9;8;;4 How do i fill
I have a program that I use to enter values into a database using

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.