Generate a unique ID using XSLT and reference it

zhaozj2021-02-11  192

GENERATING UNIQUE IDS AND LINKING TOTHM

When an XSLT stylesheet converts one XML document into another, the ability to add unique ID values ​​to elements in the result document can make the result document much more useful to applications that use it. Adding unique IDs can, for example, turn each element into The Unique Target OF A LINK.

XSLT's generate-id () function generates a unique ID for a node passed to it as an argument. This ID starts with a letter so that you can use it as the value of an XML ID attribute. For example, the following stylesheet copies an XML document and adds a uid ( "unique ID") attribute to each chapter, sect1, and sect2 element The xsl:. value-of instruction uses the generate-id () function in the stylesheet's first template rule to create a value for these Attribute.

Version =

"1.0">

The Stylesheet Turns this XML Document

Then with Expanded Wings He Steers His Flight

"Incumbent on the dusky air" </ title> <graphic fileref = "pic1.jpg" /> </ figure></p> <p><Para> Aloft, Incumbent on The Dusky Air </ Para></p> <p><SECT1></p> <p><Para> That Felt Unusual Weight, Till On Dry Land </ Para></p> <p><Figure> <title> "He Lights </ Title></p> <p><graphic fileref = "pic2.jpg" /> </ figure></p> <p><Para> He Lights, IT WERE LAND That Ever Burned </ Para></p> <p><SECT2></p> <p><Para> with solid, as the lake with liquid fire </ para></p> <p><Figure> <title> "The Lake with Liquid Fire" </ Title></p> <p><graphic fileref = "pic3.jpg" /> </ figure></p> <p></ sect2></p> <p></ sect1></p> <p></ chapter></p> <p>INTO this one:</p> <p><chapter uid = "n134711680"></p> <p><Para> Then with Expanded Wings He Steers His Flight </ Para></p> <p><Figure> <title> "Incumbent on the dusky air" </ title></p> <p><graphic fileref = "pic1.jpg" /> </ figure></p> <p><Para> Aloft, Incumbent on The Dusky Air </ Para></p> <p><SECT1 UID = "N134683456"></p> <p><Para> That Felt Unusual Weight, Till On Dry Land </ Para></p> <p><Figure> <title> "He Lights </ Title></p> <p><graphic fileref = "pic2.jpg" /> </ figure></p> <p><Para> He Lights, IT WERE LAND That Ever Burned </ Para></p> <p><SECT2 UID = "N134684064"></p> <p><Para> with solid, as the lake with liquid fire </ para></p> <p><Figure> <title> "The Lake with Liquid Fire" </ Title></p> <p><graphic fileref = "pic3.jpg" /> </ figure></p> <p></ sect2></p> <p></ sect1></p> <p></ chapter></p> <p>Your XSLT processor may generate different values ​​with the generate-id () function. In fact, if you run the same stylesheet with the same input document a second time, the XSLT processor may not generate the same ID values ​​that it generated the first time . However, if you call generate-id () more than once in one run with the same node as an argument, it generates the same ID value each time for that node. Because unique IDs are popular ways to identify link destinations, this consistency of the generate-id () function makes it a great way to generate links.For example, adding a list of all of its illustrations at the beginning of the result document. If we make the result tree version an HTML file, we can use THE generate-id function to Turn Each Entry of this opening illustration list int in html link to the img element in the body of the document That Has the Illustration:</p> <p><XSL: Stylesheet XMLns: XSL = "http://www.w3.org/1999/xsl/transform"</p> <p>Version = "1.0"></p> <p><XSL: Output method = "html" /></p> <p><XSL: Template Match = "Chapter"></p> <p><html> <body></p> <p><! - Generate a List of Picture Titles, with Each</p> <p>Title Linking to The Picture In The Poem Below. -></p> <p><b> Pictures: </ b></p> <p><xsl: for-each select = "descendant :: figure"></p> <p><a href="#{ generate-id (growth )"></p> <p><xsl: value-of select = "title" /> </a></p> <p></ xsl: for-energy></p> <p><XSL: Apply-Templates /></p> <p></ body> </ html></p> <p></ xsl: template></p> <p><XSL: Template Match = "Para"></p> <p><p> <xsl: Apply-Templates /> </ P></p> <p></ xsl: template></p> <p><XSL: Template Match = "Graphic"></p> <p><! - image and title as caption, centered. -> <center> <a name="{ generate-id(.) }"> <img src = "{@ fileref}" /> </A></p> <p><B> <XSL: Value-of Select = "../ Title" /> </ b> </ center></p> <p></ xsl: template></p> <p><! - Suppress Figure Title Because "Graphic" Template</p> <p>Rule Already Added It To Result Tree. -></p> <p><XSL: Template Match = "Figure / Title" /></p> <p></ xsl: stylesheet></p> <p>With The Source Document Above, this Stylesheet Creates The Following HTML Document:</p> <p><html></p> <p><body></p> <p><b> Pictures: </ b></p> <p><br></p> <p><a href="#n134691840"> "incumbent on the dusky air" </A></p> <p><br></p> <p><a href="#n134692416"> "he lights" </A></p> <p><br></p> <p><a href="#n134757920"> "The Lake with Liquid fire" </A></p> <p><br></p> <p><P> Then with expanded Wings He Steers His Flight </ P></p> <p><center></p> <p><a name="n134691840"> <img src = "pic1.jpg"> </A></p> <p><b> "Incumbent on the dusky air" </ b></p> <p></ center></p> <p><p> aloft, incumbent on the dusky air </ p></p> <p><p> That Felt Unusual Weight, Till On Dry Land </ P></p> <p><center></p> <p><a name="n134692416"> <img src = "pic2.jpg"> </A></p> <p><b> "He Lights" </ b></p> <p></ center></p> <p><p> he lights, if it were land this EVER BURNED </ P></p> <p><p> with solid, as the lake with liquid fire </ p></p> <p><center></p> <p><a name="n134757920"> <img src = "pic3.jpg"> </A></p> <p><B> "The Lake with Liquid Fire" </ b></p> <p></ center></p> <p></ body></p> <p></ html></p> <p>(To View The HTML Document, You'll NEED To Supply Your OWN PIC1.JPG, PIC2.JPG, AND PIC3.JPG Files.) The Stylesheet Uses The Generate-ID Twice:</p> <p>As the xsl: for-each instruction in the "chapter" template rule adds each figure element's title to the result tree for the "Pictures:" list at the beginning of the result document, it puts each of these title elements inside of an HTML a element to link to the appropriate picture in the main part of the document. Each of these a elements has an href attribute to indicate the link destination. An href attribute that begins with a pound sign ( "#") looks for the link destination In The Same Document - Specifically, IT Looks for Another A ELEMENT WITH A Name Attribute Value Equal To The Part After The Pound Sign, An A Start-Tag of <a href="#a123"> links to an a element with an <a name="a123"> start-tag elsewhere in the same document. Instead of the string "a123" identifying each link destination, this stylesheet uses the generate-id () function to make up an Identifying string. Because The graphic element node is passed to it as an argument, the function cre ates an ID string for each of the three graphic elements:. "N134691840", "N134692416", and "N134757920" To create the link destinations, the "graphic" template rule puts each HTML img element in the result tree inside of an a element. These img elements use the value of the source tree graphic elements' fileref attributes as their src value, and the a elements use the generate-id () function to create the values ​​for their name attributes. Passing this function an argument of " "</p> <p>is the same as passing it self :: node (), which in this case means passing it the graphic element node, so the XSLT processor generates an ID value for each graphic node. These are the same three nodes that the earlier use of the generate-id () created IDs for, and it creates the same three values:. "N134691840", "N134692416", and "N134757920" When this HTML file is displayed in a browser, each link in the opening "Pictures:" list will now go to the corresponding picture in the document.This consistency in the generate-id () function's treatment of a particular node, even if the function generates an ID for that node more than once, is the key to its power. These graphic .</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-5424.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="5424" 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.039</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 = '8HvQraGcUR1EvfznzOxaVGj6DWF8JrEfZEeTImF_2BfHM7HGwC_2FV8Z5tjJSRjn8IiAoNvuTpGRE89mGQ3L'; 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>