Validate XBRL and iXBRL files in C#
Validate XBRL instance file in C#
XBRL Instances, XBRL Linkbases, and XBRL Taxonomy Schemas MUST comply with the syntax requirements imposed in XBRL specification. To validate these, the XbrlInstance class provides the Validate() method.
The following C# code snippet demonstrates how to validate an XBRL instance document.
XbrlDocument document = new XbrlDocument(XbrlFilePath + @"IdScopeContextPeriodStartAfterEnd.xml"); | |
XbrlInstanceCollection xbrlInstances = document.XbrlInstances; | |
XbrlInstance xbrlInstance = xbrlInstances[0]; | |
xbrlInstance.Validate(); | |
if(xbrlInstance.ValidationErrors.Count > 0) | |
{ | |
List<ValidationError> validationErrors = xbrlInstance.ValidationErrors; | |
} |
Validate iXBRL file in C#
The iXBRL specification defines many validation rules. For validating iXBRL files, the InlineXbrlDocument class provides a Validate() method.
The following C# code snippet demonstrates validating an iXBRL instance document.
InlineXbrlDocument document = new InlineXbrlDocument(XbrlFilePath + @"account_1.html"); | |
document.Validate(); | |
if (document.ValidationErrors.Count > 0) | |
{ | |
List<ValidationError> validationErrors = document.ValidationErrors; | |
} |
Validation error codes
In the enum ValidationErrorCode, validation error codes are defined for each validation rule. The following are the error code definitions:
- ContextPeriodNoStartTime: Context period type is duration, but has no start date.
- ContextPeriodNoEndTime: Context period type is duration, but has no end date.
- ContextPeriodStartAfterEnd: Context period type is duration, but the end date is before the start date.
- ContextInstantNoTime: Context period type is instant, but has no instant date.
- ContextScenarioXbrlNamespace: Context scenario can not have XBRL namespace node.
- ContextScenarioXbrlSubstitutionGroup: Context scenario can not have an element in the substitution group for elements defined in the XBRL namespace.
- ContextScenarioEmpty: Context scenario can not be empty.
- ContextSegmentXbrlNamespace: Context segment can not have XBRL namespace node.
- ContextSegmentXbrlSubstitutionGroup: Context segment can not have element in the substitution group for elements defined in the XBRL namespace.
- ContextSegmentEmpty: Context segment can not be empty.
- ItemNoContext: Item must have a context.
- ItemPeroidTypeConflictWithContext: Item has period type conflict with context.
- ItemNumericNoUnit: Item is numeric and must have a unit.
- MonetaryItemNoSingleUnitMeasure: Item is a monetary type and must have a single unit measure.
- MonetaryItemNoISO4217: Item is a monetary type and must have an Iso 4217 style unit measure.
- ShareItemNoSingleUnitMeasure: Item is a share type and must have a single unit measure.
- ShareItemNoShareUnitMeasure: Item is share type and must have an xbrli:shares unit measure.
- NillItemWithPrecisionOrDecimals: Item is nil and must not have either precision or decimals.
- FractionItemWithPrecisionOrDecimals: Item is a fraction type and must not have either precision or decimals.
- NumericItemWithBothPrecisionAndDecimals: Item is a numeric type and must not have both precision and decimals.
- NumericItemWithoutPrecisionOrDecimals: Item is a numeric type and must have either precision or decimals.
- NonNumericItemWithPrecisionOrDecimals: Item is not a numeric type and must not have either precision or decimals.
- FootnoteArcFromNotFound: Unable to find Footnote arc from Loc.
- FootnoteArcToNotFound: Unable to find Footnote arc to Footnote.
- DefinitionArcFromNotFound: Unable to find Definition arc from Loc.
- DefinitionArcToNotFound: Unable to find Definition arc to Loc.
- EssenceAliasDefinitionArcDifferentType: Essence-alias Definition arc has different types.
- EssenceAliasDefinitionArcDifferentPeriodType: Essence-alias Definition arc has different periodTypes.
- EssenceAliasDefinitionArcDifferentBalance: Essence-alias Definition arc has different balances.
- CalculationArcFromNotFound: Unable to find Calculation arc from Loc.
- CalculationArcToNotFound: Unable to find Calculation arc to Loc.
- LabelArcFromNotFound: Unable to find Lable arc from Loc.
- LabelArcToNotFound: Unable to find Lable arc to Loc.
- PresentationArcFromNotFound: Unable to find a Presentation arc from Loc.
- PresentationArcToNotFound: Unable to find a Presentation arc to Loc.
- ReferenceArcFromNotFound: Unable to find a Reference arc from Loc.
- ReferenceArcToNotFound: Unable to find a Reference arc to Loc.
Example of standard validation error message
Above is an XBRL instance, it defines context “cd1”, this context period type is duration, it’s start date is 2002-03-31, the end date is 2001-03-31, so the end date is before than start date. In XBRL specification, chapter 4.7.2, it defines validation rule: “the endDate MUST specify or imply a point in time that is later than the specified or implied point in time of the corresponding startDate”. According to this rule, this XBRL instance is not a valid one.
Validate XBRL and output standard error message
The following code validates the XBRL instance and outputs the standard error message.
XbrlDocument document = new XbrlDocument(XbrlFilePath + @"IdScopeContextPeriodStartAfterEnd.xml"); | |
XbrlInstanceCollection xbrlInstances = document.XbrlInstances; | |
XbrlInstance xbrlInstance = xbrlInstances[0]; | |
xbrlInstance.Validate(); | |
if (xbrlInstance.ValidationErrors.Count > 0) | |
{ | |
foreach(ValidationError validationError in xbrlInstance.ValidationErrors) | |
{ | |
Console.WriteLine("Find validation error:" + validationError.Message); | |
} | |
} |
The following image shows the output:
Validate XBRL and output customized error message
The following code validates the XBRL instance and output customized error message.
XbrlDocument document = new XbrlDocument(XbrlFilePath + @"IdScopeContextPeriodStartAfterEnd.xml"); | |
XbrlInstanceCollection xbrlInstances = document.XbrlInstances; | |
XbrlInstance xbrlInstance = xbrlInstances[0]; | |
xbrlInstance.Validate(); | |
if (xbrlInstance.ValidationErrors.Count > 0) | |
{ | |
foreach (ValidationError validationError in xbrlInstance.ValidationErrors) | |
{ | |
if(validationError.Code == ValidationErrorCode.ContextPeriodStartAfterEnd) | |
{ | |
ContextValidationError contextValidationError = validationError as ContextValidationError; | |
Console.WriteLine("Validation error: end date is before start date in context " + contextValidationError.Object.Id); | |
} | |
else | |
{ | |
Console.WriteLine("Find validation error:" + validationError.Message); | |
} | |
} | |
} |
The following image shows the output:
Validate XBRL and output standard error message