Reputation:
I have a scenario where i want to copy all the attributes including namespaces [Here I mean the entire attribute list and namespace list and not just the values of the attributes] from an XML tag in the input XML.
For Ex:
Input XML:
<?xml version="1.0" encoding="UTF-8"?>
<enricher>
<result>
<xbrl xmlns="http://www.xbrl.org/2003/instance"
xmlns:idp-com="http://www.dnb.com/IDP/Common/Vers1"
xmlns:idp-enumcom="http://www.dnb.com/IDP/Common/Enumeration/Common/Vers1"
xsi:schemaLocation="http://www.dnb.com/IDP/Product/Common/Vers1 ../Common/ProductCommonTaxonomy.xsd
http://www.dnb.com/IDP/Common/Vers1 ../../Data/Common/CommonTaxonomy.xsd">
<context id="defaultI">
<entity>
<identifier scheme="http://www.dnb.com">Text</identifier>
</entity>
<period>
<instant>2000-07-14</instant>
</period>
</context>
</xbrl>
</result>
</enricher>
Output XML:
<?xml version="1.0" encoding="UTF-8"?>
<enricher>
<result>
<xbrlresp xmlns="http://www.xbrl.org/2003/instance"
xmlns:idp-com="http://www.dnb.com/IDP/Common/Vers1"
xmlns:idp-enumcom="http://www.dnb.com/IDP/Common/Enumeration/Common/Vers1"
xsi:schemaLocation="http://www.dnb.com/IDP/Product/Common/Vers1 ../Common/ProductCommonTaxonomy.xsd
http://www.dnb.com/IDP/Common/Vers1 ../../Data/Common/CommonTaxonomy.xsd">
<context id="defaultI">
<entity>
<identifier scheme="http://www.dnb.com">Text</identifier>
</entity>
<period>
<instant>2000-07-14</instant>
</period>
</context>
</xbrlresp>
</result>
</enricher>
The output should have the <xbrlresp>
tag with all the namespaces and attributes of the <xbrl>
.
Upvotes: 0
Views: 144
Reputation: 122364
Extending the answer from Matthias, if, for aesthetic reasons, you want to copy the namespace declarations of the xbrl
element onto the new element you're creating, you can do this using
<xsl:template match="xb:xbrl">
<xsl:element name="xbrlresp" namespace="http://www.xbrl.org/2003/instance">
<xsl:copy-of select="namespace::*" />
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
The copy-of
copies the namespace nodes from the input xbrl
element onto the generated xbrlresp
, which should result in the serializer adding the namespace declarations.
Upvotes: 1
Reputation: 22617
EDIT: After you have revealed the actual problem, I have also changed the stylesheet. Again, your input is not well-formed XML because a namespace is not defined:
`xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
XML Input
<enricher>
<result>
<xbrl xmlns="http://www.xbrl.org/2003/instance"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:idp-com="http://www.dnb.com/IDP/Common/Vers1"
xmlns:idp-enumcom="http://www.dnb.com/IDP/Common/Enumeration/Common/Vers1"
xsi:schemaLocation="http://www.dnb.com/IDP/Product/Common/Vers1 ../Common/ProductCommonTaxonomy.xsd
http://www.dnb.com/IDP/Common/Vers1 ../../Data/Common/CommonTaxonomy.xsd">
<context id="defaultI">
<entity>
<identifier scheme="http://www.dnb.com">Text</identifier>
</entity>
<period>
<instant>2000-07-14</instant>
</period>
</context>
</xbrl>
</result>
</enricher>
XSLT Stylesheet
The stylesheet is a simple identity transformation with an exception, namely replacing the xbrl
element with a new one.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:xb="http://www.xbrl.org/2003/instance">
<xsl:strip-space elements="*"/>
<xsl:output method="xml" encoding="UTF-8" indent="yes" />
<xsl:template match="xb:xbrl">
<xsl:element name="xbrlresp" namespace="http://www.xbrl.org/2003/instance">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:transform>
XML Output
<?xml version="1.0" encoding="UTF-8"?>
<enricher>
<result>
<xbrlresp xmlns="http://www.xbrl.org/2003/instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.dnb.com/IDP/Product/Common/Vers1 ../Common/ProductCommonTaxonomy.xsd http://www.dnb.com/IDP/Common/Vers1 ../../Data/Common/CommonTaxonomy.xsd">
<context xmlns="http://www.xbrl.org/2003/instance" xmlns:idp-com="http://www.dnb.com/IDP/Common/Vers1" xmlns:idp-enumcom="http://www.dnb.com/IDP/Common/Enumeration/Common/Vers1" id="defaultI">
<entity>
<identifier scheme="http://www.dnb.com">Text</identifier>
</entity>
<period>
<instant>2000-07-14</instant>
</period>
</context>
</xbrlresp>
</result>
</enricher>
Upvotes: 1