This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes XML and Related Technologies and the fly likes String to InputStream for XSL transformation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "String to InputStream for XSL transformation" Watch "String to InputStream for XSL transformation" New topic

String to InputStream for XSL transformation

avaj eikunj

Joined: Mar 04, 2011
Posts: 4
Hello gurus,

I am seeing a strange behavior. Here is the code.

OutputStream xfmString = new ByteArrayOutputStream();
InputStream xmlStream = new ByteArrayInputStream(transformXMLStr.getBytes("UTF8"));
StreamSource xmlSource = new StreamSource(xmlStream); //"CDCResources/CDCData.xml"
StreamSource stylesource = new StreamSource("CDCResources/SortNGroupCDCData.xsl");

TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(stylesource);

StreamResult result = new StreamResult(System.out);
transformer.transform(xmlSource, result);

I am passing in a long XML in Java type String (transformXMLStr) on line 4. In Line 5 if I directly read an xml File CDCResources/CDCData.xml, the transformation works perfectly. But when the same data from the file is in the form of a java.util.String (transformXMLStr), It outputs nothing. I tried with UTF-8 and without it. Same response.

Here is the XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="">
<xslutput method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xsl:for-each select="Organizations//Organization">
<xsl:sort order="ascending" data-type="number" select="Distance"/>
<OrgName><xsl:value-of select="OrgName"/></OrgName>
<OrgNbr><xsl:value-of select="OrgNbr"/></OrgNbr>
<Distance><xsl:value-of select="Distance"/></Distance>

Here is the XML

<?xml version="1.0" encoding="UTF-8"?>
<OrgName>Los Angeles Gay and Lesbian Center</OrgName>
<Street1>745 N San Vicente Blvd West</Street1>
<City>West Hollywood</City>
<County>Los Angeles</County>
<CategoryAbbrev>CHYM, GONR, RPBL, STD, SYPH</CategoryAbbrev>
<OrgName>Lim-Keith Multispecialty Medical Clinic Incorporated</OrgName>
<Street1>6200 Wilshire Blvd Ste 1510</Street1>
<City>Los Angeles</City>
<County>Los Angeles</County>
<CategoryAbbrev>CHYM, CVBL, GONR, HERP, STD</CategoryAbbrev>
<OrgName>The Saban Free Clinic</OrgName>
<Street1>Seniel Ostrow</Street1>
<Street2>8405 Beverly Blvd</Street2>
<City>Los Angeles</City>
<County>Los Angeles</County>
<CategoryAbbrev>CHYM, GONR, HEPB, HPV, RPBL, STD, SYPH</CategoryAbbrev>

Two questions. What does this happen? Am I missing something in converting the String to InputStream?
<xsl:for-each select="Organizations//Organization[OrgName='The Saban Free*']"> is this the right way to select the organization with that name?

Really appreciate your help. Thank you!!

Sai Hegde
security forum advocate
Ranch Hand

Joined: Oct 26, 2010
Posts: 199

Please use code formatter for your code.

As for your 1st question, The input String needs to be converted to XML first to be able to apply XSLT to it.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: String to InputStream for XSL transformation