Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 20th, 2007, 03:48 AM
Authorized User
 
Join Date: Jul 2007
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
Default Removing Namespace

hi,

I am trying to remove namespace xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/" from root element. My XML source looks something like below:

<chapter extra-info="Chapter 2" id="id_0073376965_001_000001" aid:pstyle="bch_nm" xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/"><char>Chapter 2</char>
<title id="uuu" aid:cstyle="OOO">
<para id="xx" aid:pstyle="yy">
<char>PARR</char>
PA ZZZ SSSS.</para>
</title>
</chapter>

Below is the stylesheet, wherein I am removing the added attributes "aid:pstyle"
 and "aid:cstyle" and an <char> from the source.

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/">

<xsl:preserve-space elements="*"/>
<xsl:template match="*|@*">
      <xsl:choose>
      <xsl:when test="name()='aid:pstyle'">
      </xsl:when>
      <xsl:when test="name()='aid:cstyle'">
      </xsl:when>
<xsl:when test="name()='char'">
      </xsl:when>
     <xsl:otherwise>
    <xsl:copy>
        <xsl:apply-templates select="text()|*|@*"/>
        </xsl:copy>
     </xsl:otherwise>
     </xsl:choose>
</xsl:template>
</xsl:stylesheet>

Everything is fine with above stylesheet, which removes all the undesired attributes and element from it, but I am unable to remove xmlns:aid namespace from the root <chapter> element.

I've tried Michael's suggestion using the below code, but I am sure I did something wrong and not getting the output [or infact I am not sure where to add the below snippet in my above stylesheet].

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

Can somebody give some advice on it.

Thanks for help.
Pankaj

Pankaj
__________________
Pankaj
 
Old September 20th, 2007, 08:51 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

xsl:copy copies an element together with all its namespaces. In 2.0 you can use copy-namespaces="no" to prevent this. In 1.0 you must use xsl:element:

<xsl:element name="{local-name()}" namespace="{namespace-uri()}">

The code as you've written it should work except that it also changes the elements to be in the null namespace, which isn't what you said you wanted.

To remove attributes, a better approach is rather than

<xsl:template match="*|@*">
      <xsl:choose>
      <xsl:when test="name()='aid:pstyle'">

override the identity template with

<xsl:template match="@aid:pstyle | @aid:cstyle/>

so when these attributes are matched, nothing is output.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old September 22nd, 2007, 03:23 AM
Authorized User
 
Join Date: Jul 2007
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks again Michael.

It does the trick.

Pankaj

Pankaj





Similar Threads
Thread Thread Starter Forum Replies Last Post
removing duplicates havey C# 9 February 26th, 2008 02:05 PM
Removing Comments in one go Pankaj C XSLT 3 September 9th, 2007 07:05 AM
removing variable boksi General .NET 0 June 2nd, 2005 03:49 PM
Removing Tables p_nut33 ADO.NET 1 February 18th, 2004 04:53 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.