[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Generating XML fragment to DOM node using XSL

Subject: Re: Generating XML fragment to DOM node using XSL
From: Steve Dussinger <sdussing@xxxxxxxxx>
Date: Wed, 17 Dec 2003 09:26:19 -0800
using documentfragment
On 12/16/03 4:02 PM, "Josh Canfield" <josh.canfield@xxxxxxxxxxxx> wrote:

> I believe this does what you want, I've included the java source, the xml and
> xsl files... I used xalan-j_2.5.1. I found that passing an Element to the
> DOMResult constructor caused an error during the transform when there wasn't a
> single document node, using a DocumentFragment worked. This seems like a bug
> in xalan...
> 
> Hope this helps,
> Josh
> 
> ### test.java
> 
> import java.io.*;
> import javax.xml.transform.*;
> import javax.xml.transform.stream.*;
> import javax.xml.transform.dom.*;
> 
> import org.w3c.dom.*;
> import org.apache.xerces.dom.DocumentImpl;
> import org.apache.xml.serializer.*;
> 
> public class test {
> static public void main(String[] args) {
> try {
> // Create xsl source
> StreamSource xslSource = new StreamSource(new File(args[0]));
> // Create xml source
> StreamSource xmlSource = new StreamSource(new File(args[1]));
> 
> // Create transformer
> TransformerFactory tFactory = TransformerFactory.newInstance();
> /// output the xslt
> Transformer transformer = tFactory.newTransformer(xslSource);
> 
> // Create document
> // <ui><listbox><model/></listbox></ui>
> Document doc = new DocumentImpl();
> Element ui = doc.createElement("ui");
> Element listbox = doc.createElement("listbox");
> Element model = doc.createElement("model");
> doc.appendChild(ui);
> ui.appendChild(listbox);
> listbox.appendChild(model);
> 
> // Fragment to hold transformation
> DocumentFragment frag = doc.createDocumentFragment();
> 
> DOMResult dr = new DOMResult(frag);
> transformer.transform(xmlSource, dr);
> 
> // append transformed frag into model element
> model.appendChild(frag);
> 
> // Dump to System.out
> Transformer identity = tFactory.newTransformer();
> identity.setOutputProperty("indent","yes");
> identity.setOutputProperty("{http://xml.apache.org/xslt}indent-amount","4");
> identity.transform(new DOMSource(doc), new StreamResult(System.out));
> 
> } catch ( Exception e ) {
> e.printStackTrace();
> }
> }
> }
> 
> ### xml.xml
> 
> <?xml version="1.0" encoding="UTF-8"?>
> 
> <addrList>
> <address>
> <name>One</name>
> <name>Two</name>
> <name>Three</name>
> <name>Four</name>
> </address>
> </addrList>
> 
> ### xsl.xsl
> 
> <?xml version="1.0"?>
> 
> <xsl:stylesheet 
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
> version="1.0">
> <xsl:output method="xml" indent="no"/>
> <xsl:template match="/">
> <xsl:copy-of select="addrList/address/name"/>
> </xsl:template>
> </xsl:stylesheet>
> 
> ### System.out: 
> 
> <ui>
>   <listbox>
>       <model>
>           <name>One</name>
>           <name>Two</name>
>           <name>Three</name>
>           <name>Four</name>
>       </model>
>   </listbox>
> </ui>
> 
> 
> -----Original Message-----
> From: Steve Dussinger [mailto:sdussing@xxxxxxxxx]
> Sent: Tuesday, December 16, 2003 1:10 PM
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: Re:  Generating XML fragment to DOM node using XSL
> 
> 
> On 12/16/03 12:49 PM, "cknell@xxxxxxxxxx" <cknell@xxxxxxxxxx> wrote:
> 
>> Here are two XML documents and an XSLT stylesheet
>> 
>> sduss_a.xml
>> -------------
>> <?xml version="1.0" encoding="UTF-8" ?>
>> <menu>
>> <breakfast />
>> <lunch />
>> </menu>
>> 
>> 
>> sduss_b.xml
>> -------------
>> <?xml version="1.0" encoding="UTF-8" ?>
>> <dinner>
>> <salad />
>> <soup />
>> <main-dish />
>> <dessert />
>> </dinner>
>> 
>> sduss.xsl
>> -------------
>> <?xml version="1.0" encoding="UTF-8" ?>
>> <xsl:stylesheet version="1.0"
>> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
>> <xsl:output method="xml" indent="yes" encoding="UTF-8" />
>> <xsl:variable name="dinner" select="document('sduss_b.xml')" />
>> 
>> <xsl:template match="/menu">
>>   <menu>
>>     <xsl:copy-of select="*" />
>>     <xsl:copy-of select="$dinner/*" />
>>   </menu>
>> </xsl:template>
>> 
>> </xsl:stylesheet>
>> 
>> Now apply suss.xsl to sduss_a.xml and you will get:
>> 
>> <?xml version="1.0" encoding="UTF-8"?>
>> <menu>
>> <breakfast />
>> <lunch />
>> <dinner>
>> <salad />
>> <soup />
>> <main-dish />
>> <dessert />
>> </dinner>
>> </menu>
>> 
>> There it is, processed with XALAN-J. I'm still new to Java, so I won't be
>> able
>> to help you debug your program, but the problem is clearly not XSL and you
>> may
>> do better posting to the XALAN-J list to get pointers programming those
>> classes.
> 
> Unfortunately, this isn't doing what I need to do. This XSL creates a
> completely new DOM document with a 'menu' root. I need to put a series of
> nodes into an existing document at a specific node beneath the root.
> 
> I want the root node to be any currently existing element in my DOM tree. I
> can control the choice of which node is my parent in the destination tree,
> but if I try to put more than one node under that parent element, I get this
> error, even though I don't think the error is correct.
> 
> I understand that I can't create a DOM document with more than one root
> element, but that's not what I'm trying. I already have the DOM document,
> and I want to put a series of nodes under a child element of that DOM
> document...
> 
> Thanx,
>  Steve
> 
> 
> 
> XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
> 
> 
> XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
> 

Thank you. This is precisely what I was looking for. I had the same problem
you did when trying to use an Element as the DOMResult, that's what
originally precipitated my posting.

The part I was missing was creating the DocumentFragment. It seems so simple
in hindsight.

I'm still of the opinion that this is a bug in Xalan. I'll post something to
BugZilla over the weekend...

Thanx again everyone...

--Steve



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.