XML templates
XML content assist provides a comment template, a section of predefined code that you can insert into a file. You may find a template useful when you have a certain piece of code you want to reuse several times, and you do not want to write it out every time.
You can use a default template as provided, customize that template, or create your own templates.
For example, you can work on a group of XML pages that should all contain a table with a specific appearance. Create a template that contains the tags for that table, including the appropriate attributes and attribute values for each tag. (You can copy and paste the tags from a structured text editor into the template’s Pattern field.) Then select the name of the template from a content assist proposal list whenever you want to insert your custom table into an XML file.
To create a new XML template, complete the following steps:
- Click Window > Preferences and select XML > XML Files > Templates.
- Click New if you want to create a completely new template.
- Supply a new template Name and Description.
- Specify the Context for the template. This is the context in which the template is available in the proposal list when content assist is requested.
- Specify the Pattern for your template using the appropriate tags, attributes, or attribute values to be inserted by content assist.
- If you want to insert a variable, click the Insert Variable button and select the variable to be inserted.For example, the date variable indicates the current date will be inserted.
- Click OK and then Apply to save your changes.
File: Data.xml
<?xml version=”1.0″?>
<employees>
<animal>
<name language=”English”>T1</name>
<name language=”Latin”>T2</name>
<projects>
<project>project1</project>
</projects>
</animal>
</employees>
File: Transform.xslt
<?xml version=”1.0″?>
<xsl:stylesheet xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”
version=”1.0″>
<xsl:template match=”/”>
<html>
<head>
<title>this is the title</title>
</head>
<body bgcolor=”white”>
<xsl:apply-templates select=”employees/animal” />
</body>
</html>
</xsl:template>
<xsl:template match=”animal”>
<p align=”center”>
<br />
<font size=”+3″>
<xsl:apply-templates select=”name” />
</font>
</p>
<paragraph>
<xsl:value-of select=”name[@language=’English’]” />
<a href=”http://www.java2s.com”>pages</a>
</p>
<hr />
</xsl:template>
<xsl:template match=”name[@language=’English’]”>
<nobr>
<b>
<xsl:value-of select=”.” />
:
</b>
</nobr>
</xsl:template>
<xsl:template match=”name[@language=’Latin’]”>
<nobr>
<i>
<xsl:value-of select=”.” />
</i>
</nobr>
</xsl:template>
</xsl:stylesheet>
Output:
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>this is the title</title>
</head>
<body bgcolor=”white”>
<p align=”center”><br><font size=”+3″>
<nobr><b>T1
:
</b></nobr>
<nobr><i>T2</i></nobr></font></p>
<paragraph>T1<a href=”http://www.java2s.com”>pages</a></p>
<hr>
</body>
</html>