Web Accessibility Rules – Principles, Guidelines, Criterions, and Techniques
Aspose.HTML for Java provides the com.aspose.html.accessibility package, which is intended for all Web Accessibility related manipulations and checks. In this article, you will learn how to use the AccessibilityRules class, which is a repository of WCAG 2.0 requirements, success criteria, and techniques.
Accessibility rules are a set of guidelines, standards, or best practices that define how to design and develop digital content, such as websites, applications, and media, to ensure that they are usable by people with disabilities. These rules help make online experiences accessible to individuals with visual, auditory, motor, or cognitive disabilities, promoting digital inclusion and ensuring equal access to information, services, and technology for everyone.
The structure of the web accessibility quick reference can be represented as a diagram, which is shown below:

Accessibility Rules
The AccessibilityRules class is a repository of WCAG 2.0 requirements, success criteria, and techniques and has a structure that corresponds to the specification WCAG (Quick Reference). In order to view the list of rules, you need to initialize the WebAccessibility container and use the property Rules.
The property returns an object of type AccessibilityRules, which has such methods for accessing the rules:
| Methods | Description |
|---|---|
getPrinciple(string code) | Returns a Principle object by code from WCAG. |
| getPrinciples() | Returns a list of principles IList<Principle>. |
getRules(params string[] codes) | Returns a list of rules IList< IRule> for given codes from WCAG. |
The directory contains rules that inherit from the abstract class Rule. There are several types of rules:
- principle
- guideline
- criterion
- technique
IRule
All rules implement the interface IRule is a public interface that defines the basic properties of the rules:
| Property | Description |
|---|---|
| Code | Rule code from the quick reference WCAG. |
| Description | Description of Rule from the quick reference WCAG. |
Rule
The base abstract class for all rules, which implements the interface IRule. To get any rule, you can use the getRules() method:
1// Retrieve and list accessibility rules by code with their descriptions from the rules repository
2
3// Initialize a webAccessibility container
4WebAccessibility webAccessibility = new WebAccessibility();
5
6// List of rule codes can contain both technique codes and principles,
7// guidelines and criteria - all rules that implement the interface IRule
8String[] rulesCodes = new String[]{"H2", "H37", "H30", "1.1", "1.2.1"};
9
10// Get a list of IRule objects; if a rule with the specified code is not found,
11// it will not be in the list
12List<IRule> rules = webAccessibility.getRules().getRules(rulesCodes);
13
14// Get code and description of rule
15for (IRule rule : rules) {
16 System.out.println(String.format("%s: %s",
17 rule.getCode(),
18 rule.getDescription()
19 ));
20}The program will output to the console:
1 H2:Combining adjacent image and text links for the same resource
2 H37:Using alt attributes on img elements
3 H30:Providing link text that describes the purpose of a link for anchor elements
4 1.1:Text Alternatives
5 1.2.1:Audio-only and Video-only (Prerecorded)Principle
At the first level of the list of rules are the Principles, they indicate the main direction and purpose of the rules that are in this section. Therefore, work with the directory begins with them.
The Principle class inherited from Rule and also contains a list of guidelines.
| Method | Description |
|---|---|
getGuideline(string code) | Get Guideline by code from WCAG, contained in principle. Return Guideline object. |
| getGuidelines() | Get a list of Guideline from Principle. Return IList< Guideline> object. |
An example of how to get a Principle object from a list of rules:
1// Get accessibility principle by code from WCAG rules in Aspose.HTML for Java
2
3// Initialize a webAccessibility container
4WebAccessibility webAccessibility = new WebAccessibility();
5
6// Get the principle by code
7Principle rule = webAccessibility.getRules().getPrinciple("1");
8
9// Get code and description of principle
10System.out.println(String.format("%s: %s",
11 rule.getCode(),
12 rule.getDescription()
13));
14// @output: 1:PerceivableGuideline
The Guideline class inherited from Rule and contains a criteria list. Guidelines are the next level after principles. They outline frameworks and general goals that help authors understand success criteria and better apply the techniques.
| Method | Description |
|---|---|
getCriterion(string code) | Get Guideline by code from WCAG, contained in current Guideline. Return Criterion object. |
| getCriterions() | Get a list of Criterion from current Guideline. Return IList< Criterion> object. |
An example of how to get a Guideline object from a list of rules:
1// Get accessibility guideline by code from WCAG principle in Aspose.HTML for Java
2
3// Initialize a webAccessibility container
4WebAccessibility webAccessibility = new WebAccessibility();
5
6// Get the principle by code
7Principle principle = webAccessibility.getRules().getPrinciple("1");
8
9// Get guideline 1.1
10Guideline guideline = principle.getGuideline("1.1");
11if (guideline != null) {
12 System.out.println(String.format("%s: %s, %s",
13 guideline.getCode(),
14 guideline.getDescription(),
15 guideline
16 ));
17 // @output: 1.1:Text Alternatives
18}Criterion
The Criterion class describes the WCAG success criteria, inherited from abstract class Rule. Detailed information can be found here – Understanding Techniques for WCAG Success Criteria. The basis for determining conformance to WCAG 2.0 is the success criteria from the standard. The criterion contains a list of techniques for meeting WCAG web content accessibility guidelines. If all sufficient methods for a given criterion are supported by accessibility, then the success criterion has passed.
Use the getCriterion(code) method to access criteria from guideline:
1// Get accessibility criterion and its sufficient techniques in Java
2
3// Initialize a webAccessibility container
4WebAccessibility webAccessibility = new WebAccessibility();
5
6// Get the principle by code
7Principle principle = webAccessibility.getRules().getPrinciple("1");
8
9// Get guideline
10Guideline guideline = principle.getGuideline("1.1");
11
12// Get criterion by code
13Criterion criterion = guideline.getCriterion("1.1.1");
14if (criterion != null) {
15 System.out.println(String.format("%s: %s - %s",
16 criterion.getCode(),
17 criterion.getDescription(),
18 criterion.getLevel()
19 ));
20 // @output: 1.1.1:Non-text Content - A
21
22 // Get all Sufficient Techniques and write to console
23 for (IRule technique : criterion.getSufficientTechniques())
24 System.out.println(String.format("%s: %s",
25 technique.getCode(),
26 technique.getDescription()
27 ));
28}Check HTML Against Specific Web Accessibility Rules
This code demonstrates how to validate an HTML document for web accessibility using the specified set of rules.
1// Validate an HTML document using specific rule codes with Java
2
3String htmlPath = "input.html";
4
5// Initialize a webAccessibility container
6WebAccessibility webAccessibility = new WebAccessibility();
7
8// List of necessary rules for checking (rule code according to the specification)
9String[] rulesCode = new String[]{"H2", "H37", "H67", "H86"};
10
11// Get the required IList<Rule> rules from the rules reference
12List<IRule> rules = webAccessibility.getRules().getRules(rulesCode);
13
14// Create an accessibility validator, pass the found rules as parameters,
15// and specify the full validation settings
16AccessibilityValidator validator = webAccessibility.createValidator(
17 rules, ValidationBuilder.getAll());
18
19// Initialize an object of the HTMLDocument
20final HTMLDocument document = new HTMLDocument(htmlPath);
21// Check the document
22ValidationResult validationResult = validator.validate(document);
23
24// Return the result in string format
25// SaveToString - return only errors and warnings
26System.out.println(validationResult.saveToString());- Use the
WebAccessibility() constructor to create the
webAccessibilitycontainer, which manages the accessibility rules and validation. - Define a list of rule codes (e.g., “H2”, “H37”, “H67”, “H86”) that correspond to the accessibility standards you want to validate against.
- Call the getRules() method, passing the list of rule codes as a parameter.
- Create a
validatorby calling the createValidator() method on thewebAccessibilitycontainer and passing it the resulting rules and theValidationBuilder.getAll(). - Load an HTML document you want to validate using one of the HTMLDocument() constructors.
- Use the
validate(
document) method of thevalidatorto check the loadedHTMLDocumentagainst the defined rules. The result of the validation is theValidationResultobject. - Call the
saveToString() method on the
ValidationResultobject to format the results, including only the errors and warnings, as a string. - Output the validation results to the console or save them as needed for further analysis or reporting.
See Also
- You will find helpful tips on evaluating and improving text visibility in the article Color Contrast Accessibility, which covers contrast checking based on WCAG using Aspose.HTML for Java.
- For instructions on checking web content is compatible with screen readers, you will find in the article Screen Reader Accessibility. You will learn how to check alt text and other key elements.
- If you want to learn how to view validation results and identify web accessibility issues, see the Validation Results article.
- In the article
Web Accessibility Check – Errors and Warnings, you will learn how to programmatically in Java collect error and warning information while checking a website’s accessibility.
Aspose.HTML offers free online Web Accessibility Checker. This tool scans web pages, validates them for WCAG compliance, identifies problems, and suggests improvements. Get instant insights into your website’s compliance, allowing you to determine the scope of necessary corrections and the gap between the current state of your website or HTML document and WCAG requirements.
