I'm new to XSLT and I don't know how to accomplish the following.
Below is a part of a xbrl document, generate开发者_运维技巧d by another program. Via XSLT, I would like to add an extra element at 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 that location, I would like to add the following element using XSLT:
<xbrli:unit id="EUR">
<xbrli:measure>iso4217:EUR</xbrli:measure>
</xbrli:unit>
So that the final 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>
(The xbrli:context
element has only one occurence in the whole document, so that perhaps makes it easier to locate the mentioned position for the new element?)
Is there a way I can accomplish this via XSLT?
This transformation:
<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 on the following XML document (cleaning and wrapping the provided text to make it well-formed):
<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>
produces the wanted, correct result:
<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>
Explanation: Use of the identity rule, overriden for xbrli:context
.
How about this:
<?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>
精彩评论