Skriptcode

www.altova.com Dieses Kapitel drucken Eine Ebene nach oben

Startseite > 

Skriptcode

Das folgende mit Anmerkungen versehene Python-Skript verarbeitet Daten in einem beliebigen XBRL-Taxonomiedokument und erzeugt eine Liste aller Concept-Elemente und Tuples in der Taxonomie. Es erstellt ein Ausgabedokument namens report.html.

 

Das Skript wird mit einem Befehl wie dem folgenden an die Befehlszeilenschnittstelle übergeben:

 

raptorxml dts --script=dtsreport.py --script-api-version=1 AnyTaxonomy.xsd

 

Dateiname: dtsreport.py

 

import os

from altova import xml, xsd, xbrl

 

def getBalance(item):

 """Return the balance as string for the given item concept"""

 

 if item.balance == xbrl.Concept.DEBIT:

         return 'Debit'

 elif item.balance == xbrl.Concept.CREDIT:

         return 'Credit'

 else:

         return 'None'

 

 

def getPeriodType(item):

 """Return the period type as string for the given item concept"""

 

 if item.period_type == xbrl.Concept.INSTANT:

         return 'Instant'

 elif item.period_type == xbrl.Concept.DURATION:

         return 'Duration'

 else:

         return 'None'

 

 

def getElemTextValue(elem):

 """Return the text content of an XML element"""

 

 text = ''

 # Iterate through all child nodes and concatenate all character nodes

 for child in elem.children:

         if isinstance(child,xml.Character):

                 text += child.character_code

 return text

 

 

def getLabel(concept):

"""Return the text of the first label connected to this concept"""

 

 for label in concept.label_elements:

# Return the text value for the first connected label element

return getElemTextValue(label)

# If there are no labels connected to this concept return a non-breaking space

return ' '

 

 

def writeItem(f,item):

"""Write some information about the item concept"""

 

f.write('<h3>'+item.qname.local_name+'</h3>\n')

f.write('<p><table border="1">\n')

f.write('<tr><td>Name</td><td>'+item.qname.local_name+'</td></tr>')

f.write('<tr><td>Namespace</td><td>'+item.qname.namespace_name+'</td></tr>')

f.write('<tr><td>Type</td><td>'+item.element_declaration.type_definition.name+'</td></tr>')

f.write('<tr><td>Abstract</td><td>'+str(item.is_abstract())+'</td></tr>')

f.write('<tr><td>Nillable</td><td>'+str(item.is_nillable())+'</td></tr>')

f.write('<tr><td>Numeric</td><td>'+str(item.is_numeric())+'</td></tr>')

f.write('<tr><td>Balance</td><td>'+getBalance(item)+'</td></tr>')

f.write('<tr><td>Period Type</td><td>'+getPeriodType(item)+'</td></tr>')

f.write('<tr><td>Label</td><td>'+getLabel(item)+'</td></tr>')

f.write('</table></p>\n')

 

 

def writeTuple(f,tuple):

"""Write some information about the tuple concept"""

 

f.write('<h3>'+tuple.qname.local_name+'</h3>\n')

f.write('<p><table border="1">\n')

f.write('<tr><td>Name</td><td>'+tuple.qname.local_name+'</td></tr>')

f.write('<tr><td>Namespace</td><td>'+tuple.qname.namespace_name+'</td></tr>')

f.write('<tr><td>Abstract</td><td>'+str(tuple.is_abstract())+'</td></tr>')

f.write('<tr><td>Nillable</td><td>'+str(tuple.is_nillable())+'</td></tr>')

f.write('<tr><td>Label</td><td>'+getLabel(tuple)+'</td></tr>')

f.write('</table></p>\n')

 

 

def writeReport(dts,filename):

"""Write a report listing all the item and tuple concepts in the taxonomy"""

 

 # Open output file

 f = open(filename,'w')

 f.write('<html><title>Report</title><body>\n')

 # Write all item concepts

 f.write('<h1><center>Item Concepts</center></h1>\n')

 for item in dts.items:

         writeItem(f,item)

 # Write all tuple concepts

 f.write('<h1><center>Tuple Concepts</center></h1>\n')

 for tuple in dts.tuples:

         writeTuple(f,tuple)

 f.write('</body></html>\n')

 # Close output file

 f.close()

 

 

def on_dts_valid(job,dts):

"""This method will be automatically called by RaptorXMLXBRL after successful validation of the XBRL taxonomy"""

 

 # Create a 'report.html' file in the job's ouptut directory (when run from the CLI this will be the current working directory)

 filename = os.path.join(job.output_dir,'report.html')

 # Create report html document of the DTS taxonomy

 writeReport(dts,filename)

 # Register the newly generated 'report.html' output file

 job.append_output_filename(filename)

 


© 2019 Altova GmbH