*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: xmlns problem in xslt
 
Similar Threads
Selecting Node attribute in xslt
xmlns attribute
modify the value of an existing attribute in XSLT
XSLT problem when transfer the xml with schema definition
Java XML parser to parse the XML file