选择显示字体大小

利用xslt产生一个唯一的id并引用它

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 attributes.

<xsl:stylesheet xmlns:xsl=&quot;http://www.w3.org/1999/xsl/transform&quot;
version=
&quot;1.0&quot;><xsl:output method=&quot;xml&quot; omit-xml-declaration=&quot;yes&quot;/>
<xsl:template match=&quot;chapter sect1 sect2&quot;>
<xsl:copy>
<xsl:attribute name=&quot;uid&quot;>
<xsl:value-of select=&quot;generate-id(.)&quot;/>
</xsl:attribute>
<xsl:apply-templates select=&quot;@*node()&quot;/>
</xsl:copy>
</xsl:template>
<xsl:template match=&quot;@*node()&quot;>
<xsl:copy>
<xsl:apply-templates select=&quot;@*node()&quot;/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

the stylesheet turns this xml document

<chapter>
<para>then with expanded wings he steers his flight</para>
<figure><title>&quot;incumbent on the dusky air&quot;</title>
<graphic fileref=&quot;pic1.jpg&quot;/></figure>
<para>aloft, incumbent on the dusky air</para>
<sect1>
<para>that felt unusual weight, till on dry land</para>
<figure><title>&quot;he lights&quot;</title>
<graphic fileref=&quot;pic2.jpg&quot;/></figure>
<para>he lights, if it were land that ever burned</para>
<sect2>
<para>with solid, as the lake with liquid fire</para>
<figure><title>&quot;the lake with liquid fire&quot;</title>
<graphic fileref=&quot;pic3.jpg&quot;/></figure>
</sect2>
</sect1>
</chapter>

into this one:

<chapter uid=&quot;n134711680&quot;>
<para>then with expanded wings he steers his flight</para>
<figure><title>&quot;incumbent on the dusky air&quot;</title>
<graphic fileref=&quot;pic1.jpg&quot;/></figure>
<para>aloft, incumbent on the dusky air</para>
<sect1 uid=&quot;n134683456&quot;>
<para>that felt unusual weight, till on dry land</para>
<figure><title>&quot;he lights&quot;</title>
<graphic fileref=&quot;pic2.jpg&quot;/></figure>
<para>he lights, if it were land that ever burned</para>
<sect2 uid=&quot;n134684064&quot;>
<para>with solid, as the lake with liquid fire</para>
<figure><title>&quot;the lake with liquid fire&quot;</title>
<graphic fileref=&quot;pic3.jpg&quot;/></figure>
</sect2>
</sect1>
</chapter>
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 into an html link to the img element in the body of the document that has the illustration:

<xsl:stylesheet xmlns:xsl=&quot;http://www.w3.org/1999/xsl/transform&quot;
version=&quot;1.0&quot;>
<xsl:output method=&quot;html&quot;/>
<xsl:template match=&quot;chapter&quot;>
<html><body>
<!-- generate a list of picture titles, with each
title linking to the picture in the poem below. -->
<b>pictures:</b><br/>
<xsl:for-each select=&quot;descendant::figure&quot;>
<a href=&quot;#{generate-id(graphic)}&quot;>
<xsl:value-of select=&quot;title&quot;/></a><br/>
</xsl:for-each>
<xsl:apply-templates/>
</body></html>
</xsl:template>
<xsl:template match=&quot;para&quot;>
<p><xsl:apply-templates/></p>
</xsl:template>
<xsl:template match=&quot;graphic&quot;>
<!-- image and title as caption, centered. -->
<center><a name=&quot;{generate-id(.)}&quot;><img src=&quot;{@fileref}&quot;/></a>
<b><xsl:value-of select=&quot;../title&quot;/></b></center>
</xsl:template>
<!-- suppress figure title because &quot;graphic&quot; template
rule already added it to result tree. -->
<xsl:template match=&quot;figure/title&quot;/>
</xsl:stylesheet>

with the source document above, this stylesheet creates the following html document:

<html>
<body>
<b>pictures:</b>
<br>
<a href=&quot;#n134691840&quot;>&quot;incumbent on the dusky air&quot;</a>
<br>
<a href=&quot;#n134692416&quot;>&quot;he lights&quot;</a>
<br>
<a href=&quot;#n134757920&quot;>&quot;the lake with liquid fire&quot;</a>
<br>
<p>then with expanded wings he steers his flight</p>
<center>
<a name=&quot;n134691840&quot;><img src=&quot;pic1.jpg&quot;></a>
<b>&quot;incumbent on the dusky air&quot;</b>
</center>
<p>aloft, incumbent on the dusky air</p>
<p>that felt unusual weight, till on dry land</p>
<center>
<a name=&quot;n134692416&quot;><img src=&quot;pic2.jpg&quot;></a>
<b>&quot;he lights&quot;</b>
</center>
<p>he lights, if it were land that ever burned</p>
<p>with solid, as the lake with liquid fire</p>
<center>
<a name=&quot;n134757920&quot;><img src=&quot;pic3.jpg&quot;></a>
<b>&quot;the lake with liquid fire&quot;</b>
</center>

</body>
</html>

(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() id twice:

as the xsl:for-each instruction in the &quot;chapter&quot; template rule adds each figure element's title to the result tree for the &quot;pictures:&quot; 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 (&quot;#&quot;) 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 in the link origin. for example, an a start-tag of <a href=&quot;#a123&quot;> links to an a element with an <a name=&quot;a123&quot;> start-tag elsewhere in the same document.

instead of the string &quot;a123&quot; 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 creates an id string for each of the three graphic elements: &quot;n134691840&quot;, &quot;n134692416&quot;, and &quot;n134757920&quot;.

to create the link destinations, the &quot;graphic&quot; 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 &quot;.&quot; 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: &quot;n134691840&quot;, &quot;n134692416&quot;, and &quot;n134757920&quot;. when this html file is displayed in a browser, each link in the opening &quot;pictures:&quot; 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 elements didn't have ids in the source document; with the help of this function, their equivalent in the result document has them, and other elements in that document use those ids to link to them.

  


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons