display xml with xslt and resx in the jsp file

This is a little test for displaying xml with xslt and rest in a jsp file.
A test jsp file.
<!DOCTYPE html>
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ page import="java.io.File"%>
<%@ page import="javax.xml.transform.stream.StreamSource"%>
<%@ page import="javax.xml.transform.stream.StreamResult"%>
<%@ page import="javax.xml.transform.TransformerFactory"%>
<%@ page import="javax.xml.transform.Transformer"%>
<%@ page import="org.w3c.dom.Document"%>
<%@ page import="org.w3c.dom.Element"%>
<%@ page import="javax.xml.parsers.DocumentBuilder"%>
<%@ page import="javax.xml.transform.dom.DOMSource"%>
<%@ page import="javax.xml.parsers.DocumentBuilderFactory"%>
<%
//Create a document in the memory.
        Document doc=null;
       
        String root = "Query";
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
            doc = docBuilder.newDocument();
            Element rootElement = doc.createElement(root);
            doc.appendChild(rootElement);

            Element Table_2 = doc.createElement("Table_2");
            Table_2.appendChild(doc.createTextNode("Table_2"));
            rootElement.appendChild(Table_2);
           
            Element row = doc.createElement("row");
            row.appendChild(doc.createTextNode("row"));
            Table_2.appendChild(row);
            Element test1 = doc.createElement("test1");
            test1.appendChild(doc.createTextNode("#value1"));
            row.appendChild(test1);
           
%>           
<%
           
DOMSource source1 = new DOMSource(doc);

String ls_path=request.getServletPath();
/*
out.println("request.getServletPath() : Returns the part of this request's URL that calls the servlet.<br>"+ls_path);
out.println("<br>request.getRequestURI() : Returns the part of this request's URL from the protocol name up to the query string in the first line of the HTTP request.<br>"+request.getRequestURI());
out.println("<br>request.getRequestURL() :  Reconstructs the URL the client used to make the request.<br>"+request.getRequestURL());
*/
String realPath=application.getRealPath(ls_path);
//out.println("<br>realPath: "+realPath  +"  File.separator : "+File.separator);
realPath=realPath.substring(0,realPath.lastIndexOf(File.separator));
//out.println("<br>realPath: "+realPath);

String ls_xml = realPath +File.separator+"data"+File.separator+"demo.xml";
String ls_xslt= realPath +File.separator+"data"+File.separator+"demo.xslt";
String resxArg = realPath +File.separator+"data"+File.separator+"demo.resx";

StreamSource source2= new StreamSource(new File(ls_xml));
StreamSource xslt = new StreamSource(new File(ls_xslt));

StreamResult result = new StreamResult(out);

TransformerFactory fF = TransformerFactory.newInstance();
Transformer tr = fF.newTransformer(xslt);
tr.setParameter("localeUrl", resxArg); //set a parameter in the xslt file

//from a xml file
//tr.transform(source2,result);

//from  a document object.
tr.transform(source1,result);

%>

 The demo.xslt

<?xml version="1.0" encoding="iso-8859-1" ?>
<!--a test-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
  <xsl:param name="localeUrl" select="'resource_en.xml'"/>
  <xsl:variable name="paraXml" select="document($localeUrl)/*" />
  <xsl:param name="Value1" select="$paraXml/data[@name='Value1']/value/text()"/>
<xsl:template match="/">
<h1><xsl:value-of select="$Value1" />: <xsl:value-of select="Query/Table_2/row/test1" /></h1>
</xsl:template>
</xsl:stylesheet>

The demo.resx
 <?xml version="1.0" encoding="utf-8"?>
<root>
  <data name="Value1" xml:space="preserve">
    <value>Value1xx</value>
  </data>
 </root>

The demo.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Query>
  <Table_2>Table_2<row>
      <test1>3A</test1>
    </row>
  </Table_2>
</Query>

评论

此博客中的热门博文

XML, XSL, HTML

Input in element.eleme.io

How do I load binary image data using Javascript and XMLHttpRequest?