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 XSLT to copy an XML document Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "XSLT to copy an XML document" Watch "XSLT to copy an XML document" New topic
Author

XSLT to copy an XML document

John Davis
Ranch Hand

Joined: Nov 02, 2000
Posts: 189
I want XSLT to produce a result document that is a copy of the input XML document. When I try:

<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>

I appear to get a result document which contains only the XML values (the XML version, namespaces, and XML tags in the body are missing).

Perhaps I have a defective XSLT IDE - I'm using EDITiX

If I can get this to work, then I'd like to be able to transform XML documents with changes but the resultant document should be XML not just values.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

No, you are getting the correct output for that transformation. You are relying on the default processing rules for XSLT, which are basically that text nodes are copied to the output.

What you are looking for is called an "identity transformation"; it looks like this:

I would suggest you read a tutorial or a book about XSLT, rather than writing code based on guesses about how the language works. (Your other sin was to blame your IDE rather than assuming you were wrong. I always find that assuming it's my fault when things go wrong is a much more fruitful approach to solving problems.)
John Davis
Ranch Hand

Joined: Nov 02, 2000
Posts: 189
Paul,


Thanks for that information.

I went through 2 XSLT tutorials but neither contained the information you conveyed in your response. The tutorials focus on what most beginning XSLT users need, namely the ability to extract textual information from an XML source and format/display it in a browser. Consequently they usually show the generation of HTML tags and xsl:value-of text.

I didn't blame my XML IDE, I merely suggested that it might be at fault.
Since I am an XSLT newbie I naturally suspect that any problem I might encounter using XSLT would be rooted in my ignorance, but after some fruitless experience with ButterflyXML I don't rule out the possibility of XML IDE malfunction.

I greatly appreciate your response and value your help now and possibly in the future.

Thanks,

John Davis
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Well, I do have to admit, most of the XSLT "tutorials" I have seen on the web just rehash the XSLT elements and throw in a few trivial examples. Not very impressive in general. I learned XSLT by reading Michael Kay's XSLT book, which at least has some decent examples in it. Not to say there aren't other good XSLT books out there.
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
John Davis

Have you tried this FAQ? ;)

http://bobcat.webappcabaret.net/javachina/faq/xslt_01.htm

Search for "identity" text, you will find two versions of Identity templates useful for different purposes.

Thanks!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: XSLT to copy an XML document