English 中文(简体)
XSLT - <key>
  • 时间:2024-11-05

XSLT <key>


Previous Page Next Page  

<xsl:key> tag要素具体指明了XML文件中具体内容的名值乳制品。 该钥匙用于XPath语中的关键()功能,以便查阅XML文件中的指定内容。

Declaration

以下为<xsl:key>。 内容。

<xsl:key 
   name = QName 
   match = Pattern 
   use = Expression > 
</xsl:key>

Attributes

S.No Name & Description
1

拟使用的关键名称。

2

用于确定具有这一关键意义的节点的模式。

3

XPath expression to identifying the nodes of xml document.

Elements

Number of Occurrences Unpmited
Parent elements xsl:stylesheet
Child elements None

Demo Example

这个例子创建了一个“与”;“student>”表;其属性如下:rollno及其子女与lt;firstname> 和lt;lastname> 和lt;nickname>and <marks> by iterating over each students。 它检查了学生名字中的第一个名字,然后印刷学生细节。

iii

<?xml version = "1.0"?> 
<?xml-stylesheet type = "text/xsl" href = "students.xsl"?> 
<class> 
   <student rollno = "393"> 
      <firstname>Dinkar</firstname> 
      <lastname>Kad</lastname> 
      <nickname>Dinkar</nickname> 
      <marks>85</marks> 
   </student> 
   <student rollno = "493"> 
      <firstname>Vaneet</firstname> 
      <lastname>Gupta</lastname> 
      <nickname>Vinni</nickname> 
      <marks>95</marks> 
   </student> 
   <student rollno = "593"> 
      <firstname>Jasvir</firstname> 
      <lastname>Singh</lastname> 
      <nickname>Jazz</nickname> 
      <marks>90</marks> 
   </student> 
</class>

students.xsl

<xsl:stylesheet version = "1.0" 
   xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
   <xsl:key name = "firstname-search" match = "student" use = "firstname"/> 
   <xsl:template match = "/"> 
      <html> 
         <body> 
            <h2>Students</h2> 
            <table border = "1"> 
               <tr bgcolor = "#9acd32"> 
                  <th>Roll No</th> 
                  <th>First Name</th> 
                  <th>Last Name</th> 
                  <th>Nick Name</th> 
                  <th>Marks</th> 
               </tr> 
					
               <xsl:for-each select = "key( firstname-search ,  Dinkar )"> 
				
                  <tr> 
                     <td><xsl:value-of select = "@rollno"/></td> 
                     <td><xsl:value-of select = "firstname"/></td> 
                     <td><xsl:value-of select = "lastname"/></td> 
                     <td><xsl:value-of select = "nickname"/></td> 
                     <td><xsl:value-of select = "marks"/></td> 
                  </tr> 
					
               </xsl:for-each> 
            </table> 
         </body> 
      </html> 
   </xsl:template>  
</xsl:stylesheet>

Output

Formatted Key Output Advertisements