I want to transfor hierarchical elements that represent a tree. The data is provided inside a table in this format:
<?xml version="1.0" encoding="utf-16"?>
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
<asx:values>
<CONCEPTS>
<item>
<NO>1</NO>
<NS_PREFIX>de-gaap-ci</NS_PREFIX>
<XBRL_ID>bs.ass.monetary</XBRL_ID>
<IS_TUPLE>false</IS_TUPLE>
<FATHER_ID/>
<CONTEXT_REF>D-2012</CONTEXT_REF>
<UNIT_REF>EUR</UNIT_REF>
<DECIMALS>2</DECIMALS>
<PRECISION/>
<IS_NIL>false</IS_NIL>
<VALUE>12923.00-</VALUE>
</item>
<item>
<NO>2</NO>
<NS_PREFIX>de-gaap-ci</NS_PREFIX>
<XBRL_ID>bs.ass.string</XBRL_ID>
<IS_TUPLE>false</IS_TUPLE>
<FATHER_ID/>
<CONTEXT_REF>D-2012</CONTEXT_REF>
<UNIT_REF/>
<DECIMALS/>
<PRECISION/>
<IS_NIL>false</IS_NIL>
<VALUE>Test String</VALUE>
</item>
<item>
<NO>3</NO>
<NS_PREFIX>de-gaap-ci</NS_PREFIX>
<XBRL_ID>bs.ass.nil</XBRL_ID>
<IS_TUPLE>false</IS_TUPLE>
<FATHER_ID/>
<CONTEXT_REF>D-2012</CONTEXT_REF>
<UNIT_REF/>
<DECIMALS/>
<PRECISION/>
<IS_NIL>true</IS_NIL>
<VALUE/>
</item>
<item>
<NO>4</NO>
<NS_PREFIX>de-gaap-ci</NS_PREFIX>
<XBRL_ID>bs.ass.tuple1</XBRL_ID>
<IS_TUPLE>true</IS_TUPLE>
<FATHER_ID/>
<CONTEXT_REF>D-2012</CONTEXT_REF>
<UNIT_REF/>
<DECIMALS/>
<PRECISION/>
<IS_NIL>false</IS_NIL>
<VALUE/>
</item>
<item>
<NO>5</NO>
<NS_PREFIX>de-gaap-ci</NS_PREFIX>
<XBRL_ID>bs.ass.a</XBRL_ID>
<IS_TUPLE>false</IS_TUPLE>
<FATHER_ID>bs.ass.tuple1</FATHER_ID>
<CONTEXT_REF>D-2012</CONTEXT_REF>
<UNIT_REF/>
<DECIMALS/>
<PRECISION/>
<IS_NIL>false</IS_NIL>
<VALUE>Value for bs.ass.a</VALUE>
</item>
<item>
<NO>6</NO>
<NS_PREFIX>de-gaap-ci</NS_PREFIX>
<XBRL_ID>bs.ass.b</XBRL_ID>
<IS_TUPLE>false</IS_TUPLE>
<FATHER_ID>bs.ass.tuple1</FATHER_ID>
<CONTEXT_REF>D-2012</CONTEXT_REF>
<UNIT_REF/>
<DECIMALS/>
<PRECISION/>
<IS_NIL>false</IS_NIL>
<VALUE>Value for bs.ass.b</VALUE>
</item>
</CONCEPTS>
<CONTEXTS>
<item>
<ID>D-2012</ID>
<SCHEME>http://www.rzf-nrw.de/Steuernummer</SCHEME>
<IDENTIFIER>5117050051729</IDENTIFIER>
<IS_INSTANT>false</IS_INSTANT>
<DATE_A>2012-08-28</DATE_A>
<DATE_B>2012-08-30</DATE_B>
</item>
<item>
<ID>I-2012</ID>
<SCHEME>http://www.rzf-nrw.de/Steuernummer</SCHEME>
<IDENTIFIER>5117050051729</IDENTIFIER>
<IS_INSTANT>true</IS_INSTANT>
<DATE_A>2012-08-28</DATE_A>
<DATE_B/>
</item>
</CONTEXTS>
<UNITS>
<item>
<ID>EUR</ID>
<MEASURE_NS>iso4217</MEASURE_NS>
<MEASURE_VALUE>EUR</MEASURE_VALUE>
</item>
(...)
</UNITS>
<NAMESPACES>
<item>
<PREFIX>de-gcd</PREFIX>
<URI>http://www.xbrl.de/taxonomies/de-gcd-2011-09-14</URI>
<IS_DEFAULT>false</IS_DEFAULT>
</item>
(...)
</NAMESPACES>
<SCHEMAS>
<item>
<SCHEMA_REF>http://www.xbrl.de/taxonomies/de-gcd-2011-09-14.xsd</SCHEMA_REF>
</item>
(...)
</SCHEMAS>
</asx:values>
</asx:abap>
The column IS_TUPLE
says that this element is a father node inside the tree. All -elements that have the XBRL_ID
of this father inside the column FATHER_ID
are supposed to be the children of this father. Hence, if the column FATHER_ID
is empty the node has no father.
I want to process these elements recursively with XSLT. The target format is XBRL which is basically XML. Here is my attempt:
<xsl:template match="/">
<xbrl ...>
<xsl:apply-templates select="/CONCEPTS/item">
<xsl:with-param name="rec_father_node" select="/" />
</xsl:apply-templates>
</xbrl>
</xsl:template>
<xsl:template name="tpl_concept" match="/CONCEPTS/item">
<xsl:param name="rec_father_node"/>
<xsl:variable name="rec_father_id" select="string($rec_father_node/XBRL_ID/text())"/>
<xsl:variable name="father_id" select="string(*[local-name()='FATHER_ID'])"/>
<xsl:variable name="is_tuple" select="string(*[local-name()='IS_TUPLE'])"/>
<xsl:variable name="ns_prefix" select="string(*[local-name()='NS_PREFIX'])"/>
<xsl:variable name="xbrl_id" select="string(*[local-name()='XBRL_ID'])"/>
<xsl:variable name="name" select="$xbrl_id"/>
<xsl:if test="$is_tuple = 'false' and $rec_father_id = $father_id">
<xsl:element name="{$name}">
(...)
</xsl:element>
</xsl:if>
<xsl:if test="$is_tuple = 'true' and $rec_father_id = $father_id">
<xsl:element name="{$name}">
<xsl:choose>
<xsl:when test="$is_nil = 'true'">
<xsl:attribute name="xsi:nil">true</xsl:attribute>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="/CONCEPTS/item">
<xsl:with-param name="rec_father_id" select="current()" />
</xsl:apply-templates>
</xsl:otherwise>
</xsl:choose>
</xsl:element>
</xsl:if>
</xsl:template>
My idea was the following: nodes that are no father have the string value of '' inside the column FATHER_ID
. Therefore, I pass the root node to the parameter rec_father_node
to get this initial string. With the recursive call of the same template the parameter rec_father_node
contains the node of this father so I can read out the XBRL_ID
of this father to only add elements that have this value inside the column FATHER_ID
.
My problem is the following: the parameter rec_father_node
looses its value passed by the recursion call. The value is always the root node which is passed by the initial <xsl:apply-templates .../>
call.
Is there a way to pass a parameter to recursive template calls? Or in general, how can I transform this flat table into a XML node tree with nested elements?
The target of the transformation should look like this (taken names from the example above and added some other nodes for clarity):
<?xml version="1.0" encoding="UTF-8"?>
<xbrl>
<bs.ass.monetary>12923.00-</bs.ass.monetary>
<bs.ass.string>Test String</bs.ass.string>
<bs.ass.nil></bs.ass.nil>
<bs.ass.tuple1>
<bs.ass.a>Value for bs.ass.a</bs.ass.a>
<bs.ass.b>Value for bs.ass.b</bs.ass.b>
</bs.ass.tuple1>
</xbrl>
Thanks for any help or comments!
What you could do is firstly define a key to look up 'child' items based on their FATHER_ID elements.
<xsl:key name="children" match="item" use="FATHER_ID" />
Firstly though you would start off by matching items with no father
<xsl:apply-templates select="item[FATHER_ID='']"/>
Then, for items which are 'tuples' you could match them, and recursively match their children with a key
<xsl:template match="item[IS_TUPLE='true']">
<xsl:element name="{XBRL_ID}">
<xsl:apply-templates select="key('children', XBRL_ID)" />
</xsl:element>
</xsl:template>
You would also have another template for item elements, which would match non-tuples, which would output whatever value you like.
Try the following XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:key name="children" match="item" use="FATHER_ID"/>
<xsl:template match="/">
<xsl:apply-templates select="//CONCEPTS"/>
</xsl:template>
<xsl:template match="CONCEPTS">
<xbrl>
<xsl:apply-templates select="item[FATHER_ID='']"/>
</xbrl>
</xsl:template>
<xsl:template match="item[IS_TUPLE='true']">
<xsl:element name="{XBRL_ID}">
<xsl:apply-templates select="key('children', XBRL_ID)"/>
</xsl:element>
</xsl:template>
<xsl:template match="item">
<xsl:element name="{XBRL_ID}">
<xsl:value-of select="VALUE" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
When applied to your sample, you get the following
<xbrl>
<bs.ass.monetary>12923.00-</bs.ass.monetary>
<bs.ass.string>Test String</bs.ass.string>
<bs.ass.nil></bs.ass.nil>
<bs.ass.tuple1>
<bs.ass.a>Value for bs.ass.a</bs.ass.a>
<bs.ass.b>Value for bs.ass.b</bs.ass.b>
</bs.ass.tuple1>
</xbrl>