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 539103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:06:06+00:00 2026-05-13T10:06:06+00:00

I would like to avoid producing a repeated namespace in my XSLT output. (I

  • 0

I would like to avoid producing a repeated namespace in my XSLT output.

(I am using XSLT to massage some XML so that Microsoft’s DataContractSerializer sees fit to actually process it properly. One of the things that the DCS doesn’t seem to like is defining the same namespace multiple times.)

I am taking all of the “Characteristics” elements from under the XXX element and grouping them together in a new array element like so:

<xsl:stylesheet version="1.0" 
              xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
              xmlns:feature="some namespace">
<xsl:template match="feature:XXX">
  <xsl:copy>
    <feature:Characteristics>
      <xsl:apply-templates select="feature:Characteristics"/>
     </feature:Characteristics>
    <xsl:copy-of select="*[not(self::feature:Characteristics)]" />
  </xsl:copy>
</xsl:template>
<xsl:template match="feature:Characteristics">
  <arrays:unsignedShort xmlns:arrays="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
    <xsl:value-of select="." />
  </arrays:unsignedShort>
</xsl:template>
</xsl:stylesheet>

The “feature” namespace is defined at the top of the XSLT file. However the value “feature” is not going to be found in the source XML, it will be some arbitrary prefix (usually “h”). The problem is that if I use this XSLT then the namespace is assigned to 2 prefixes in the output XML: the original namespace from the input XML (usually “h”) and “feature” generated by the XSLT. (While valid XML, this confuses poor Microsoft.)

So I would like to completely avoid defining the “feature” namespace in the output XML by instead referring to the current element’s namespace instead. I tried variants on this but I don’t know how to set the namespace value of the xsl:element correctly in order to get the current context node’s namespace prefix…

<xsl:stylesheet version="1.0" 
              xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
              xmlns:feature="some namespace">
  <xsl:template match="feature:XXX">
   <xsl:copy>
    <xsl:element name="Characteristics" namespace="{???}">
      <xsl:apply-templates select="feature:Characteristics"/>
    </xsl:element>
    <xsl:copy-of select="*[not(self::feature:Characteristics)]" />
  </xsl:copy>
</xsl:template>
  <xsl:template match="feature:Characteristics">
  <arrays:unsignedShort xmlns:arrays="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
    <xsl:value-of select="." />
  </arrays:unsignedShort>
</xsl:template>
</xsl:stylesheet>

Sample input:

<h:XXX xmlns:h="http://stackoverflow.com">
 <h:Characteristics>7</h:Characteristics>
 <h:Characteristics>11</h:Characteristics>
 <h:Characteristics>12</h:Characteristics>
 <h:ProductName>blah</h:ProductName>
 <h:Vendor>blah</h:Vendor>
 <h:Version></h:Version>
</h:XXX>

Repeat namespaces (bad):

<h:XXX xmlns:h="http://stackoverflow.com">
 <feature:Characteristic xmlns:feature="http://stackoverflow.com">
      <arrays:unsignedShort xmlns:arrays="http://schemas.microsoft.com/2003/10/Serialization/Arrays">7</arrays:unsignedShort>
      <arrays:unsignedShort xmlns:arrays="http://schemas.microsoft.com/2003/10/Serialization/Arrays">11</arrays:unsignedShort>
      <arrays:unsignedShort xmlns:arrays="http://schemas.microsoft.com/2003/10/Serialization/Arrays">12</arrays:unsignedShort>
 </feature:Characteristic>
 <h:ProductName>blah</h:ProductName>
 <h:Vendor>blah</h:Vendor>
 <h:Version></h:Version>
</h:XXX>

Desired output:

<h:XXX xmlns:h="http://stackoverflow.com">
 <h:Characteristic>
      <arrays:unsignedShort xmlns:arrays="http://schemas.microsoft.com/2003/10/Serialization/Arrays">7</arrays:unsignedShort>
      <arrays:unsignedShort xmlns:arrays="http://schemas.microsoft.com/2003/10/Serialization/Arrays">11</arrays:unsignedShort>
      <arrays:unsignedShort xmlns:arrays="http://schemas.microsoft.com/2003/10/Serialization/Arrays">12</arrays:unsignedShort>
 </h:Characteristic>
 <h:ProductName>blah</h:ProductName>
 <h:Vendor>blah</h:Vendor>
 <h:Version></h:Version>
</h:XXX>

This XSLT almost works, but it involves hardcoding the “h” value, and I would like to support an arbitrary value instead:

<?xml version ="1.0" encoding="iso-8859-1"?>
  <xsl:stylesheet version="1.0" 
              xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
              xmlns:feature="some ns"
              xmlns:h="some ns"
              exclude-result-prefixes="h">

<xsl:template match="feature:XXX">
  <xsl:copy>
    <h:Characteristic>
      <xsl:apply-templates select="feature:Characteristics"/>
    </h:Characteristic>
    <xsl:copy-of select="*[not(self::feature:Characteristics)]" />
  </xsl:copy>
</xsl:template>

<xsl:template match="feature:Characteristics">
  <arrays:unsignedShort xmlns:arrays="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
    <xsl:value-of select="." />
  </arrays:unsignedShort>
 </xsl:template>

 </xsl:stylesheet>
  • 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-13T10:06:06+00:00Added an answer on May 13, 2026 at 10:06 am

    If you want consistent namespace prefixes then you will need to generate new elements, rather than copy-of. Set your namespace prefix to whatever you want it to be (feature, h, etc) in the XSLT.

    The following XSLT:

    <xsl:stylesheet version="1.0"
                  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                  xmlns:feature="http://stackoverflow.com"
                  xmlns:arrays="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
      <xsl:output method="xml" indent="yes" />
    
      <xsl:template match="feature:XXX">
       <xsl:element name="feature:XXX">
        <feature:Characteristic>
          <xsl:apply-templates select="feature:Characteristics"/>
        </feature:Characteristic>
        <xsl:apply-templates select="*[not(self::feature:Characteristics)]" />
      </xsl:element>
      </xsl:template>
    
      <xsl:template match="feature:Characteristics">
        <xsl:element name="arrays:unsignedShort">
            <xsl:value-of select="." />
        </xsl:element>
      </xsl:template>
    
      <xsl:template match="*">
         <xsl:element name="feature:{local-name()}" >
            <xsl:value-of select="." />
         </xsl:element>
      </xsl:template>
    
    </xsl:stylesheet>
    

    Generates the following output:

    <?xml version="1.0" encoding="UTF-16"?>
    <feature:XXX xmlns:feature="http://stackoverflow.com">
     <feature:Characteristic xmlns:arrays="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
      <arrays:unsignedShort>7</arrays:unsignedShort>
      <arrays:unsignedShort>11</arrays:unsignedShort>
      <arrays:unsignedShort>12</arrays:unsignedShort>
     </feature:Characteristic>
     <feature:ProductName>blah</feature:ProductName>
     <feature:Vendor>blah</feature:Vendor>
     <feature:Version></feature:Version>
    </feature:XXX>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some huge blocks of code and I would like to avoid using
Problem There are data gaps that need to be filled. Would like to avoid
I keep running into a minor inconvenience that I would like to avoid. If
I would like to avoid using CreateFileMapping/MapViewOfFile to rewrite part of a file. Does
I would like to avoid any lock-in and it seems to me that Socialauth
I would like to avoid the black screen between activities when the back button
I would like to avoid redistributing .NET runtime if possible with my application since
I would like to avoid pickling of certain fields in an instance of a
I would like to avoid the if statement below. Can I somehow copy the
When users preview a report I would like to avoid giving them the option

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.