Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8995421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:37:04+00:00 2026-06-15T23:37:04+00:00

I have an xml document where some of the elements have a namespace and

  • 0

I have an xml document where some of the elements have a namespace and others do not. All of them need namespaces, some the same some different. The elements have properties which I want to keep.

The xml:

<foo xmlns:gml="http://www.opengis.net/gml" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:gco="http://www.isotc211.org/2005/gco" xsi:schemaLocation="http://www.isotc211.org/2005/gmd http://schemas.opengis.net/iso/19139/20060504/gmd/gmd.xsd" xmlns="http://www.isotc211.org/2005/gmd">
<bar y="2">
<baz z="3"/></bar>
<a-special-element n="8"/>
<another-special-element k="8"/>
</foo>

And the xslt:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><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="foo">
    <xx:foo xmlns:xx="xx">
        <xsl:apply-templates/>
    </xx:foo>
</xsl:template>

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

<xsl:template match="another-special-element">
    <C:a-special-element xmlns:C="C">
        <xsl:apply-templates/>
    </C:another-special-element>
</xsl:template>

</xsl:stylesheet>

Here is the output i would like to have:

<xx:foo xmlns:gml="http://www.opengis.net/gml" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:gco="http://www.isotc211.org/2005/gco" xsi:schemaLocation="http://www.isotc211.org/2005/gmd http://schemas.opengis.net/iso/19139/20060504/gmd/gmd.xsd" xmlns="http://www.isotc211.org/2005/gmd">    <bar y="2">
    <baz z="3"/>
</bar>
<B:a-special-element n="8"/>
<C:another-special-element k="4"/>

</xx:foo>

I checked out this thread but there the property of the “a-special-element” has been magically removed. Add a namespace to elements

Also I have multiple xmlns:??? in the foo that I want to keep.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:37:05+00:00Added an answer on June 15, 2026 at 11:37 pm

    First of all: namespaces are a fundamental concept in XML. If you are not familiar with namespaces, please take time to learn and understand them.

    I have an xml document where some of the elements have a namespace and
    others do not.

    Actually in your code sample all the elements belong to a namespace.

    Code xmlns="http://www.isotc211.org/2005/gmd" in your root element defines a default namespace with URI "http://www.isotc211.org/2005/gmd". Thus this element and its descendants belong to this default namespace if the element name doesn’t have a namespace prefix. It’s easy to forget that an element is in some namespace, if it uses the default namespace, because there is no namespace prefix. Note that default namespace doesn’t apply to attributes, only elements.

    Your expected result document is not clear. It should also have namespace definitions for the namespace prefixes xx, B and C. I also assumed that your namespace A is the same as the default namespace used your expected result document.

    Anyway, given this input:

    <foo xmlns:gml="http://www.opengis.net/gml"
         xmlns:xlink="http://www.w3.org/1999/xlink"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:gco="http://www.isotc211.org/2005/gco"
         xmlns="http://www.isotc211.org/2005/gmd"
         xsi:schemaLocation="http://www.isotc211.org/2005/gmd http://schemas.opengis.net/iso/19139/20060504/gmd/gmd.xsd"
         >
        <bar y="2">
            <baz z="3"/>
        </bar>
        <a-special-element n="8"/>
        <another-special-element k="8"/>
    </foo>
    

    This XSLT code:

    <xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:gml="http://www.opengis.net/gml"
        xmlns:xlink="http://www.w3.org/1999/xlink"
        xmlns:gco="http://www.isotc211.org/2005/gco"
        xmlns:gmd="http://www.isotc211.org/2005/gmd"
        xmlns="http://www.isotc211.org/2005/gmd"
        xmlns:B="B"
        xmlns:C="C"
        xmlns:xx="xx"
        >
    
    <xsl:template match="node() | @*" name="identity">
        <xsl:copy>
            <xsl:apply-templates select="node() | @*"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="*">
        <xsl:element name="{local-name()}">
            <xsl:apply-templates select="node() | @*"/>
        </xsl:element>
    </xsl:template>
    
    <xsl:template match="gmd:foo">
        <xx:foo>
            <xsl:apply-templates select="node() | @*"/>
        </xx:foo>
    </xsl:template>
    
    <xsl:template match="gmd:a-special-element">
        <B:a-special-element>
            <xsl:apply-templates select="node() | @*"/>
        </B:a-special-element>
    </xsl:template>
    
    <xsl:template match="gmd:another-special-element">
        <C:another-special-element>
            <xsl:apply-templates select="node() | @*"/>
        </C:another-special-element>
    </xsl:template>
    
    </xsl:stylesheet>
    

    Will produce this output:

    <xx:foo xmlns:xx="xx" 
            xmlns:gml="http://www.opengis.net/gml" 
            xmlns:xlink="http://www.w3.org/1999/xlink" 
            xmlns:gco="http://www.isotc211.org/2005/gco" 
            xmlns:gmd="http://www.isotc211.org/2005/gmd" 
            xmlns="http://www.isotc211.org/2005/gmd" 
            xmlns:B="B" 
            xmlns:C="C" 
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
            xsi:schemaLocation="http://www.isotc211.org/2005/gmd http://schemas.opengis.net/iso/19139/20060504/gmd/gmd.xsd"
            >
        <bar y="2">
            <baz z="3"/>
        </bar>
        <B:a-special-element n="8"/>
        <C:another-special-element k="8"/>
    </xx:foo>
    

    Notice how an identity template is used to recursively copy the contents of the input document which is a common and useful technique.

    Note that the default namespace of the XSLT document doesn’t apply to element names used in <xsl:template match="ELEMENT-NAME">. Therefore you need to define a namespace prefix for the default namespace of the input document and use that prefix when you refer to those elements, for example in the match and select attributes. Also note that because of this, the namespace URI http://www.isotc211.org/2005/gmd is defined twice in the result document – with the prefix gmd and as a default namespace. If this bothers you, you can add exclude-result-prefixes="gmd" attribute to the <xsl:stylesheet> element. As a side effect, this could cause your default namespace definition appear later in the document and not in the root element but that is just a visual difference, the underlying functionality stays the same.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML document in which I want to search for some elements
I have some XML document with <item> elements, and i'd want to wrap each
I have an xml document that contains some Item elements with ids. I want
In an XML document, I have elements which share the same name, but the
I have an XML document containing contact information as seen below: <contact type=individual> <firstname>Some</firstname>
I have written some code that loads an XML document using an XmlDocument object
I have some xml documents I need to run queries on. I've created some
So, i have some question about xml Documents in Java. Can i get all
I have an XML document with a default namespace indicated at the root. Something
I have an XML document that describes how to build UI elements for user

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.