Re: [xsl] template matching everywhere

Subject: Re: [xsl] template matching everywhere
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Thu, 04 Mar 2004 18:39:02 -0500
Hi JF,

At 06:06 PM 3/4/2004, you wrote:
I would like to write a template that match a particular element but this element could be located anywhere

No problem: templates are generally written to match elements no matter where they appear (though you can go to extra trouble to restrict this behavior).


<ROOT>
   <NODE>
      maybe some text here
      <TOBEMATCHED>    </TOBEMATCHED>
      maybe some text here
   </NODE>
   <NODE>
      <SUBNODE>
          maybe some text here
          <TOBEMATCHED>   </TOBEMATCHED>
          maybe some text here
      </SUBNODE>
      <SUBNODE>
          <SUBSUBNODE>
               maybe some text here
              <TOBEMATCHED>   </TOBEMATCHED>
              maybe some text here
          </SUBSUBNODE>
      </SUBNODE>
   </NODE>
</ROOT>

I would like to write a template that allow me to automatically translate the TOBEMATCHED element while processing his parent.

The way the XSLT processing model works, you don't translate TOBEMATCHED while processing its parent; you output the result you want from TOBEMATCHED when you process TOBEMATCHED itself:


<xsl:template match="TOBEMATCHED">
  <!-- this works wherever TOBEMATCHED occurs, assuming the node
       is selected for processing at all -->
  <output>
    <xsl:apply-templates/>
  </output>
</xsl:template>

...which will replace every TOBEMATCHED element with an <output> element.

For example suppose you have to proceed an HTML document and you want to replace each <b> tag with an <em> tag and did other work on other it may help you understand what I want to do.

<xsl:template match="b"> <em> <xsl:apply-templates/> </em> </xsl:template>

Assuming you do not override the default node traversal of the document, this will result in all <b> elements occuring anywhere with <em> elements (and will go on to process the rest of the document too).

This is so basic to XSLT that I wonder where the gap is. Have you looked at any introductory material on the XSLT processing model? It might also be instructive to you to try a simple stylesheet containing nothing but a template or two like this one.

My guess is that if you can't get this to work, it's because you are making things too complicated. Maybe posting a complete sample with a stylesheet that you believe should work, but doesn't, can help us understand where to direct your attention.

Cheers,
Wendell


====================================================================== Wendell Piez mailto:wapiez@xxxxxxxxxxxxxxxx Mulberry Technologies, Inc. http://www.mulberrytech.com 17 West Jefferson Street Direct Phone: 301/315-9635 Suite 207 Phone: 301/315-9631 Rockville, MD 20850 Fax: 301/315-8285 ---------------------------------------------------------------------- Mulberry Technologies: A Consultancy Specializing in SGML and XML ======================================================================


XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list



Current Thread