I've got a strange problem with tomcat transforming an xml document with xsl apparently only on Linux. Below is a small portion of the style sheet that is relevant:

<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="grammar:rule">
<!--Make sure this is capital-->
<xsl:if test="string(parent::node()/@root) != string(./@id)">
<xsl:value-of select="translate(substring(@id, 1, 1), 'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/><xsl:value-of select="translate(substring(@id, 2), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')"/>
</xsl:if>
(
<xsl:apply-templates/>
)
</xsl:template>

<xsl:template match="grammar:one-of"> <xsl:variable name="weights">
<xsl:value-of select="sum(grammar:item/@weight)"/>
</xsl:variable>
[<xsl:for-each select="grammar:item"><xsl:choose>
<xsl:when test="@repeat='0-1'"><xsl:text> </xsl:text>?<xsl:apply-templates/><xsl:if test="string(@weight)!=''">~<xsl:value-of select="concat('0.', string(round((number(@weight) div number($weights))*10)))"/></xsl:if><xsl:text> </xsl:text></xsl:when>
<xsl:when test="@repeat='0+'">
<xsl:text> </xsl:text>*(<xsl:apply-templates/>)<xsl:if test="string(@weight)!=''">~<xsl:value-of select="concat('0.', string(round((number(@weight) div number($weights))*10)))"/></xsl:if><xsl:text> </xsl:text>
</xsl:when>
<xsl:when test="@repeat='1+'">
<xsl:text> </xsl:text>+(<xsl:apply-templates/>)<xsl:if test="string(@weight)!=''">~<xsl:value-of select="concat('0.', string(round((number(@weight) div number($weights))*10)))"/></xsl:if><xsl:text> </xsl:text>
</xsl:when>
<xsl:otherwise>(<xsl:apply-templates/>)<xsl:if test="string(@weight)!=''">~<xsl:value-of select="concat('0.', string(round((number(@weight) div number($weights))*10)))"/></xsl:if><xsl:text> </xsl:text></xsl:otherwise>
</xsl:choose></xsl:for-each>]
</xsl:template>

<xsl:template match="grammar:item">
<xsl:choose>
<xsl:when test="@repeat='0-1'"><xsl:text> </xsl:text>?<xsl:apply-templates/><xsl:if test="string(@repeat-prob)!=''">~<xsl:value-of select="@repeat-prob"/></xsl:if><xsl:text> </xsl:text></xsl:when>
<xsl:when test="@repeat='0+'">
<xsl:text> </xsl:text>*(<xsl:apply-templates/>)<xsl:if test="string(@repeat-prob)!=''">~<xsl:value-of select="@repeat-prob"/></xsl:if><xsl:text> </xsl:text>
</xsl:when>
<xsl:when test="@repeat='1+'">
<xsl:text> </xsl:text>+(<xsl:apply-templates/>)<xsl:if test="string(@repeat-prob)!=''">~<xsl:value-of select="@repeat-prob"/></xsl:if><xsl:text> </xsl:text>
</xsl:when>
<xsl:otherwise>(<xsl:apply-templates/>)<xsl:if test="string(@repeat-prob)!=''">~<xsl:value-of select="@repeat-prob"/></xsl:if><xsl:text> </xsl:text></xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template match="grammar:tag">
<xsl:choose>
<xsl:when test="starts-with(string(text()),'Append')"></xsl:when>
<xsl:otherwise>{<xsl:value-of select="'&lt;'" disable-output-escaping="yes"/><xsl:value-of select="substring-before(text(),'=')"/><xsl:text> "</xsl:text><xsl:value-of select="substring-before(substring-after(text(),'=&quot;'), '&quot;')"/><xsl:text>"</xsl:text><xsl:value-of select="'&gt;'" disable-output-escaping="yes"/>}</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template match="grammar:item/text()"><xsl:value-of select="normalize-space(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'))"/></xsl:template>
<xsl:template match="*|@*|text()" priority="-1">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template> </xsl:stylesheet>

Below is a subset of the xml I'm parsing:
<field name="browse_subject" expr="/'default/'">
<grammar version="1.0" xmlns="http://www.w3.org/2001/06/grammar"; root="subject_grammar" type="application/grammar+xml">
<rule id="subject_grammarOpening">
<one-of>
<item>
Do this for me
</item>
<item>
I want
</item>
<item>
Give me
</item>
<item>
I demand
</item>
<item>
I'd like
</item>
<item>
I would like
</item>
</one-of>
</rule>
<rule id="subject_grammarSelection" scope="public">
<one-of><item>default<tag>browse_subject="default"</tag></item></one-of>
</rule>
<rule id="subject_grammarClosing">
<one-of>
<item>
now
</item>
<item>
please
</item>
<item>
that's all
</item>
<item>
right now
</item>
</one-of>
</rule>
<rule id="subject_grammar" scope="public">
<item repeat="0-1">
<ruleref uri="#subject_grammarOpening"/>
</item>
<ruleref uri="#subject_grammarSelection"/>
<item repeat="0-1">
<ruleref uri="#subject_grammarClosing"/>
</item>
</rule>
<rule id="subject_grammarOpening">
<one-of>
<item>
Do this for me
</item>
<item>
I want
</item>
<item>
Give me
</item>
<item>
I demand
</item>
<item>
I'd like
</item>
<item>
I would like
</item>
</one-of>
</rule>
<rule id="subject_grammarSelection" scope="public">
<one-of><item>default<tag>browse_subject="default"</tag></item></one-of>
</rule>
<rule id="subject_grammarClosing">
<one-of>
<item>
now
</item>
<item>
please
</item>
<item>
that's all
</item>
<item>
right now
</item>
</one-of>
</rule>
</grammar>
</field>


I've repeated the rule id="subject_grammarClosing" to show off the problem tomcat is having with parsing this.

below is the correct transform that occurs from the commmand line with jdk 1.3.1+, from standalone applications with jdk 1.3.1+ and from the Windows version of tomcat 4.0.4 with jdk 1.4.1. I'm using xalan 2.1.0+(I've tried several) and xerces 2.2.1. This is the correct output:

