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

  • Home
  • SEARCH
  • 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 680065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:19:47+00:00 2026-05-14T01:19:47+00:00

I can do it, but not for the default namespace, using the <xsl:namespace>. If

  • 0

I can do it, but not for the default namespace, using the <xsl:namespace>. If I try to do it for the default namespace:

<xsl:namespace name="" select"myUri"/>

it never works. It demands that I explicitly define the namespace of the element to be able to use the above null prefix declaration.
The reason I want this is because I have a task to transform an input XML file to another output xml. The output XML has many elements and i dont want to have to explicitly set the namespace for every element. Thats why I want to set the default and never bother again. But the default must be computed from some data in the source XML. It does not change during the whole transformation, but it is dependent on input XML data.
Any solution?

EDIT 1:
To sup up:

  1. I want to create a namespace dynamically and set it to be the default namespace of the output xml document. The uri of the namespace is derived from some data in the input XML.
  2. If I use <xsl:namespace> in my root output element, I cannot create a default namespace for it, only a prefixed one. And even with the prefixed one, it does not propagate to children.

EDIT 2:
dkackman proposed:

<xsl:template match="root">
  <xsl:param name ="ns">my-computed-namespace</xsl:param>
  <xsl:element name="newRoot" namespace="{$ns}"/>
</xsl:template>

It almost solves the problem. Unfortunately the children are injected with “”(blank) namespace by the transformer. Here is what I get if I put a child element:

<newRoot xmlns="my-computed-namespace"> 
    <child xmlns=""> ... 
    </child> 
 </newRoot>

Why does the transformer put this xmlns="" in the children? If I can prevent this then I have found my solution.

  • 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-05-14T01:19:47+00:00Added an answer on May 14, 2026 at 1:19 am

    In addition to @Tomalak who has provided the precise answer, do note that <xsl:namespace> is not intended to create a namespace declaration to be used by the XSLT processor generally with all elements or attributes.

    The purpose of <xsl:namespace> is to create a specific namespace node. This node has a limited scope only: the current element or attribute, and all children of the current node, if they do not re-assign the prefix to another namespace-uri.

    Using <xsl:namespace> is necessary only if we want to create a namespace dynamically for a namespace-uri that must be generated dynamically (was not known statically at the start of the transformation). Such cases are extremely rare.

    In all cases when the desired namspace-uri is known statically, simply declare this namespace at a suitable level of visibility (usually at the <xsl:stylesheet> instruction) and then simply use the associated prefix, anywhere this namespace must be used.

    UPDATE:
    I have just confirmed in a dialog with specialists in another forum that this is not possible to do with <xsl:namespace>. It adds a namespace node with no name to the current element, but literal result elements are copied 1:1 and remain in their (no) namespace.

    Here is how Dr. Michael Kay, the Editor of the W3C WG on XSLT explains this:

    “You need to create elements and attributes with the correct expanded name at
    the time you create them. If that means using xsl:element, so be it.
    xsl:namespace can only be used to create additional namespace nodes to those
    that are created automatically for the prefixes/uris used in element and
    attribute names; it can’t be used to modify the name of an element or
    attribute node.
    As always, to understand this you need to understand the data model for
    namespaces. An element/attribute name is a triple, containing (prefix, uri,
    localname)
    . A namespace node is a pair (prefix, uri). There is a consistency
    rule that if an element or attribute name exists containing prefix=P uri=U
    then there must be a namespace node (P, U). The namespace fixup process
    ensures that this namespace node is created automatically when you create an
    element or attribute. xsl:namespace is there to allow you to create
    additional namespace nodes, typically for namespaces used in QName-valued
    content
    “.

    If such result is needed, the solution is to use a second pass and to convert any element belonging to “no namespace” to the desired new namespace.

    This is the transformation to use in the second pass (the two passes can be combined into a single stylesheet/transformation):

    <xsl:stylesheet  version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
      <xsl:variable name="vUrl" select="'my:Url'"/>
    
     <xsl:template match="*[namespace-uri()='']">
       <xsl:element name="{name()}" namespace="{$vUrl}">
         <xsl:copy-of select="@*"/>
         <xsl:apply-templates/>
       </xsl:element>
    
     </xsl:template>
    </xsl:stylesheet>
    

    When the above transformation is applied on the following sample (pass-1-result) xml document:

    <a>
      <b>
        <c/>
      </b>
    </a>
    

    The desired result is produced:

    <a xmlns="my:Url">
        <b>
            <c/>
        </b>
    </a>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.