Inserting elements when surrounding elements are optional

Roseanne Zhang

I made a template and it works now.

I made up an xml to test, the filter element is always inserted in the correct place.

Take a look! Thanks!

Q. How to insert an element in the right place when elements before or after are optional?

The style sheet:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml" indent="yes"/>

  <xsl:variable name="bf" select="'icon display-name description distributable context-param'" />
  <xsl:variable name="af" select="'filter filter-mapping listener servlet servlet-mapping session-config mime-mapping welcome-file-list error-page taglib resource-env-ref resource-ref security-constraint login-config security-role env-entry ejb-ref ejb-local-ref'" />
  <xsl:template match="/">
    <xsl:apply-templates />
  <xsl:template match="web-app">
      <xsl:apply-templates select="@*|node()[contains($bf,name())]"/>
      <xsl:call-template name="newfilter" />
      <xsl:apply-templates select="@*|node()[contains($af,name())]"/>
  <xsl:template match="@*|node()">
      <xsl:apply-templates select="@*|node()"/>
  <xsl:template match="comment()"  />

  <xsl:template name="newfilter" >
    <xsl:value-of select="'  '" /><filter>This is a filter</filter>