Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a namespace to elements

I have an XML document with un-namespaced elements, and I want to use XSLT to add namespaces to them. Most elements will be in namespace A; a few will be in namespace B. How do I do this?

like image 757
Craig Walker Avatar asked Sep 27 '08 23:09

Craig Walker


People also ask

How do you add a namespace to an XML element in Java?

Add name space to document root element as attribute. Transform the document to XML string. The purpose of this step is to make the child element in the XML string inherit parent element namespace. Now the xml string have name space.

How do you add a namespace in HTML?

The namespace can be defined by an xmlns attribute in the start tag of an element. The namespace declaration has the following syntax. xmlns:prefix="URI".

How do you put a name space in XML?

To add namespaces to the XML constructed by the FOR XML query, first specify the namespace prefix to URI mappings by using the WITH NAMESPACES clause. Then, use the namespace prefixes in specifying the names in the query as shown in the following modified query.

How do you add a namespace on boomi?

It is used by all types that do not declare a namespace. Click Add Namespace to add a namespace. Namespaces are created on the XML profile's Types tab and referenced by elements on the Data Elements tab. Used to identify the namespace.


1 Answers

With foo.xml

<foo x="1">
    <bar y="2">
        <baz z="3"/>
    </bar>
    <a-special-element n="8"/>
</foo>

and foo.xsl

    <xsl:template match="*">
        <xsl:element name="{local-name()}" namespace="A" >
            <xsl:copy-of select="attribute::*"/>
            <xsl:apply-templates />
        </xsl:element>
    </xsl:template>

    <xsl:template match="a-special-element">
        <B:a-special-element xmlns:B="B">
            <xsl:apply-templates match="children()"/>
        </B:a-special-element>
    </xsl:template>

</xsl:transform>

I get

<foo xmlns="A" x="1">
    <bar y="2">
        <baz z="3"/>
    </bar>
    <B:a-special-element xmlns:B="B"/>
</foo>

Is that what you’re looking for?

like image 67
andrewdotn Avatar answered Nov 14 '22 16:11

andrewdotn