JSP combined XML XSLT output

xiaoxiao2021-03-19  210

We know that XML XSLT can output directly to the XML browser, such as IE 5.0 or more, but we have to consider that there are many browsers to support XML, in which case we need to be on the server. Convert to HTML output to the browser, this temporary transition method is probably to use within a period of time.

With JSP plus the TabLib ID library, we can complete this conversion.

The famous Open Source Project Group Jakarta.apache.org has this feature TANGLIB: http://jakarta.apache.org/taglibs/doc/xsl-doc/intro.html

According to the Jakarta configuration method, it is a bit cumbersome, you need to modify or define web.xml. I have been exploited, using the following quite simple approach, you can make JSP to run the XSL this identity library.

XSL ID libraries have three key packages:

Xerces.jar can get in http://xml.apache.org/

Xalan.jar can get in http://xml.apache.org/

Xsl.jar gets from http://jakarta.apache.org/taglibs/doc/xsl-doc/intro.html

1. Place these three packages in the Tomcat's CommON / lib directory, or put them directly into the ClassPath environment.

2. Call the logos library in JSP:

The original JAKARTA recommended method is:

<% @ Taglib Uri = "http://jakarta.apache.org/taglibs/xsl-1.0" prefix = "xsl"%>

This will need to define http://jakarta.apache.org/taglibs/xsl 1.0 points under /Web-inf/web.xml. Such as:

http://jakarta.apache.org/taglibs/xsl-1.0

/web-inf/xsl.tld

Although this approach is very standard, if your container has been using Tomcat, it will not have to.

Our approach is:

<% @ Taglib Uri = "xsl.jar" prefix = "xsl"%>

As an example with Apply.jsp comes with Jakarta's XSL Taglib, let's take a look at the relationship between JSP XML XSLT:

Apply.jsp

<% @ Taglib Uri = "xsl.jar" prefix = "xsl"%>

Employee List </ Title></p> <p></ hEAD></p> <p><body bgcolor = "white"></p> <p><p> The following is shown in the four combinations of XML XSLT of JSP:</p> <p><p> Using the Apply method below, combine the already existing Employees.xml and Employeelist.xsl</p> <p><XSL: Apply XML = "/ XML / Employees.xml" XSL = "/ XML / EMPLOYELIST.XSL" /></p> <p><hr></p> <p><p> The following is to write XML data directly in your own Employeelist.xsl and then yourself in JSP. <XSL: Apply XSL = "/ XML / Employeelist.xsl></p> <p><? Xml Version = "1.0" encoding = "ISO-8859-1"?></p> <p><Employees></p> <p><Employee ID = "123"></p> <p><first-name> john </ first-name></p> <p><Last-Name> Doe </ last-name></p> <p><Telephone> 800-555-1212 </ telephone></p> <p></ Employee></p> <p><Employee ID = "456"></p> <p><first-name> jane </ first-name></p> <p><Last-Name> Smith </ last-name></p> <p><Televhone> 888-555-1212 </ telephone></p> <p></ Employee></p> <p><Employee ID = "789"></p> <p><first-name> George </ first-name></p> <p><last-name> Taylor </ last-name></p> <p><Televhone> 555-555-1212 </ telephone></p> <p></ Employee></p> <p></ Employees></p> <p></ xsl: apply></p> <p><hr></p> <p><p> The following is the way to use include, such an XSLT style can accommodate different XML files.</p> <p><XSL: Apply XSL = "/ XML / EMPLOYELIST.XSL"></p> <p><xsl: include page = "/ xml / Employees.xml" /></p> <p></ xsl: apply></p> <p><hr></p> <p><p> The following is the use of the Import method, imported XML file </ p> in page-scope (similar to scope = "page")</p> <p><xsl: import id = "data" Page = "/ xml / employees.xml" /></p> <p><xsl: apply namexml = "data" xsl = "/ xml / employeelist.xsl" /></p> <p></ body></p> <p>In the above program, four JSP combined XML XSLT methods are shown, which can basically meet our needs. Note The above XML file path is "/ xml /", which is an absolute path to the Tomcat container.</p> <p>Let's take a look at Employeelist.xsl and Employees.xml content:</p> <p>EmployeeList.xsl is similar to CSS in HTML, mainly to define data display mode in XML:</p> <p><? XML Version = "1.0"?></p> <p><XSL: Stylesheet Version = "1.0" XMLns: XSL = "http://www.w3.org/1999/xsl/transform"></p> <p><XSL: Template Match = "EMPLOYEES"></p> <p><table border = "1" width = "100%"> <TR></p> <p><TH> ID </ th></p> <p><TH> Employee Name </ TH></p> <p><TH> Phone Number </ TH></p> <p></ TR></p> <p><xsl: for-each select = "employee"></p> <p><tr></p> <p><TD></p> <p><xsl: value-of select = "@ id" /></p> <p></ td></p> <p><TD></p> <p><xsl: value-of select = "last-name" />,</p> <p><xsl: value-of select = "first-name" /></p> <p></ td></p> <p><TD></p> <p><xsl: value-of select = "telephone" /></p> <p></ td></p> <p></ TR></p> <p></ xsl: for-energy></p> <p></ TABLE></p> <p></ xsl: template></p> <p></ xsl: stylesheet></p> <p>EMPLOYEES.XML</p> <p><? Xml Version = "1.0" encoding = "ISO-8859-1"?></p> <p><Employees></p> <p><Employee ID = "123"></p> <p><first-name> john </ first-name></p> <p><Last-Name> Doe </ last-name></p> <p><Telephone> 800-555-1212 </ telephone></p> <p></ Employee></p> <p><Employee ID = "456"></p> <p><first-name> jane </ first-name></p> <p><Last-Name> Smith </ last-name></p> <p><Televhone> 888-555-1212 </ telephone></p> <p></ Employee></p> <p><Employee ID = "789"></p> <p><first-name> George </ first-name></p> <p><last-name> Taylor </ last-name></p> <p><Televhone> 555-555-1212 </ telephone></p> <p></ Employee></p> <p></ Employees></p> <p>If we join at the top of Employees.xml:</p> <p><? xml: stylesheet type = "text / xsl" href = "catalog.xsl"?></p> <p>Use the browser that supports XML IE 5.0 or above, the display page is the same as the Apply.jsp display page.</p> <p>This XML file we can use JDM to make editors and queries, see me another article.</p> <p>At this point, we basically complete the use of various basic functions of XML in Java: Edit usage display. In practical applications, it is enough to deal with it!</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-130205.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="130205" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.048</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'lDn_2FJIfVC8mXTbbDqTz2VQoLr6MyCEQu3LysW_2FhgLd_2BSuqbF729_2FAas84S3r5KHk5jNHWTbjTQ424rdq_2BNjfsg_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>