Elements in XSLT


XSLT is all about selecting one or more nodes from XML document and transforming or replacing its content with something else. A node could be any of the following: elements, attributes, text, namespaces, processing-instructions, and comments.
The <xsl:template> element is to select a node from XML document and transform its contents.

To select an element, you use the match attribute. To transform its contents, you simply place the new content between the opening (<xsl:template>) and closing (</xsl:template>) tags.

Example

<FoodItem>
    <Item1>Juice</Item1>
    <Item2>Milk</Item2>
</FoodItem>

In this case, I’m selecting the root node (i.e. FoodItem). By selecting this node, the template element tells the XSLT processor how to transform the output. The processor will replace the root node (i.e. the whole XML document) with what is being written between the <xsl:template> tags.
In this case, the contents of an HTML document are written inside the tags. When a user views any XML document that uses this XSL document, they will simply see the line “New item…”.

<xsl:template match="FoodItem">
  <html> 
    <body>
      <p>New item...</p>
    </body>
  </html>
</xsl:template>

Root Node Selection

In the example above, we selected the “FoodItem” node which happens to be the root node of our XML document. Another way of selecting the root node is to use a forward slash in place of the node’s name. The following example results in the same output as the above example.

Example:

<xsl:template match="/">
  <html>
    <body>
      <p>New item...</p>
    </body>
  </html>
</xsl:template>

 

Advertisements

Categories: xslt 1.0

Tagged as: , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.