读书人

xml高手们小弟我有一段代码帮小弟我

发布时间: 2012-02-27 10:00:22 作者: rapoo

xml高手们,我有一段代码帮我看看错在哪里!!!~~~先谢谢了!~~~
aaa.xml
<?xml version= "1.0 " encoding= "UTF-8 "?>
<?xml-stylesheet type= "text/xsl " href= "DeviceGroup.xsl "?>
<localizationpack>
<greetiongs>
<msg1> 你好 </msg1>
<msg1> 早上好 </msg1>
<msg1> 晚安 </msg1>
</greetiongs>
</localizationpack>

bbb.xml
<?xml version= "1.0 " encoding= "UTF-8 "?>
<?xml-stylesheet type= "text/xsl " href= "DeviceGroup.xsl "?>
<localizationpack>
<greetiongs>
<msg1> hello </msg1>
<msg1> good morning </msg1>
<msg1> good night </msg1>
</greetiongs>
</localizationpack>

ccc.xsl
<?xml version= "1.0 " encoding= "UTF-8 "?>
<xsl:stylesheet xmlns:xsl= "http://www.w3.org/1999/XSL/Transform " version= "1.0 ">
<xsl:param name= "lpfile "/>
<xsl:template match= "/ ">
<html>
<body bgcolor= "#6a6dee " text= "#000000 ">
<p>
<xsl:value-of select= "docment($lpfile)//greetings/Msg1 "/>
</p>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
当中我有了JAVA写了一个Servlet,代码如下:
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.xalan.xpath.XObject;
import org.apache.xalan.xslt.XSLTInputSource;
import org.apache.xalan.xslt.XSLTProcessor;
import org.apache.xalan.xslt.XSLTProcessorFactory;
import org.apache.xalan.xslt.XSLTResultTarget;
import org.xml.sax.SAXException;

public class TestLanguage extends HttpServlet {

public TestLanguage() {
super();
}
public void destroy() {
super.destroy();
}
public void doGet(HttpServletRequest request HttpServletResponse response)throws ServletException, IOException,
java.net.MalformedURLException {
try {
response.setContentType( "text/html; charset=UTF-8 ");
PrintWriter out = response.getWriter();
Locale clocale = request.getLocale();//获得用户本机当前语言
StringWriter resultStringWriter = new StringWriter();
XSLTProcessor processor = XSLTProcessorFactory.getProcessor();
processor.setStylesheetParam( "lpfile ", "clocale ");//把获得用户本机语言的值传参给lpfile,这句也是把lpfile得到的值传递给ccc.xsl,用 <xsl:value-of select= "docment($lpfile)//greetings/Msg1 "/> 进行判断,然后输出想要的内容



String xml_doc = "E:/zheng XML-3.15/ddd.xml ";
String stylesheet = "E:/zheng XML-3.15/ccc.xsl ";

processor.process(new XSLTInputSource(xml_doc),
new XSLTInputSource(stylesheet),
new XSLTResultTarget(resultStringWriter));
out.write(resultStringWriter.toString());
out.close();
} catch (SAXException e) {
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {}

public void init() throws ServletException {}
}
当中的ddd.xml也只是一些数据同aaa.xml中的数据一样,我想在输出的时候进行判断,当用户机的本地语言为中文的时候,就输出aaa.xml中的节点,当用户本的本地语言英语时,就输出bbb.xml中的节点,但是在输出后页面什么内容也没有,点击查看原文件的时候里面只是html的代码,根本没有输出我想要的内容,这是怎么回事,还是我在XSL中的代码写错了??请高手们指点一二,不胜感激!~~~



[解决办法]
这个,xml文件和xsl文件都没错, "docment($lpfile)//greetings/Msg1 "那个是document是楼主不小心输入错了吧,java代码不太懂,楼主看看程序运行时参数传递是否正确,还有因为文件中有中文,可以用gb2312编码试验下看是否是编码问题.代码不多,问题该好找.
[解决办法]
$lpfile换成 "bbb.xml "试试,如果可以的话,看看$lpfile的值少引号吗?
<xsl:variable name= "file " select= " 'c.xml ' " />
[解决办法]
如果你怀疑XSL的话,那先在XMLSPY中,检查XSL。

如果没问题,再仔细检查你的SERVLET代码。

读书人网 >XML SOAP

热点推荐