Skip to content

Working with XBRL Contexts

Jeff Ferguson edited this page Oct 9, 2017 · 1 revision

In XBRL, a context specifies a reporting period and other information regarding the scenario under which reporting is taking place.

Contexts

Within Gepsio, each XbrlFragment object contains a property called Contexts, which is a list of contexts specified in the fragment. Each context in the list is represented by an object of class JeffFerguson.Gepsio.Context. A context has an ID and a period type which specifies whether the context is used to report a point in time or a date range.

The period type is specified in a Context object through one of three Boolean properties available on the object. If the value of a Context object's InstantPeriod property is true, then the context specifies an instant period. If the value of a Context object's DurationPeriod property is true, then the context specifies a duration period. If the value of a Context object's ForeverPeriod property is true, then the context specifies a period without a start or an end.

Contexts With Instant Period

If the value of a Context object's InstantPeriod property is true, then the context specifies an instant period. The date and time for the instant is specified in the Context object's InstantDate property:

if(currentContext.InstantPeriod)
{
    Console.WriteLine($"\tInstant Date: {currentContext.InstantDate}");
}

Contexts With Duration Period

If the value of a Context object's DurationPeriod property is true, then the context specifies a duration period. The start date and time for the period is specified in the Context object's PeriodStartDate property, while the end date and time for the period is specified in the Context object's PeriodEndDate property:

if(currentContext.DurationPeriod)
{
    Console.WriteLine($"\tPeriod Date : from {currentContext.PeriodStartDate} to {currentContext.PeriodEndDate}");
}

Finding Contexts For A Fact

Each fact has two properties that specifies the context that is associated with the fact: ContextRefName, which specifies the name of the context associated with the fact, and ContextRef, which directly references the Context object associated with the fact.

using JeffFerguson.Gepsio;
using System;

namespace Contexts
{
    class Program
    {
        static void Main(string[] args)
        {
            var xbrlDoc1 = new XbrlDocument();
            xbrlDoc1.Load(@"..\..\JeffFerguson.Gepsio.Test\XBRL-CONF-2014-12-10\Common\300-instance\301-01-IdScopeValid.xml");
            ShowContextsInDocument(xbrlDoc1);
            ShowFactsInDocument(xbrlDoc1);
        }

        private static void ShowContextsInDocument(XbrlDocument doc)
        {
            foreach (var currentFragment in doc.XbrlFragments)
            {
                ShowContextsInFragment(currentFragment);
            }
        }

        private static void ShowContextsInFragment(XbrlFragment currentFragment)
        {
            foreach (var currentContext in currentFragment.Contexts)
            {
                ShowContext(currentContext);
            }
        }

        private static void ShowContext(Context currentContext)
        {
            Console.WriteLine("CONTEXT");
            Console.WriteLine($"\tID          : {currentContext.Id}");
             Console.Write($"\tPeriod Type : ");
            if(currentContext.InstantPeriod)
            {
                Console.WriteLine("instant");
                Console.WriteLine($"\tInstant Date: {currentContext.InstantDate}");
            }
            else if(currentContext.DurationPeriod)
            {
                Console.WriteLine("period");
                Console.WriteLine($"\tPeriod Date : from {currentContext.PeriodStartDate} to {currentContext.PeriodEndDate}");
            }
            else if(currentContext.ForeverPeriod)
            {
                Console.WriteLine("forever");
            }
        }

        private static void ShowFactsInDocument(XbrlDocument doc)
        {
            foreach (var currentFragment in doc.XbrlFragments)
            {
                ShowFactsInFragment(currentFragment);
            }
        }

        private static void ShowFactsInFragment(XbrlFragment currentFragment)
        {
            foreach (var currentFact in currentFragment.Facts)
            {
                ShowFact(currentFact);
            }
        }

        private static void ShowFact(Fact fact)
        {
            Console.WriteLine($"FACT {fact.Name}");
            if (fact is Item)
            {
                ShowItem(fact as Item);
            }
        }

        private static void ShowItem(Item item)
        {
            Console.WriteLine("\tType      : Item");
            Console.WriteLine($"\tNamespace : {item.Namespace}");
            Console.WriteLine($"\tValue     : {item.Value}");
            Console.WriteLine($"\tContext ID: {item.ContextRefName}");
            if (item.ContextRef != null)
                ShowContext(item.ContextRef);
        }
    }
}