利用XSLT产生一个唯一的ID并引用它

Generating Unique IDs and Linking to Them

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.

  1<xsl:stylesheet xmlns:xsl="http: 1999="" transform"="" version="1.0" www.w3.org="" xsl=""><xsl:output method="xml" omit-xml-declaration="yes"></xsl:output method="xml" omit-xml-declaration="yes">
  2      <xsl:template match="chapter | sect1 | sect2">
  3        <xsl:copy>
  4          **&lt; xsl:attribute name="uid"&gt;
  5            <xsl:value-of select="generate-id(.)"></xsl:value-of select="generate-id(.)">
  6          **
  7          <xsl:apply-templates select="@*|node()"></xsl:apply-templates select="@*|node()">
  8        </xsl:copy>
  9      
 10      <xsl:template match="@*|node()">
 11        <xsl:copy>
 12          <xsl:apply-templates select="@*|node()"></xsl:apply-templates select="@*|node()">
 13        </xsl:copy>
 14      
 15
 16    
 17
 18The stylesheet turns this XML document 
 19    
 20    
 21    <chapter>
 22<para>Then with expanded wings he steers his flight</para>
 23<figure><title>"Incumbent on the Dusky Air"</title>
 24<graphic fileref="pic1.jpg"></graphic fileref="pic1.jpg"></figure>
 25<para>Aloft, incumbent on the dusky Air</para>
 26<sect1>
 27<para>That felt unusual weight, till on dry Land</para>
 28<figure><title>"He Lights"</title>
 29<graphic fileref="pic2.jpg"></graphic fileref="pic2.jpg"></figure>
 30<para>He lights, if it were Land that ever burned</para>
 31<sect2>
 32<para>With solid, as the Lake with liquid fire</para>
 33<figure><title>"The Lake with Liquid Fire"</title>
 34<graphic fileref="pic3.jpg"></graphic fileref="pic3.jpg"></figure>
 35</sect2>
 36</sect1>
 37</chapter>
 38    
 39
 40into this one: 
 41    
 42    
 43    <chapter **uid="n134711680"**>
 44<para>Then with expanded wings he steers his flight</para>
 45<figure><title>"Incumbent on the Dusky Air"</title>
 46<graphic fileref="pic1.jpg"></graphic fileref="pic1.jpg"></figure>
 47<para>Aloft, incumbent on the dusky Air</para>
 48<sect1 **uid="n134683456"**>
 49<para>That felt unusual weight, till on dry Land</para>
 50<figure><title>"He Lights"</title>
 51<graphic fileref="pic2.jpg"></graphic fileref="pic2.jpg"></figure>
 52<para>He lights, if it were Land that ever burned</para>
 53<sect2 **uid="n134684064"**>
 54<para>With solid, as the Lake with liquid fire</para>
 55<figure><title>"The Lake with Liquid Fire"</title>
 56<graphic fileref="pic3.jpg"></graphic fileref="pic3.jpg"></figure>
 57
 58
 59
 60
 61Your 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. 
 62
 63For 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: 
 64    
 65    
 66    <xsl:stylesheet xmlns:xsl="http: 1999="" transform"="" version="1.0" www.w3.org="" xsl="">
 67      <xsl:output method="html"></xsl:output method="html">
 68      <xsl:template match="chapter">
 69        <html><body>
 70          <!-- Generate a list of picture titles, with each 
 71               title linking to the picture in the poem below. -->
 72          <b>Pictures:</b>
 73
 74          <xsl:for-each select="descendant::figure">
 75            **&lt; a href="#{generate-id(graphic)}"&gt;
 76            <xsl:value-of select="title"></xsl:value-of select="title">**
 77
 78        
 79        <xsl:apply-templates></xsl:apply-templates>
 80        </xsl:for-each select="descendant::figure"></body></html>
 81      
 82      <xsl:template match="para">
 83        <p><xsl:apply-templates></xsl:apply-templates></p>
 84      
 85      <xsl:template match="graphic">
 86        <!-- Image and title as caption, centered. -->
 87        <center> **&lt; a name="{generate-id(.)}"&gt;<img src="{@fileref}"></img src="{@fileref}">**
 88        <b><xsl:value-of select=".. title"=""></xsl:value-of select="..></b></center>
 89      
 90      <!-- Suppress figure title because "graphic" template
 91           rule already added it to result tree. -->
 92      <xsl:template match="figure title"=""></xsl:template match="figure>
 93
 94    
 95
 96With the source document above, this stylesheet creates the following HTML document: 
 97    
 98    
 99    <html>
100        <body>
101            <b>Pictures:</b>
102            <br/>
103            <a **href="#n134691840"**>"Incumbent on the Dusky Air"
104            <br/>
105            <a **href="#n134692416"**>"He Lights"
106            <br/>
107            <a **href="#n134757920"**>"The Lake with Liquid Fire"
108            <br/>
109<p>Then with expanded wings he steers his flight</p>
110<center>
111                <a **name="n134691840"**><img src="pic1.jpg">
112                <b>"Incumbent on the Dusky Air"</b>
113            </img src="pic1.jpg"></a **name="n134691840"**></center>
114<p>Aloft, incumbent on the dusky Air</p>
115<p>That felt unusual weight, till on dry Land</p>
116<center>
117                <a **name="n134692416"**><img src="pic2.jpg">
118                <b>"He Lights"</b>
119            </img src="pic2.jpg"></a **name="n134692416"**></center>
120<p>He lights, if it were Land that ever burned</p>
121<p>With solid, as the Lake with liquid fire</p>
122<center>
123                <a **name="n134757920"**><img src="pic3.jpg">
124                <b>"The Lake with Liquid Fire"</b>
125            </img src="pic3.jpg"></a **name="n134757920"**></center>
126</a **href="#n134757920"**></a **href="#n134692416"**></a **href="#n134691840"**></body>
127</html>
128    
129
130(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: 
131
132  * 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 in the link origin. For example, 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. 
133
134Instead 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 creates an ID string for each of the three ` graphic ` elements: "N134691840", "N134692416", and "N134757920". 
135
136  * 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 " ` . ` " 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. 
137
138
139
140
141This 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.</a></a></xsl:template match="graphic"></xsl:template match="para"></xsl:template match="chapter"></xsl:stylesheet xmlns:xsl="http:></sect2 **uid="n134684064"**></sect1 **uid="n134683456"**></chapter **uid="n134711680"**></xsl:template match="@*|node()"></xsl:template match="chapter | sect1 | sect2"></xsl:stylesheet xmlns:xsl="http:>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus