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 March 11th, 2009, 05:25 AM
Authorized User
 
Join Date: Mar 2009
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default Remove unwanted attribute

Dear all,

I am very new to xslt kidly help me to remove unwanted attribute from xml using xsl

here my example i dont want attribute for <job> element.


here my sample input xml
===============
<persons>
<person>
<name id="001">
<firstname>Paul</firstname>
<lastname>McCartney</lastname>
</name>
<job type="NIT">Singer</job>
<gender>Male</gender>
</person>
<person>
<name id="002">
<firstname>Paul</firstname>
<lastname>McCartney</lastname>
</name>
<job type="IT">SoftwareDeveloper</job>
<gender>Male</gender>
</person>
</persons>



output xml should be
==============

<persons>
<person>
<name id="001">
<firstname>Paul</firstname>
<lastname>McCartney</lastname>
</name>
<job>Singer</job>
<gender>Male</gender>
</person>
<person>
<name id="002">
<firstname>Paul</firstname>
<lastname>McCartney</lastname>
</name>
<job>SoftwareDeveloper</job>
<gender>Male</gender>
</person>
</persons>
 
Old March 11th, 2009, 05:35 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Search online for xslt identity template. Then add a template that matches the attribute and does nothing:
Code:
<xsl:template match="job/@type" />
__________________
Joe
http://joe.fawcett.name/
 
Old March 11th, 2009, 05:40 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Thumbs up

As Joe has told use the below stylesheet.

Code:
 
<xsl:stylesheet version="2.0" xmlns:xsl=http://www.w3.org/1999/XSL/Transform>
<xsl:output method="xml" indent="yes"/>

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

<xsl:template match="job/@type"/>
<xsl:stylesheet>

__________________
Rummy
 
Old March 11th, 2009, 09:19 AM
Authorized User
 
Join Date: Mar 2009
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default To aviid the inherited namespace

Dear Joe and rummy

Thanks for your solution.

Its working fine, what i was expected. Now when i using my xsl by the tag of <xsl:copy-of.. for the node person that would inherit the root element namespace but i don't want to inherit the root element namespace and when i implement to ignore the attribute that does not work.


Here is my sample input xml
===========================
<persons article-type="research-article" dtd-version="1.1" xml:lang="EN" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:mml="http://www.w3.org/1998/Math/MathML">
<person>
<name id="001">
<firstname>Paul</firstname>
<lastname>McCartney</lastname>
</name>
<job type="NIT">Singer</job>
<gender>Male</gender>
</person>
<person>
<name id="002">
<firstname>Paul</firstname>
<lastname>McCartney</lastname>
</name>
<job type="IT">SoftwareDeveloper</job>
<gender>Male</gender>
</person>
</persons>

output should be
===============
<persons article-type="research-article" dtd-version="1.1" xml:lang="EN" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:mml="http://www.w3.org/1998/Math/MathML">
<person>
<name id="001">
<firstname>Paul</firstname>
<lastname>McCartney</lastname>
</name>
<job>Singer</job>
<gender>Male</gender>
</person>
<person>
<name id="002">
<firstname>Paul</firstname>
<lastname>McCartney</lastname>
</name>
<job>SoftwareDeveloper</job>
<gender>Male</gender>
</person>
</persons>


Here my xsl
=======================================
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="persons">
<xsl:element name="persons">
<xsl:attribute name="article-type">research-article</xsl:attribute>
<xsl:attribute name="dtd-version">1.1</xsl:attribute>
<xsl:copy-of select="descendant::person"/>
</xsl:element>
</xsl:template>
<xsl:template match="persons/person/job/@type"/>
</xsl:stylesheet>




Since i am new to the xsl, i don't know where i am getting fail. Please do the needful.

Thanks,
Thava
 
Old March 11th, 2009, 10:36 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

As you didn't use the suggested code it's hard to know what to suggest.
As far as I can see the root element isn't namespaced.
__________________
Joe
http://joe.fawcett.name/
 
Old March 11th, 2009, 10:41 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Joe's suggestion is to use the following stylesheet:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="job/@type"/>

</xsl:stylesheet>
That produces exactly the output result you posted.

If that is not what you want to achieve then you need to show us the output you want. You mention some 'root element namespace' but in your sample input the root element is a 'persons' element in no namespace.

Please also mention whether you want to use XSLT 1.0 or 2.0.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old March 12th, 2009, 01:19 AM
Authorized User
 
Join Date: Mar 2009
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default xslt in indesignCS3

Dear all,

As told that the code which you given is working fine in normal browser, but when i import into Adobe IndesingCS3 it shows error message "DOM transformation error:Invalid namespace". Pls. advise.

Thanks,
 
Old March 12th, 2009, 02:38 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Thumbs up

When you dont use the prefixes, why do you use
xmlns:xlink
="http://www.w3.org/1999/xlink" xmlns:mml=http://www.w3.org/1998/Math/MathML. If possible try without those namespaces.
__________________
Rummy
 
Old March 12th, 2009, 05:45 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>When you dont use the prefixes, why do you use
xmlns:xlink
="http://www.w3.org/1999/xlink" xmlns:mml=http://www.w3.org/1998/Math/MathML. If possible try without those namespaces.

Rummy, I know you are trying to be helpful. But this is like suggesting to someone who's car won't start that it would help to remove the unnecessary newspapers sitting on the back seat.

Yes, simplifying the problem to its bare essentials can sometimes be useful, by eliminating all distractions and helping you to focus on the essence of the problem. But don't imagine that these redundant namespaces are anything to do with the problem.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old March 12th, 2009, 05:51 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Default

Thanks Mike. I thought that could help him in anyway. Is there any other solution?
__________________
Rummy





Similar Threads
Thread Thread Starter Forum Replies Last Post
Remove the unwanted space at end of the string gmbalaa General .NET 2 August 21st, 2007 11:35 PM
How to remove unwanted DS fields before WriteToXML majkinetor ADO.NET 4 December 8th, 2006 01:52 AM
How to filter out unwanted data fdtoo SQL Server 2000 1 April 25th, 2006 10:44 AM
Access to attribute values from class of attribute jacob C# 1 October 28th, 2005 01:11 PM
Unwanted space... again Snib HTML Code Clinic 1 August 25th, 2004 04:45 PM





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