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 5th, 2011, 03:25 AM
Authorized User
 
Join Date: Sep 2011
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Thumbs down How to get rid of not required namespace from result

Hi,
Can anyone suggest me the work around to get rid of redundant xmlns:xlink from result xml.
I declared xmlns:xlink as root stylesheet attribute but it is posing the problem for me in result xml, as the element which are not process specifically or newly created in stylesheet itself adds default namespace xmlns:xlink.

stylesheet:
Code:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/" xmlns:xlink="http://www.w3.org/1999/xlink">
input xml:
Code:
<volume>0</volume><issue>0</issue><fpage>506</fpage><lpage>513</lpage>
result xml:
Code:
<!--Below tags are not proessed specificly -->

<volume xmlns:xlink="http://www.w3.org/1999/xlink">0</volume>
         <issue xmlns:xlink="http://www.w3.org/1999/xlink">0</issue>
         <fpage xmlns:xlink="http://www.w3.org/1999/xlink">506</fpage>
         <lpage xmlns:xlink="http://www.w3.org/1999/xlink">513</lpage>
One way to solve this issue to process one by one all elements like below but it is pretty uncomfortable.

Code:
<xsl:template match="volume">
    <xsl:element name="volume">
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>
  <xsl:template match="issue">
    <xsl:element name="issue">
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>
  <xsl:template match="fpage">
    <xsl:element name="fpage">
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>
  <xsl:template match="lpage">
    <xsl:element name="lpage">
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>
So can any one let me know what I am doing wrong and a decent workaround?

Mohan
 
Old September 5th, 2011, 03:36 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Add exclude-result-prefixes="xlink" to the xsl:stylesheet element.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old September 5th, 2011, 04:49 AM
Authorized User
 
Join Date: Sep 2011
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to get rid of not required namespace from result

as per defination also your answer is absoultly correct but I don't know what is wrong with my result, still showing the same result(with redundant xlink attrib)

Code:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/" xmlns:xlink="http://www.w3.org/1999/xlink" exclude-result-prefixes="xlink">
I am using saxon 9.0 processor with windows xp

Mohan

Last edited by mmmathur06; September 5th, 2011 at 05:38 AM..
 
Old September 5th, 2011, 05:57 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You'll have to show more of your code. I can't see what you've done wrong unless you show me what you've done.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old September 5th, 2011, 08:01 AM
Authorized User
 
Join Date: Sep 2011
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Okay... caught it... since namespace is also given in input xml, stylesheet was not able to remove it.

Code:
<article article-type="research-article" dtd-version="2.2" xml:lang="en" xmlns:xlink="http://www.w3.org/1999/xlink">
now if i remove it from input xml am able to overcome from this issue.

but now another question since I need this namespace in output xml as an attribute of <article>, if I try to generate it something like below but it produce the error [Invalid attribute name: {xmlns:xlink}
Failed to compile stylesheet. 1 error detected. ]

Code:
<xsl:template match="article">
		<xsl:element name="article">
      <xsl:attribute name="article-type">research-article</xsl:attribute>
      <xsl:attribute name="dtd-version">2.2</xsl:attribute>
      <xsl:attribute name="xml:lang">en</xsl:attribute>
 <xsl:attribute name="xmlns:xlink">http://www.w3.org/1999/xlink</xsl:attribute>
<xsl:apply-templates/>
</xsl:element >
</xsl:template>
where I am sliping here?

Thanks for taking this issue
Mohan
 
Old September 5th, 2011, 08:19 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

with XSLT a namespace declaration is not considered an attribute. If you want to output a namespace declaration in XSLT 2.0 use xsl: namespace http://www.w3.org/TR/xslt20/#creating-namespace-nodes, not xsl: attribute.
But usually putting the namespace declaration on your xsl: stylesheet element is enough to have it in scope:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xlink="http://www.w3.org/1999/xlink"
  version="2.0">

...

</xsl:stylesheet>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old September 5th, 2011, 08:49 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You've got me very confused because you started by saying that you didn't want to declare the xlink namespace because it was unused, and now you're asking how to generate an attribute that's in the xlink namespace, so it clearly isn't unused.

Go back to first principles: show us your input document, your desired output, your attempt at an XSLT stylesheet, and explain how the output differs from what you want.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old September 6th, 2011, 01:09 AM
Authorized User
 
Join Date: Sep 2011
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry for getting you confuse here is my sample input and output fragment:

input xml:
Code:
<?xml version="1.0" encoding="UTF-16" standalone="yes"?>
<article article-type="research-article" dtd-version="2.2" xml:lang="en"<!-- xmlns:xlink="http://www.w3.org/1999/xlink"-->>
<journal-id>JO</journal-id>
<journal-title>The JO</journal-title>
<journal-subtitle>Journal of Ornithology</journal-subtitle>
<abbrev-journal-title>JO</abbrev-journal-title>
<issn>0234-8038</issn>
<issn>2938-4254</issn>
<email>[email protected]</email>
<alt-title>Covino and Holberton</alt-title>
<alt-title>Energy and Migratory Decisions</alt-title>
<volume>56</volume>
<issue>2</issue>
<fpage>506</fpage>
<lpage>513</lpage>
</article>
desierd output:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<article article-type="research-article" dtd-version="2.2" xml:lang="en" xmlns:xlink="http://www.w3.org/1999/xlink">
   <front>
      <journal-meta>
         <journal-id journal-id-type="publisher-id">JO</journal-id>
         <journal-title>The JO</journal-title>
         <issn pub-type="ppub">0234-8038</issn>
         <issn pub-type="epub">2938-4254</issn>
	 <email xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="[email protected]">[email protected]</email>
      </journal-meta>
      <article-meta> 
         <volume>56</volume>
         <issue>2</issue>
         <fpage>506</fpage>
         <lpage>513</lpage></article-meta></front>
	</article>
As could see I need xmlns:xlink in some of elements as attrib but not in all the elements as it was in preceding post. Defining xmlns:xlink at stylesheet level add this namespace to all unprocess tags which is what I asked about and you suggested in one of your post resolve my issue by using exclude-result-prefixes="xlink", but now as this namespace required in some specific element how could I add this in those specific element?


Thanks
Mohan
 
Old September 6th, 2011, 04:11 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

exclude-result-prefixes should work here. It will exclude the namespace from elements where it is not needed, and cause it to be declared at the level where it is actually used.

(Your example has a declaration of xmlns:xlink at the top level of your "desired" output - I'm assuming that was a mistake.)
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old September 6th, 2011, 06:02 AM
Authorized User
 
Join Date: Sep 2011
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

No, It was not a mistake my whole theread is all about this since namespace is required in only and only <article> and <ext-link> elements. so I have to declare it at top level but not in others so I need to prevent it to add in other elements also.

Using exclude-result-prefix solve one issue but also adds another issue: [how to add namespace in <article> or <ext-link> ?]

Thanks
Mohan





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to copy namespace delcarations to result mphare XSLT 5 April 22nd, 2010 02:12 PM
error CS0234: The type or namespace name 'Xml' does not exist in the namespace 'Syste shailesh_kumar C# 2008 aka C# 3.0 8 August 20th, 2009 03:11 AM
Need to convert an existing namespace and add new namespace to the SOAP xml Prabeen XSLT 10 April 28th, 2009 10:18 AM
GridView and SQL More than one result = one result DarkForce ASP.NET 2.0 Basics 0 July 20th, 2007 04:29 AM
How do I get rid of this comma?!?!?!?! tsindos Classic ASP Databases 10 February 16th, 2006 12:55 AM





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