RezgHansa
RezgHansa

Reputation: 45

Multiple xml files transform using java to html file

My question is simple I have two xml files and one xsl file using java Transformer I needed to create html stringbuffer. I know how to deal with single xml file I need help to using two xmls.

My first xml

xml_1.xml(data xml)

<data>  
      <title>Mr</title>
      <name>Peter</name>
</data>

And my second xml is

xml_2.xml(property xml)

<prop>
       <lblname>Name</lblname>
       <lbltitle>Title</lbltitle>
</prop>

And my xsl is

dispaly.xsl

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE xsl:stylesheet [<!ENTITY nbsp "&#xa0;"><!ENTITY bull "&#x2022;">]>
 <xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
    <table width="100%" border="0" cellspacing="0" cellpadding="3" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;">
        <tr>
            <td colspan="2"><xsl:value-of select="prop/lbltitle" /></td>
            <td width="1"><xsl:value-of select="prop/lblname" /></td>
        </tr>
        <tr>
            <td colspan="2"><xsl:value-of select="data/title" /></td>
            <td width="1"><xsl:value-of select="data/namee" /></td>
        </tr>
      </table>
</xsl:template>

What I need is using java make transform this in to html file and here is the sample code I used for single xml file,

Java sample code

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

            DOMSource source = new DOMSource(document);

            Writer outWriter = new StringWriter();
            StreamResult result = new StreamResult(outWriter);
            transformer.transform(source, result);

What I need is using java make transform this in to html file and here is the sample code I used for single xml file,

Upvotes: 0

Views: 1637

Answers (1)

Mathias M&#252;ller
Mathias M&#252;ller

Reputation: 22647

Use the document() function to access multiple documents during the transformation. For convenience, the solution below stores the documents in variables because they are used more than once.

Note that

  • since you are obviously producing HTML, you should set the output method to "html"
  • there was a typo in your stylesheet, an element with the name namee does not exist in the XML input
  • the resulting table does not qualify as a complete HTML document. You should be aware of that if you intend to use it as such
  • the colspan attributes of some of your td elements do not make sense. It is of use only if, in another row, there are more table cells that can be spanned

Stylesheet

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE xsl:stylesheet [<!ENTITY nbsp "&#xa0;"><!ENTITY bull "&#x2022;">]>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

   <xsl:output method="html" indent="yes"/>

   <xsl:variable name="data" select="document('data.xml')"/>
   <xsl:variable name="property" select="document('property.xml')"/>

   <xsl:template match="/">
       <table width="100%" border="0" cellspacing="0" cellpadding="3" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;">
           <tr>
               <td colspan="2"><xsl:value-of select="$property/prop/lbltitle" /></td>
               <td width="1"><xsl:value-of select="$property/prop/lblname" /></td>
           </tr>
           <tr>
               <td colspan="2"><xsl:value-of select="$data/data/title" /></td>
               <td width="1"><xsl:value-of select="$data/data/name" /></td>
           </tr>
         </table>
   </xsl:template>

</xsl:stylesheet>

Output

<table width="100%" border="0" cellspacing="0" cellpadding="3" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;">
   <tr>
      <td colspan="2">Title</td>
      <td width="1">Name</td>
   </tr>
   <tr>
      <td colspan="2">Mr</td>
      <td width="1">Peter</td>
   </tr>
</table>

Upvotes: 1

Related Questions