Reputation: 45
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 " "><!ENTITY bull "•">]>
<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
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
output
method to "html"namee
does not exist in the XML inputcolspan
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 spannedStylesheet
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [<!ENTITY nbsp " "><!ENTITY bull "•">]>
<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