aspose file tools*
The moose likes XML and Related Technologies and the fly likes XSLT losing element and attribute names 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 "XSLT losing element and attribute names" Watch "XSLT losing element and attribute names" New topic
Author

XSLT losing element and attribute names

Kelly Dolan
Ranch Hand

Joined: Jan 08, 2002
Posts: 109

Initially I tried doing a transform (Transformer.transform()) passing in a StreamSource. This did not work for me (nothing to do with the above) because my real XML has a DTD reference and the transform process fails when it tries to load the DTD. So, I switched to a SAXSource which allowed me to get a handle to a SAXParser and XMLReader and resolve my entities. USING A SAXSource GENERATED THE XML ABOVE AND LOST ALL OF MY ELEMENT AND ATTRIBUTE NAMES. I tried using a DOMSource. This worked generating valid XML.

My basic question is does anyone know why using the SAXSource lost my element and attribute names and what I can do to fix this? I really do not want to use the DOMSource because my real XML files are huge and I didn't want to have to read the entire document into memory.

Thanks!
Kelly
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18898
    
    8

Don't know. Here's a little program that I wrote to test that:It does an identity transformation and it works just fine. I didn't assign an EntityResolver to the XMLReader but I don't see why that would have any effect.

Were there any namespaces in the real XML that you had the problem with? I ask because, if you look at the API documentation for org.xml.sax.ContentHandler, specifically the startElement() method, you'll see a lot of information about when the local name and/or the qualified name are passed to that method. It just seemed to me that if the SAXSource was assuming it would get one of those parameters, but the parser wasn't actually passing that parameter, then the SAXSource wouldn't have anything for its element names.
Kelly Dolan
Ranch Hand

Joined: Jan 08, 2002
Posts: 109
I modifed my program to what you have above and scaled my XML down to simple XML (no DTD reference or ENTITYs) and the resulting XML was correct.

Then, I put everything back and it does not work.

To my XML, I added:



My program code now looks like:



Kelly
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
Works fine for me with Xalan-J 2.7 and JDK 1.4.2_11.
What is your CLASSPATH ?
I have not yet used 1.5

- m


Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
Kelly Dolan
Ranch Hand

Joined: Jan 08, 2002
Posts: 109
Actually I have JAVA_HOME set to my jre1.5.0_05 directory and that is it. I do not set anything JAVA specific on my CLASSPATH or PATH. In other words, if I enter "SET CLASSPATH" at a prompt, I only see the location of my application classes and if I enter "SET PATH" at a prompt, I only see what a Windows install would have put there.
Kelly Dolan
Ranch Hand

Joined: Jan 08, 2002
Posts: 109
Some good news. I determined that if I set namespaces aware to true on the SAX parser, my element and attribute names are generated. Yippee!!!

On the other hand, I noticed that my DOCTYPE and ENTITY references disappeared. So now I need to figure out how to retain that information in an XSLT transformation. Any ideas?

Kelly
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18898
    
    8

Apparently "How do I get XSLT to preserve entities?" is a FAQ, based on the hits I got from Google for "xslt preserve entities". I will let Bob DuCharme give you the whole story.
Kelly Dolan
Ranch Hand

Joined: Jan 08, 2002
Posts: 109
Thanks! I happen to run across his first article yesterday (referenced by the page at the link you provided above). Unfortunately, it is not exactly what I'm trying to do and therefore does not work for me but I put the blame on me for not communicating this well.



Technically, I don't care if the XSLT transformation replaces &elm2Text; because I don't lose information. However, I lose information regarding elm1Obj because it is an attribute of type ENTITY and its reference is stripped out. I'm thinking I'm going to have to have my XSLT replace object="elm1Obj" with object="my.jpg".

Kelly
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: XSLT losing element and attribute names