<field name="browse_subject" expr="/'default/'">
<grammar type="application/x-gsl"><![CDATA[
Subject_grammaropening
(
[(do this for me) (i want) (give me) (i demand) (i'd like) (i would like) ]
)
Subject_grammarselection
(
[(default{<browse_subject "default">}) ]
)
Subject_grammarclosing
(
[(now) (please) (that's all) (right now) ]
)
(
?Subject_grammaropening
Subject_grammarselection
?Subject_grammarclosing
)
Subject_grammaropening
(
[(do this for me) (i want) (give me) (i demand) (i'd like) (i would like) ]
)
Subject_grammarselection
(
[(default{<browse_subject "default">}) ]
)
Subject_grammarclosing
(
[(now) (please) (that's all) (right now) ]
)
]]></grammar></field>

This is the problem result when running the transformation in tomcat under linux with jdk 1.3.1 and jdk1.4.1, both blackdown and sun, and Tomcat 3.3.1 to Tomcat 4.0.6.

<field name="browse_subject" expr="/'default/'">
<grammar type="application/x-gsl"><![CDATA[
Subject_grammaropening
(

[(do this for me) (i want) (give me) (i demand) (i'd like) (i would like) ]>}) ]

)
Subject_grammarclosing
(

[(now) (please) (that's all) (right now) ]

)
(
?Subject_grammaropening Subject_grammarselection ?Subject_grammarclosing
)
Subject_grammaropening
(

[(do this for me) (i want) (give me) (i demand) (i'd like) (i would like) ]

)
Subject_grammarselection
(

[(default{<browse_subject default>}) ]

)
Subject_grammarclosing
(

[(now) (please) (that's all) (right now) ]

)
]]></grammar></field>


As you can see these extra characters ">}) ]" are printed on Subject_grammarOpening line and the next element Subject_grammarSelection is ignored only for the first instance of the elements. The repeated. second set of elements work fine.

In a series of these field/grammar elements, there is a repeating pattern where the first element with one-of child elements will fail to see the closing tag and start into the next sibling element. This cannot be a bug in xerces or xalan, because the standalone apps work fine. I'm familar with the problems with making sure the correct parser is being read into the classpath in tomcat and can assure you I've replace each instance of the installed xerces with multiple different versions, renaming them aaaxerces just to make sure they're picked up first. This cannot be a bug in java linux because the standalone applications work fine, I've also done the transformation inside of netbeans and jbuilder. The only piece left is tomcat. The windows version of tomcat 4.0.4 works fine for both jdk 1.3.1 and jdk1.4. Only different tomcat versions on linux have this problem. Below is code from the test servlet I wrote.

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try
{
if(request.getParameter("inputType")!=null &&
request.getParameter("outputType")!=null &&
request.getParameter("withTemplate")!=null && request.getParameter("inputSource")!=null)
{
String inputUrl = request.getParameter("inputSource");
javax.xml.transform.Transformer transformer = null;
javax.xml.transform.Source inputSource = null;
if(request.getParameter("inputType").equals("stringReader"))
{

BufferedReader in = new BufferedReader(new FileReader(inputUrl));
StringBuffer sb = new StringBuffer();
String str;
while ((str = in.readLine()) != null) {
sb.append(str);
}
in.close();



inputSource = new javax.xml.transform.stream.StreamSource(new StringReader(sb.toString()));
}
else
{
inputSource = new javax.xml.transform.sax.SAXSource(new org.xml.sax.InputSource(new java.io.FileInputStream(inputUrl)));
}

if(request.getParameter("withTemplate").equals("testWithTemplate"))
{
javax.xml.transform.TransformerFactory tFactory =
javax.xml.transform.TransformerFactory.newInstance();
javax.xml.transform.Templates template = tFactory.newTemplates(new javax.xml.transform.sax.SAXSource(new org.xml.sax.InputSource(
new java.io.FileInputStream(request.getParameter("templateSource")))));
transformer = template.newTransformer();
}
else
{
// 1. Instantiate a TransformerFactory.
javax.xml.transform.TransformerFactory tFactory =
javax.xml.transform.TransformerFactory.newInstance();

// 2. Use the TransformerFactory to process the stylesheet Source and
// generate a Transformer.
transformer = tFactory.newTransformer(new javax.xml.transform.sax.SAXSource(new org.xml.sax.InputSource(new java.io.FileInputStream(request.getParameter("templateSource")))));

}


if(request.getParameter("outputType").equals("printWriter"))
{
response.setContentType("text/xml");
PrintWriter out = response.getWriter();
transformer.transform(inputSource, new javax.xml.transform.stream.StreamResult(out));
out.close();
}
else
{
transformer.transform(inputSource, new javax.xml.transform.stream.StreamResult(new java.io.FileWriter(request.getParameter("outputfile"))));
response.setContentType("text/html");
java.io.PrintWriter out = response.getWriter();
out.println("<html><head></head><body><a "+
"href=\"file://"+request.getParameter("outputfile")+"\">test "+
"results</a></body></html>");
}
}
else
{
response.setContentType("text/html");
java.io.PrintWriter out = response.getWriter();
out.println("<html><head></head><body><form action=\"test\">input "+
"type:<select name=\"inputType\"><option value=\"stringReader\">String "+
"Reader Test</option> "+
"<option value=\"fileReader\">File Reader Test</option> "+
"</select>&nbsp;&nbsp;with template?<select name=\"withTemplate\"><option "+
"value=\"testWithTemplate\">With Template</option><option "+
"value=\"testWithoutTemplate\">No Template "+
"Test</option></select>&nbsp;&nbsp;output type:<select "+
"name=\"outputType\"><option value=\"tfileWriter\">File Writer "+
"Test</option><option value=\"printWriter\">Print Writer "+
"Test</option></select>&nbsp;&nbsp;input source: <input type=\"text\" name=\"inputSource\" size=\"40\"/>&nbsp;&nbsp;xsl source<input type=\"text\" name=\"templateSource\" size=\"40\"/>&nbsp;&nbsp;optional(if using file writer)&nbsp;output file<input type=\"text\" name=\"outputfile\" size=\"40\"/><input type=\"submit\" name=\"submit\" "+
"value=\"submit\"/></form></body></html>");
}

}
catch(Throwable t)
{
t.printStackTrace();
}

}


Does anyone have any ideas what is going on inside of tomcat that would cause this kind of odd transformation? If anyone is willing to assist I can share the test webapp alone with a sample stylesheet and a sample xml document.

Thanks,
Chris Hazen
[EMAIL PROTECTED]
913-234-8075





--
To unsubscribe, e-mail: <mailto:tomcat-user-unsubscribe@;jakarta.apache.org>
For additional commands, e-mail: <mailto:tomcat-user-help@;jakarta.apache.org>

Reply via email to