XSLT - adding a new item in a specific place

I am new to XSLT and I do not know how to accomplish the following.

The following is part of an xbrl document created by another program. Through XSLT, I would like to add an extra element to the position of my comment line:

<xbrli:context id="ctx1">
[...]
</xbrli:context>
//Insert a new element here!
<bd-ob-tuple:TaxData>
[...]
</bd-ob-tuple:TaxData>

At this point, I would like to add the following element using XSLT:

<xbrli:unit id="EUR">
  <xbrli:measure>iso4217:EUR</xbrli:measure>
</xbrli:unit>

So, the end result will be:

<xbrli:context id="ctx1">
[...]
</xbrli:context>
<xbrli:unit id="EUR">
  <xbrli:measure>iso4217:EUR</xbrli:measure>
</xbrli:unit>
<bd-ob-tuple:TaxData>
[...]
</bd-ob-tuple:TaxData>

(An element xbrli:contexthas only one occurrence in the entire document, so is it perhaps easier to find this position for a new element?)

Is there any way to accomplish this through XSLT?

+3
source share
2 answers

This conversion is :

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xbrli="some:xbrli" exclude-result-prefixes="xbrli">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="node()|@*" name="identity">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="xbrli:context">
  <xsl:call-template name="identity"/>
  <xbrli:unit id="EUR">
     <xbrli:measure>iso4217:EUR
     </xbrli:measure>
  </xbrli:unit>
 </xsl:template>
</xsl:stylesheet>

when applied to the following XML document (cleaning and packaging the provided text to make it correct):

<t xmlns:xbrli="some:xbrli" xmlns:bd-ob-tuple="some:bd-ob-tuple">
    <xbrli:context id="ctx1"> [...] </xbrli:context>
    <bd-ob-tuple:TaxData> [...] </bd-ob-tuple:TaxData>
</t>

, :

<t xmlns:bd-ob-tuple="some:bd-ob-tuple" xmlns:xbrli="some:xbrli">
    <xbrli:context id="ctx1"> [...] </xbrli:context>
    <xbrli:unit id="EUR">
        <xbrli:measure>iso4217:EUR
     </xbrli:measure>
    </xbrli:unit>
    <bd-ob-tuple:TaxData> [...] </bd-ob-tuple:TaxData>
</t>

: , xbrli:context.

+3

:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
    xmlns:xbrli="http://xbrli" xmlns:bd-ob-tuple="http://bd-ob-tuple" >
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()" />
        </xsl:copy>
        <xsl:if
            test="local-name() = 'context' and namespace-uri() = 'http://xbrli'">
            <xbrli:unit id="EUR">
                <xbrli:measure>iso4217:EUR</xbrli:measure>
            </xbrli:unit>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>
0

Source: https://habr.com/ru/post/1789776/


All Articles