dimanche 28 juin 2015

Is saxon the only framework that populates xsl transformation to a Hash Map

Hi I had to xsl transformation issue as explained in the Link , I got a wonderful solution from the community members which helped me.Saxon has OutputURIResolver.java to aid populate the transformer output into a java Map , i wonder how did they achieve it , is this the only library that can do it. Since Saxon has licensing which cannot be used by my product,i would not be able to use it.so looking for alternative.

The below transformation code is doing the job but the output string has all the schemas clubbed

 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
 DocumentBuilder builder = factory.newDocumentBuilder();
 document = builder.parse(datafile);

 // Use a Transformer for output
 TransformerFactory tFactory = TransformerFactory.newInstance();

 StreamSource stylesource = new StreamSource(stylesheet);
 Transformer transformer = tFactory.newTransformer(stylesource);

 DOMSource source = new DOMSource(document);
 StringWriter sw = new StringWriter();
 StreamResult result = new StreamResult(sw);
 transformer.transform(source, result);
 System.out.println(sw.toString());

The XSL is the same as you had provided , Just removed "xsl:result-document"

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://ift.tt/tCZ8VR" version="2.0">
    <xsl:template match="/">
        <xsl:for-each select="/*/*[local-name()='element']">
            <xsl:variable name="currentElement" select="."/>
                <xsl:for-each select="/*">
                    <xsl:copy>
                        <xsl:copy-of select="@*"/>
                         <xsl:copy-of select="node()[local-name()!='element']"/>
                        <xsl:copy-of select="$currentElement"/> <!-- Only content is current xs:element -->
                    </xsl:copy>
                </xsl:for-each>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

If the input XSD contains 2 root elements the output of transformation is <schema>first root element element</schema> <schema>Second root element element</schema> as one string .should i split the string to get individual schema.

Aucun commentaire:

Enregistrer un commentaire