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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:02:39+00:00 2026-06-11T19:02:39+00:00

I have an xml document that is structured somewhat like this :- <catalog xmlns=format_old

  • 0

I have an xml document that is structured somewhat like this :-

<catalog xmlns="format_old" xmlns:final="format_new">
  <final:book>
    <final:title>blah</final:title>
    <final:author>more blah</final:author>
  </final:book>
  <book>
    <description title="blah2"/>
    <writer name="more blah2"/>
  </book>
</catalog>

Obviously, this is a simplified version of the problem. What I want to do is to convert this into something like :-

<catalog xmlns="format_new">
  <book>
    <title>blah</title>
    <author>more blah</author>
  </book>
  <book>
    <title>blah2</title>
    <author>more blah2</author>
  </book>
</catalog>

The stylesheet that I have right now is something like this :-

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:orig="format_old"
  xmlns="format_new"/>

<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()" />
  </xsl:copy>
</xsl:template>

<xsl:template match="//orig:book">
  <xsl:element name="title">
    <xsl:value-of select="./orig:description/@title" />
  </xsl:element>
  <xsl:element name="author">
    <xsl:value-of select="./orig:writer/@name" />
  </xsl:element>
</xsl:template>

</xsl:stylesheet>

This gives me an output like :-

<catalog xmlns="format_old">
  <book xmlns="format_new">
    <title>blah</title>
    <author>more blah</author>
  </book>
  <book xmlns:orig="format_old" xmlns="format_new">
    <title>blah2</title>
    </author>more blah2</author>
  </book>
</catalog>

There are two problems with this stylesheet :-

1.) (major issue) The root element gets copied over rather than changing the default namespace of the root element. So basically the catalog element would still be in the namespace format_old.

2.) (minor issue) This will convert the elements as :-

<book xmlns:orig="format_old" xmlns="format_new">
  ...
</book>

instead of picking up the namespace from the root element as keeping it as

<book>
  ...
</book>

What am I missing here? I’m using Xalan-C.

  • 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-11T19:02:40+00:00Added an answer on June 11, 2026 at 7:02 pm

    I think the following should do:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0"
        xmlns="format_new"
        xmlns:ns1="format_old"
        exclude-result-prefixes="ns1"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    <xsl:template match="@* | text() | comment() | processing-instruction()">
      <xsl:copy/>
    </xsl:template>
    
    <xsl:template match="*">
      <xsl:element name="{local-name()}">
        <xsl:apply-templates select="@* | node()"/>
      </xsl:element>
    </xsl:template>
    
    <xsl:template match="ns1:book/ns1:description[@title]">
      <title>
        <xsl:value-of select="@title"/>
      </title>
    </xsl:template>
    
    <xsl:template match="ns1:book/ns1:writer[@name]">
      <author>
        <xsl:value-of select="@name"/>
      </author>
    </xsl:template>
    
    </xsl:stylesheet>
    

    Saxon 6.5.5 transforms your input into

    <?xml version="1.0" encoding="utf-8"?><catalog xmlns="format_new">
      <book>
        <title>blah</title>
        <author>more blah</author>
      </book>
      <book>
        <title>blah2</title>
        <author>more blah2</author>
      </book>
    </catalog>
    
    • 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 that looks like this. <foo> <bar type=artist/> Bob Marley
I have an XML document that matches our site navigation something like this: <page
I have an XML document that is pretty much structured like an Excel spreadsheet
I have an xml sitemap structured like a document tree, such that it looks
This is a tough one to google for. I have an XML document that's
I have an xml document that has a structure like so: <?xml version=1.0 encoding=iso-8859-1
I have an XML structure that looks like this: <root> <index> <item>item 1</item> <item>item
I have a piece of XML that is structured similar to this: <root> <score
I have an XML document with (basically) looks like this: ... <param> <key>age</key> <value>10</value>
I have an XML document that I'm trying to search through. The Main structure

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.