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>
    
  <xsl:template match="web-app">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()[contains($bf,name())]"/>
      <xsl:call-template name="newfilter" />
      <xsl:apply-templates select="@*|node()[contains($af,name())]"/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="comment()"  />

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

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact