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

Read More →

Create an array in xslt

You can create any data type model you want to in xslt. To create an array, just create a variable for it. Example: <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text" omit-xml-declaration="yes"/> <xsl:variable name="array" as="element()*"> <Item>A</Item> <Item>B</Item> <Item>C</Item> </xsl:variable> <xsl:template match="/"> <xsl:value-of select="$array[2]"/> </xsl:template> </xsl:stylesheet> Output B

Read More →