Hi all,

I need to obtain a subtree from a SVG DOM document, apply a XSLT
transformation on it and replace the old subtree with the new one.
I have manage to get some results but not managed to transform the subtree
properly, here is what I am doing:

// I get the root node for the subtree (In this case a <g> element)
Element element=MyObject.getElementById(...);

//This method returns a subtree with the following structure
//xxx is a customized name space, and yes there is an svg document embbeded
<g xxx:name="yyy" transform="translate(x,y)">
<svg>
<g>
<text/>
<text/>
</g>
</svg>
</g>

Then I create a DOMSource object from the SVG element node (first Child node
of element);

*DOMSource source=new DOMSource(element.getFirstChild( ));*


and then the transformation:

TransformerFactory tFactory;
        Transformer transformer = null;

         //xsl style sheet
        StreamSource classXSL;


        //Instantiate a TransformerFactory
        tFactory=TransformerFactory.newInstance();
        //processing the stylesheet Source and generate a Transformer
        String uri=...;
        classXSL=new StreamSource(uri);
        try {
            //Creates a transformer object
            transformer=tFactory.newTransformer(classXSL);
        } catch (TransformerConfigurationException e) {

            e.printStackTrace();
        }
        //use transformer to transform an XML Source
        try {
           * transformer.transform(source,result);*   //Currently ,result is
a StreamResult object to simplify testing
            System.out.println("-->Document after transformation --");
            XMLprint.printXML(sw);

        } catch (TransformerException e) {
            System.out.println("Error in XML transformation");
            e.printStackTrace();
        }

    }

the test style sheet is simple as:


<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:xxx=""  xmlns:svg="http://www.w3.org/2000/svg";  version="1.0">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="/">

*<g>*
 *<xsl:apply-templates select="svg:text"/>*

*</g>*
    </xsl:template>
    <xsl:template match="svg:text">
        <xsl:copy>
            <xsl:copy-of select="*|@*"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>


After the transformation the result is:


<?xml version="1.0" encoding="UTF-8"?>

<text *xmlns="http://www.w3.org/2000/svg"; xmlns:xlink="
http://www.w3.org/1999/xlink"* x="5" y="1em" >
<tspan baseline-shift="15%">+ example : </tspan>
</text>
<text *xmlns="http://www.w3.org/2000/svg"; xmlns:xlink="
http://www.w3.org/1999/xlink"* x="5" y="2em" >
<tspan baseline-shift="15%">information</tspan>
</text>


Basically the result is not that bad , but the <g> </g> tags were not
printed and the svg and xlink name spaces were added (but I don't want them
in the result). I need to perform a more complex transformation but have not
managed to get this bit right. I am running the application in eclipse 3.3,
with batik 1.7, windows 2000, jdk 1.6.

Any advice is very welcome.

thanks.
Abraham

Reply via email to