Search code examples
xslt-1.0xbrl

Counting child notes in an document with multiple namespaces (XBRL)


I am trying (but not succeeding) to count the children in an XBRL document. I want to know how many schemas, unit, contexts and facts grouped by namespace prefix are used.

Input:

<?xml version="1.0" encoding="utf-8"?>
<xbrl xml:lang="en" xmlns="http://www.xbrl.org/2003/instance"  
      xmlns:link="http://www.xbrl.org/2003/linkbase" 
      xmlns:find="http://www.eurofiling.info/xbrl/ext/filing-indicators" 
      xmlns:xlink="http://www.w3.org/1999/xlink"  >
  <link:schemaRef xlink:type="simple" xlink:href="http://www.eba.europa.eu/eu/fr/xbrl/crr/fws/corep/its-2013-02/2014-03-31/mod/corep_le_con.xsd" />
  <context id="I-2014-9-E">
    <entity>
      <identifier scheme="http://www.dnb.nl/id">123</identifier>
    </entity>
    <period>
      <instant>2014-09-30</instant>
    </period>
  </context>
  <unit id="u-EUR">
    <measure>iso4217:EUR</measure>
  </unit>
  <unit id="u-pure">
    <measure>pure</measure>
  </unit>
  <find:fIndicators>
    <find:filingIndicator contextRef="I-2014-9-E">C_00.01</find:filingIndicator>
  </find:fIndicators>
  <find:fIndicators>
    <find:filingIndicator contextRef="I-2014-9-E">C_26.00</find:filingIndicator>
  </find:fIndicators>
  <find:fIndicators>
    <find:filingIndicator contextRef="I-2014-9-E">C_27.00</find:filingIndicator>
  </find:fIndicators>
</xbrl>

Wanted output:

<?xml version="1.0" encoding="utf-8"?>
<XBRLfacts xmlns="http://www.xbrl.org/2003/instance" xmlns:link="http://www.xbrl.org/2003/linkbase">
  <linkCount>1</linkCount>
  <unitCount>2</unitCount>
  <contextCount></contextCount>
  <factCount>
    <find>3</find>
  </factCount>
</XBRLfacts>

XSLT tried:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.xbrl.org/2003/instance" xmlns:link="http://www.xbrl.org/2003/linkbase"  >
  <xsl:output method="xml" encoding="UTF-8" indent="yes" media-type="text/xml" />

  <xsl:template match="/">
    <XBRLfacts >
        <linkCount>
            <xsl:value-of select="//xbrl/link:schemaRef" />
        </linkCount>
        <unitCount>
            <xsl:value-of select="//xbrl/unit" />
        </unitCount>
        <contextCount>
            <xsl:value-of select="//xbrl/context" />
        </contextCount>
        <!-- something for the facts -->
    </XBRLfacts>
  </xsl:template>

</xsl:stylesheet>

Output gotten:

<?xml version="1.0" encoding="utf-8"?>
<XBRLfacts xmlns="http://www.xbrl.org/2003/instance" xmlns:link="http://www.xbrl.org/2003/linkbase">
  <linkCount></linkCount>
  <unitCount></unitCount>
  <contextCount></contextCount>
</XBRLfacts>

Any help telling me what I am doing wrong is greatly appreciated.

Thanks.

Paul.


Solution

  • Your source elements are in namespaces. You must assign a prefix to each namespace and use it when addressing the elements in that namespace.

    The other thing is that you're not actually counting anything.

    XSLT 1.0

    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://www.xbrl.org/2003/instance"
    xmlns:xbrl="http://www.xbrl.org/2003/instance"
    xmlns:link="http://www.xbrl.org/2003/linkbase"
    xmlns:find="http://www.eurofiling.info/xbrl/ext/filing-indicators" 
    exclude-result-prefixes="xbrl find">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    
    <xsl:template match="/xbrl:xbrl">
        <XBRLfacts>
            <linkCount>
                <xsl:value-of select="count(link:schemaRef)" />
            </linkCount>
            <unitCount>
                <xsl:value-of select="count(xbrl:unit)" />
            </unitCount>
            <contextCount>
                <xsl:value-of select="count(xbrl:context)" />
            </contextCount>
            <fIndicatorCount>
                <xsl:value-of select="count(find:fIndicators)" />
            </fIndicatorCount>
        </XBRLfacts>
      </xsl:template>
    
    </xsl:stylesheet>
    

    Result:

    <?xml version="1.0" encoding="UTF-8"?>
    <XBRLfacts xmlns="http://www.xbrl.org/2003/instance" xmlns:link="http://www.xbrl.org/2003/linkbase">
      <linkCount>1</linkCount>
      <unitCount>2</unitCount>
      <contextCount>1</contextCount>
      <fIndicatorCount>3</fIndicatorCount>
    </XBRLfacts>