A problem with template matching rules about XSLT

zhaozj2021-02-16  48

A problem with template matching rules about XSLT

Problem: http://expert.9cbs.net/expert/topic/2549/2549396.xml?temp =.5212061

Problem Description:

************** Source file: Test.xml ***************

Extreme programming research </ Title></p> <p><Price> 70 </ price></p> <p><author> giancarrio succi / michele marchesi / Zhang Hui (translation) </ author></p> <p></ book></p> <p></ books></p> <p>*************** Conversion file: Test.xsl **************</p> <p><XSL: Stylesheet XMLns: XSL = "http://www.w3.org/1999/xsl/transform" Version = "1.0"></p> <p><XSL: Output method = "html" /></p> <p><XSL: Template Match = "/ Books / Book / PRICE"></p> <p>DD</p> <p></ xsl: template></p> <p></ xsl: stylesheet></p> <p>Output:</p> <p>Extreme Programming DD Giancarrio Succi / Michele Marchesi / Zhang Hui (Translated)</p> <p>Instead, I want DD</p> <p>If match = / books / book / *, DD DD DD</p> <p>Why is this, please make a clear</p> <p>Solution and Xiang's solution</p> <p>Let us first familiarize with the XSL workflow.</p> <p>When the XSL parser starts working, I always look for the root node of the document, which is <xsl: template match = "/"> ... <xsl: template>. Almost every XSL document will be used this template .</p> <p>The XSL parser first finds the root of the document is <XSL: Template Match = "/"> If you don't write, it will use its built-in template, the main role of this template is to press you in the contents of all elements in the document. In the order of the XML documentation. It also finds the template you have provided for the node during the output. If it finds it, use this template, it will not use it built into it.</p> <p>The above words look like a bit confused, next, let's take a look at the process of the XSL document output in "Problem Description" to better understand the XSL workflow.</p> <p><XSL: Stylesheet XMLns: XSL = "http://www.w3.org/1999/xsl/transform" Version = "1.0"></p> <p><XSL: Output method = "html" /></p> <p><XSL: Template Match = "/ Books / Book / PRICE"></p> <p>DD</p> <p></ xsl: template></p> <p></ xsl: stylesheet></p> <p>The workflow for the above code parser is like this:</p> <p>1. Look for <XSL: Template Match = "/"> ... </ xsl: template>. Not provided here, so the parser uses it to be built.</p> <p>2. Look for <XSL: Template Match = "Books"> ... <</ xsl: template>. This will be available here. If there is a text, it will be in the end of the tag, it will Output. Nothing here, all do not output. For example: <books> death</p> <p><book></p> <p><title id = "TOUTOU"> Extreme programming research </ Title></p> <p><Price> 70 </ price></p> <p><author> giancarrio succi / michele marchesi / Zhang Hui (translation) </ author></p> <p></ book></p> <p></ books></p> <p>If there is no template for the BOOKS node, the parser will output "death when you deal with the BOOKS node". "</p> <p>3. Look for <XSL: Template Match = "Books / Book"> ... </ xsl: template>...................................................................................................................................................................................................................................</p> <p>4. Look for <XSL: Template Match = "Books / Book / Title"> ... </ xsl: template>. Nothing here, but there is a text "extreme programming research" between start and end tags, all parsers Kick it out of the world.</p> <p>5. Look for <XSL: Template Match = "Books / Book / Price"> ... </ xsl: template>. This node provides templates, the parser uses the feeding template, output "DD".</p> <p>6. Look for <XSL: Template Match = "Books / Book / Author"> ... </ xsl: template>. No template is available here, all "Giancarrio succi / michele marchesi / Zhang Hui (translated) is kicked out .</p> <p>The result of the final output is:</p> <p>Extreme Programming DD Giancarrio Succi / Michele Marchesi / Zhang Hui (Translated)</p> <p>Let's take a look at the second question of the problem: if match = / books / book / * is DD DD DD.</p> <p>Write the changed code:</p> <p><XSL: Stylesheet XMLns: XSL = "http://www.w3.org/1999/xsl/transform" Version = "1.0"></p> <p><XSL: Output method = "html" /></p> <p><XSL: Template Match = "/ Books / Book / *"></p> <p>DD</p> <p></ xsl: template></p> <p></ xsl: stylesheet></p> <p><XSL: Template Match = "/ Books / Book / *"> This code means that all nodes under books / book. You can see three nodes under BOOK via XML files. All code should be:</p> <p><XSL: Stylesheet XMLns: XSL = "http://www.w3.org/1999/xsl/transform" Version = "1.0"></p> <p><XSL: Output method = "html" /></p> <p><XSL: Template Match = "/ Books / Book / Title"></p> <p>DD</p> <p></ xsl: template></p> <p><XSL: Template Match = "/ Books / Book / PRICE"></p> <p>DD</p> <p></ xsl: template></p> <p><XSL: Template Match = "/ Books / Book / Autor"> DD</p> <p></ xsl: template></p> <p></ xsl: stylesheet></p> <p>This way the processor will call the provided template when the corresponding node is processed. So the result of the output will be as you can see.</p> <p>The result of the question is DD. What should we do?</p> <p>Here are a few solutions:</p> <p>The first, providing a template for a matching text node, covering the built-in template.</p> <p><XSL: Stylesheet XMLns: XSL = "http://www.w3.org/1999/xsl/transform" Version = "1.0"></p> <p><XSL: Output method = "html" /></p> <p><XSL: Template Match = "/"></p> <p><xsl: Apply-Template Select = "/ Books / Book / Price" /></p> <p><XSL: Template</p> <p><XSL: Template Match = "/ Books / Book / PRICE"></p> <p>DD</p> <p></ xsl: template></p> <p></ xsl: stylesheet></p> <p>The second, does not provide templates of the matching text node, but provides a template that matches the first-level node.</p> <p><XSL: Stylesheet XMLns: XSL = "http://www.w3.org/1999/xsl/transform" Version = "1.0"></p> <p><XSL: Output method = "html" /></p> <p><XSL: Template Match = "/ Books"></p> <p><xsl: Apply-Template Select = "/ Books / Book / Price" /></p> <p><XSL: Template</p> <p><XSL: Template Match = "/ Books / Book / PRICE"></p> <p>DD</p> <p></ xsl: template></p> <p></ xsl: stylesheet></p> <p>There are still many variations, please think yourself.</p> <p>Exterior:</p> <p>Everyone can see that XSL is displayed by selecting the content of the XML file content. Its work mode is a bit of hands. Of course, it can also show content with the XML file content. Examples, XSL is a You have achieved a good tool for hanging a sheep's head selling dog meat. Below is a XLS code, which realizes the function of hanging the dog's meat.</p> <p><? XML Version = "1.0" encoding = "GB2312"?></p> <p><XSL: Stylesheet XMLns: XSL = "http://www.w3.org/1999/xsl/transform" Version = "1.0"></p> <p><XSL: Output method = "html" /></p> <p><XSL: Template Match = "/"></p> <p><strong> Title: </ strong> <xsl: Apply-Templates SELECT = "/ Books / Book / Title" />,</p> <p><strong> Price: </ strong> <xsl: Apply-Templates SELECT = "/ Books / Book / Price" />,</p> <p><Strong> Author: </ strong> <xsl: apply-templates select = "/ books / book / author" /> </ xsl: template></p> <p><XSL: Template Match = "/ Books / Book / PRICE"></p> <p>Don't sell money!</p> <p></ xsl: template></p> <p><XSL: Template Match = "/ Books / Book / Author"></p> <p>Die</p> <p></ xsl: template></p> <p><XSL: Template Match = "/ Books / Book / Title"></p> <p>Extremely metamorphosis tobacco!</p> <p></ xsl: template></p> <p></ xsl: stylesheet></p> <p>This is my first article. I have been a lot of four years in 9cbs. I have learned a lot from here. But I have never sent a post. I saw the questionr in the forum. This is, I have just learned the difficulties in XML, so I wrote this article, I hope to help beginners spend the difficulties. In order to fall into the "dung" for a long time.</p> <p>In addition, I hope that everyone will make valuable comments and suggestions, I have unconditionally accept the insults and destruction of everyone.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-24556.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="24556" 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 = 'e3Cx0ihfOmnOdKGrsxJLfsp1_2FychG5F_2FU5PNuvsSITXEEnjGcvvgozjVqSC4nC21LfljN6U6LSf2aMCUn3AESw_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>