File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes xmlns problem in xslt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "xmlns problem in xslt" Watch "xmlns problem in xslt" New topic
Author

xmlns problem in xslt

Roy Huang
Ranch Hand

Joined: Nov 21, 2001
Posts: 104
I wrote a xslt to transform one xml format A to another xml format B.
During the process, I found if in the source XML A,
any element Tag with "xmlns" attribute like
<sdc xmlns="http://www.sdc.com"> then I can not fetch the subelemnt value
from sdc Node via XSLT.
if I delete the xmlns attribute from sdc node, all works fine.
Should I change something in my XSLT about the xmlns information?
Thanks,
Roy


SCJP 1.4/SCBCD/SCWCD<br />MCSE<br />IBM professional XML Developer<br />SAP certified Development Consultant (NetWeaver portal and Web JavaApplication)
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
The xmlns="foobar" piece, a default namespace definition, in your XML document tells the parser that the element "sdc" is actually "foobar:sdc".
In other words, your XSL needs to do

instead of

[ January 14, 2004: Message edited by: Lasse Koskela ]

Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Roy Huang
Ranch Hand

Joined: Nov 21, 2001
Posts: 104
Thanks, Lasse Koskela
but if I can not directly use the template, waht should I do?
In my case, the source xml file is like:
....
<header>
<sdc xmlns="http://www.abc.com">
<gilt>
<devel>12567</devel>
</gilt>
</sdc>
</header>
...

How can I modify the XSLT with the xmlns?
Like:
....
<develLc>
<xsl:value-of select="header/sdc/gilt/devel"/>
</develLc>
...
to fecth the value 12567?

Roy
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
1) Add this attribute to your stylesheet's root element:

2) Insert the namespace prefix into your XPath expression:
Roy Huang
Ranch Hand

Joined: Nov 21, 2001
Posts: 104
Hi, Lasse,
I have tried the exact same procedure before I
asked you agin with the last reply. But it always gets a
Java IO Null Point Exception, the value 12567 is not fetched.
I use XALAN XSLT, but if I delete the xmlns in Source XML, everything goes fine.
Any idea?
Roy
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Could you post the exact stack trace of that exception?
Oh, one more thing. You may need to add the namespace prefix into the child nodes as well:

[ January 15, 2004: Message edited by: Lasse Koskela ]
Roy Huang
Ranch Hand

Joined: Nov 21, 2001
Posts: 104
Hi, Lasse,
I just tried what you said -> add the xmlns also to the child node, but
the excpetion is still like:
IOException occur: java.lang.NullPointerException
Nothing changed..:-(
Roy
Roy Huang
Ranch Hand

Joined: Nov 21, 2001
Posts: 104
Hi, Lasse,
Sorry, it works, I have made a typing error...
Thanks very much,
Have a nice day,
Roy
Roy Huang
Ranch Hand

Joined: Nov 21, 2001
Posts: 104
Lasse, I have one more question,
In the first case your describe, suppose I have the XSLT like:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="www.abc.com/xyz">
...
<xsl:template match="my:sdc"/>
<xlk version="1.1">
...
</xsl:stylesheet>

actually I want to get the target file like:
<xlk version="1.1">
...
</xlk>

but actually I always get
<xlk xmlns:my="www.abc.com/xyz" version="1.1">
...
<xlk>

U know I don't need the xmlns from XSLT staying in my target xml file,
How can I remove it, what should I set in the XSLT?

Thanks,
Roy
Roy Huang
Ranch Hand

Joined: Nov 21, 2001
Posts: 104
Find the solution ,
have to write:
<xsl:stylesheet exclude-result-prefixes="my" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="www.sap.com/slim" version="1.0" >

Roy
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: xmlns problem in xslt