• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String to InputStream for XSL transformation

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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">
<xsl:output 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!!

:-)
 
security forum advocate
Posts: 236
1
Android Flex Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic