This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread 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 Android Security Essentials Live Lessons this week in the Android 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
Author

String to InputStream for XSL transformation

avaj eikunj
Greenhorn

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

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

OutputStream xfmString = new ByteArrayOutputStream();
System.out.println(transformXMLStr);
try{
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="http://www.w3.org/1999/XSL/Transform">
<xslutput method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<Organizations>
<xsl:for-each select="Organizations//Organization">
<xsl:sort order="ascending" data-type="number" select="Distance"/>
<Organization>
<OrgName><xsl:value-of select="OrgName"/></OrgName>
<OrgNbr><xsl:value-of select="OrgNbr"/></OrgNbr>
<Distance><xsl:value-of select="Distance"/></Distance>
</Organization>
</xsl:for-each>
</Organizations>
</xsl:template>
</xsl:stylesheet>

Here is the XML

<?xml version="1.0" encoding="UTF-8"?>
<Organizations>
<Organization>
<OrgNbr>109884</OrgNbr>
<OrgName>Los Angeles Gay and Lesbian Center</OrgName>
<Street1>745 N San Vicente Blvd West</Street1>
<Street2>NULL</Street2>
<City>West Hollywood</City>
<County>Los Angeles</County>
<StateName>California</StateName>
<ZipCode>90069</ZipCode>
<MainPhone>323-957-5280</MainPhone>
<TollFreePhone>NULL</TollFreePhone>
<CategoryAbbrev>CHYM, GONR, RPBL, STD, SYPH</CategoryAbbrev>
<Distance>5.860000</Distance>
</Organization>
<Organization>
<OrgNbr>10702</OrgNbr>
<OrgName>Lim-Keith Multispecialty Medical Clinic Incorporated</OrgName>
<Street1>6200 Wilshire Blvd Ste 1510</Street1>
<Street2>NULL</Street2>
<City>Los Angeles</City>
<County>Los Angeles</County>
<StateName>California</StateName>
<ZipCode>90048</ZipCode>
<MainPhone>323-964-1440</MainPhone>
<TollFreePhone>NULL</TollFreePhone>
<CategoryAbbrev>CHYM, CVBL, GONR, HERP, STD</CategoryAbbrev>
<Distance>2.270000</Distance>
</Organization>
<Organization>
<OrgNbr>111329</OrgNbr>
<OrgName>The Saban Free Clinic</OrgName>
<Street1>Seniel Ostrow</Street1>
<Street2>8405 Beverly Blvd</Street2>
<City>Los Angeles</City>
<County>Los Angeles</County>
<StateName>California</StateName>
<ZipCode>90048</ZipCode>
<MainPhone>323-653-1990</MainPhone>
<TollFreePhone>NULL</TollFreePhone>
<CategoryAbbrev>CHYM, GONR, HEPB, HPV, RPBL, STD, SYPH</CategoryAbbrev>
<Distance>4.270000</Distance>
</Organization>
<Organizations>

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
    
    1

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.
 
 
subject: String to InputStream for XSL transformation
 
Similar Threads
Java in XSLT
XPath/XSLT: Reverse axis really isn't?
what is exactly happening here?
HFSJ Question in JSTL
XML Transformation