Xsl Generate-id Key Example
You can use keys as generalized IDs for referencing elements in an XML document. To do this, first use xsl:key to declare a key for a node. Then call the key function to retrieve the node, supplying the name and value of the key as the function arguments. For more information, see the example below. Multiple keys can be declared on a node. Aug 20, 2008 I have what I think should be a very easy problem to solve but the solution eludes me. I have a simple stylesheet that attempts to lookup a value using the xsl:key tag and key functions. On this page the XSLT function generate-id is described which generates an identifier (ID) for a node during the runtime of the stylesheet. XML technologies / XSLT / XSLT and XPath functions / Alphabetical XSLT and XPath reference / generate-id. Example - generating corresponding id anchors: xsl:template match='song'
- This example uses both keys and modes. Every 'computer' element in my inventory has a 'vendor' by which I want to group: Define the key.
- Oct 03, 2001 Generating Unique IDs and Linking to Them. October 3, 2001. Bob DuCharme. 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.
- XSLT Tutorial
- XSLT Useful Resources
- Selected Reading
<xsl:key> tag element specifies a named name-value pair assigned to a specific element in an XML document. This key is used with the key() function in XPath expressions to access the assigned elements in an XML document.
Declaration
Following is the syntax declaration of <xsl:key> element.
Attributes
S.No | Name & Description |
---|---|
1 | Name Name of the key to be used. |
2 | Match Patterns used to identify a node that holds this key. |
3 | Use XPath expression to identify the value of the nodes of xml document. |
Elements
Number of Occurrences | Unlimited |
Parent elements | xsl:stylesheet |
Child elements | None |
Demo Example
This example creates a table of <student> element with its attribute rollno and its child <firstname>, <lastname>, <nickname>, and <marks> by iterating over each student. It checks key as firstname to be one of the student's name and then prints the student details.
students.xml
students.xsl
Output
-->Returns a string that uniquely identifies the node in the node-set argument that is first in document order.
Remarks
The unique identifier must consist of ASCII alphanumeric characters and must start with an alphabetic character. Thus, the string is syntactically an XML name. There is no guarantee that a generated unique identifier will be distinct from any unique IDs specified in the source document. If the node-set argument is empty, the empty string is returned. If the argument is omitted, it defaults to the context node.
Example
Xsl Generate-id Key Example Free
XML File (data.xml)
XSLT File (sample.xsl)
This is the formatted output:
When you click the left button in Internet Explorer, an alert box displays 'IDAHAGJD'.
When you click the right button in Internet Explorer, an alert box displays 'IDAPAGJD'.
Notice that the ID value is generate at run-time. /product-key-generator-for-microsoft-office-professional-plus-2010.html. Therefore, the specific values may vary in different invocation of the transformation.
This is the processor output:
<html>
<body><button>Gambardella, Matthew</button>
<button>Ralls, Kim</button></body>
</html>