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 February 14th, 2008, 03:20 PM
Registered User
 
Join Date: Jan 2008
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default namespace removal

Hi All,

My friend Deepa is got stuck in a XSLT transform. She is using Xalan processor.

The input file contains some namespaces which should be removed in desired output file.

Below is the input file-
<ConfigDataFile xmlns="WiMAX-CAPC" xmlns:HHP="HHP" xmlns:CLM="CLM"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="WiMAX-CAPC ./updatedSchema/WiMAX-CAPC_NECB.xsd">
   <configData dnPrefix="String">
        <CLM:CLM>
<CLM:clAgentClientTypeDefinitionTable>
   <CLM:clAgentClientTypeDefinitionEntry modemId="1" cageId="1">
   <CLM:clAgentClientType>100</CLM:clAgentClientType>
<CLM:clAgentClientTypeMax>100</CLM:clAgentClientTypeMax>

   </CLM:clAgentClientTypeDefinitionEntry>
<CLM:clAgentClientTypeMaxCountONE >test</CLM:clAgentClientTypeMaxCountONE>
<CLM:clAgentClientTypeDefinitionEntry modemId="1" cageId="1">
<CLM:clAgentClientType>100</CLM:clAgentClientType>
<CLM:clAgentClientTypeMax>200</CLM:clAgentClientTypeMax>
</CLM:clAgentClientTypeDefinitionEntry>
<CLM:clAgentClientTypeDefinitionEntry modemId="1" cageId="1">
<CLM:clAgentClientType>000</CLM:clAgentClientType>
<CLM:clAgentClientTypeMax>100</CLM:clAgentClientTypeMax>
</CLM:clAgentClientTypeDefinitionEntry>
</CLM:clAgentClientTypeDefinitionTable>
</CLM:CLM>
</configData>
</ConfigDataFile>


The XSLT(1.0) code, she is using -
<xsl:template match=”/”>
<xsl:for-each select=”descendant::*>
<xsl:if test=”name(ancestor::*[1])= ‘CLM:clAgentClientTypeDefinitionTable’ | name(ancestor::*[2])= CLM:clAgentClientTypeDefinitionTable’”>
<xsl:element name=”{name(.)}>
<xsl:copy-of select=”@*”/>
</xsl:element>
</xsl:for-each>
</xsl:template>


The output she is getting-
 <CLM:clAgentClientTypeDefinitionEntry xmlns:CLM="CLM" modemId="1" cageId="1">
<CLM:clAgentClientType>0hshj</CLM:clAgentClientType>
<CLM:clAgentClientTypeMax>100</CLM:clAgentClientTypeMax>
</CLM:clAgentClientTypeDefinitionEntry>
<CLM:clAgentClientTypeMaxCountONE xmlns:CLM="CLM">test</CLM:clAgentClientTypeMaxCountONE>
<CLM:clAgentClientTypeDefinitionEntry xmlns:CLM="CLM" modemId="1" cageId="1">
<CLM:clAgentClientType>100</CLM:clAgentClientType>
<CLM:clAgentClientTypeMax>200</CLM:clAgentClientTypeMax>
</CLM:clAgentClientTypeDefinitionEntry>
<CLM:clAgentClientTypeDefinitionEntry xmlns:CLM="CLM" modemId="1" cageId="1">
<CLM:clAgentClientType>000</CLM:clAgentClientType>
<CLM:clAgentClientTypeMax>100</CLM:clAgentClientTypeMax>
</CLM:clAgentClientTypeDefinitionEntry>

but she doesn't want the red color entries which are coming after transformation.(Means xmlns:CLM=”CLM” should be removed)

The desired output is like-
<CLM:clAgentClientTypeDefinitionEntry modemId="1" cageId="1">
<CLM:clAgentClientType>0hshj</CLM:clAgentClientType>
<CLM:clAgentClientTypeMax>100</CLM:clAgentClientTypeMax>
</CLM:clAgentClientTypeDefinitionEntry>
<CLM:clAgentClientTypeMaxCountONE >test</CLM:clAgentClientTypeMaxCountONE>
<CLM:clAgentClientTypeDefinitionEntry modemId="1" cageId="1">
<CLM:clAgentClientType>100</CLM:clAgentClientType>
<CLM:clAgentClientTypeMax>200</CLM:clAgentClientTypeMax>
</CLM:clAgentClientTypeDefinitionEntry>
<CLM:clAgentClientTypeDefinitionEntry modemId="1" cageId="1">
<CLM:clAgentClientType>000</CLM:clAgentClientType>
<CLM:clAgentClientTypeMax>100</CLM:clAgentClientTypeMax>
</CLM:clAgentClientTypeDefinitionEntry>


Is it possible to get the desired output using XSLT 1.0 or is there any way to do it in XSLT 2.0(using saxon or any other processor which supports XSLT 2.0).

Thanks

Manish
 
Old February 14th, 2008, 06:33 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You can do this with a single template rule that does a recursive walk of the input tree:

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

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old February 15th, 2008, 04:54 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

If your desired output contains the "CLM" prefix then that namespace prefix will have to be declared somewhere.

As your desired output is an XML fragment and not a full XML document (i.e. it has no single root element) then the namespace declaration must appear more than once.

Michael's example above will remove the namespace prefix "CLM" and hence will no longer require the namespace declaration - this might be what you are after.

/- Sam Judson : Wrox Technical Editor -/
 
Old February 15th, 2008, 05:17 AM
Registered User
 
Join Date: Jan 2008
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Michael for you input.

She is saying after using code you wrote, she is getting the output without (CLM:) that is like-

<clAgentClientTypeDefinitionEntry modemId="1" cageId="1">
<clAgentClientType>0hshj</clAgentClientType>
<clAgentClientTypeMax>100</clAgentClientTypeMax>
</clAgentClientTypeDefinitionEntry>

but she wants the output to come like-

 <CLM:clAgentClientTypeDefinitionEntry modemId="1" cageId="1">
<CLM:clAgentClientType>0hshj</CLM:clAgentClientType>
<CLM:clAgentClientTypeMax>100</CLM:clAgentClientTypeMax>
</CLM:clAgentClientTypeDefinitionEntry>


Here is the copy of XSLT code which she has sent me-

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:xalan="http://xml.apache.org/xalan" xmlns:CLM="CLM" xmlns:HHP="HHP">
    <xsl:output indent="yes"/>
    <xsl:template match="/">
       <xsl:for-each select="descendant::*">
        <xsl:if test="name(ancestor::*[1])='CLM:clAgentClientTypeDefinitionTable' ">
            <xsl:element name="{name(.)}">
            <xsl:copy-of select="@*"/>
                </xsl:element>
                </xsl:if>
                </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

Do you want any modification in above code to get desired output?
 
Old February 15th, 2008, 05:26 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

See my previous comment - you can't remove the namespace declaration if you want to keep the prefix. It would become invalid XML.

/- Sam Judson : Wrox Technical Editor -/
 
Old February 15th, 2008, 05:40 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>but she wants the output to come like-

 <CLM:clAgentClientTypeDefinitionEntry modemId="1" cageId="1">

That output would not be valid XML according to the XML Namespaces specification, and XSLT does not allow you to create invalid XML.



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Automatic history removal ermy78 SQL Server 2005 0 February 18th, 2008 02:41 PM
Removal of modules slgknjn Excel VBA 0 September 24th, 2004 12:35 AM
Close button removal canuck38 Access 4 July 16th, 2004 03:50 AM
Chapt 6 Removal from an ArrayList Jerry Obrien BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 0 February 19th, 2004 02:40 PM





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