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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:40:33+00:00 2026-05-16T15:40:33+00:00

I am developing a schema using RELAX NG. I’m pretty new to this, so

  • 0

I am developing a schema using RELAX NG. I’m pretty new to this, so it is quite possible that I am overlooking something obvious, but there doesn’t seem to be a convenient way to specify the number of occurrences of an element like you can in the XML Schema language.

For example, suppose I wanted to specify that an A element may contain 2 – 5 B elements. I don’t want to use the zeroOrMore or oneOrMore tags because I actually do have an upper bound on the number of elements. In XML Schema, I can use the minOccurs and maxOccurs properties to specify that compactly. I’ve read through the official RELAX NG tutorial, skimmed the spec, and done some basic googling, but I haven’t been able to find any convenient way to do this with RELAX NG. The best I can figure out, you would need to do something like this:

<element name="A">
  <ref name="B"/>
  <ref name="B"/>
  <optional><ref name="B"/></optional>
  <optional><ref name="B"/></optional>
  <optional><ref name="B"/></optional>
</element>

<define name="B">
  <element name="B">
    <text/>
  </element>
</define>

This is doable, but will start looking ugly when you need a larger number of occurrences. In my actual schema, I have one element type which could possibly occur up to 256 times, so the manually specified optional elements will be clunky. I’ll do it if I need to, but I’d like to know if there is a more elegant way of expressing my occurrence restrictions.

  • 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-16T15:40:34+00:00Added an answer on May 16, 2026 at 3:40 pm

    There really isn’t a terribly non-clunky way of doing this in RELAX NG I’m afraid.

    I’d take one of two approaches. Either build up patterns or use Schematron annotations and process in an environment where you can do process using both languages:

    For example, building up groups of B elements:

    <define name="B2">
        <ref name="B"/>
        <optional><ref name="B"/></optional>
    </define>
    
    <define name="B4">
        <ref name="B2"/>
        <ref name="B2"/>
    </define>
    

    letting you slowly build up groups of (in this case), one or two elements , then 2-4 elements and so on. Combining different groups will let you specify the appropriate count (eventually).

    Alternatively you could use Schematron annotations:

    <?xml version="1.0" encoding="utf-8"?> 
    <grammar xmlns="http://relaxng.org/ns/structure/1.0"
        xmlns:s="http://www.ascc.net/xml/schematron"> 
    
        <element name="A">
          <s:rule context="A">
            <!-- note - using XPath 2 here -->
            <s:assert test='count(B) lt 6'>A elements may contain no more than 6 B elements</s:assert>
          </s:rule>
          <ref name="B"/>
          <oneOrMore><ref name="B"/></oneOrMore>
        </element>
    
        <define name="B">
          <element name="B">
            <text/>
          </element>
        </define>
    
    </grammar>
    

    I tend to prefer the latter as it makes for much simpler schemas and it’s not hard to intermix the two but it does depend on your validation environment.

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

Sidebar

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.