RE: [xsl] Delete XML Node

Subject: RE: [xsl] Delete XML Node
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Thu, 31 Oct 2002 17:12:52 -0500

Given mapping rules like this, a template-based approach is *way* easier:

At 04:57 PM 10/31/2002, you wrote:
Input XML Output XML

<X> maps to----> <X1>

<xsl:template match="X"> <X1> <xsl:apply-templates/> </X1> </xsl:template>

<A>A</A> maps to----> <A1>A</A>

<xsl:template match="A"> <A1> <xsl:apply-templates/> </A1> </xsl:template>

 <Y>             maps to--->               <Y1>
                 if resultant <Y1>
                 has got elements within it

I'm translating this into "maps to Y1 if the source Y contains a B with value "ABC" or a C with value "XYZ". This re-expression is necessary since you can inspect the source tree, but the result tree doesn't exist yet so you can't test the "resultant Y1":

<xsl:template match="Y">
  <xsl:if test="B[.='ABC'] or C[.='XYZ']">

   <B>B</B>      maps to---->                 <B1>B1</B1>
                 only if data contained
                 in <B> is "ABC"

<xsl:template match="B"> <xsl:if test=". ='ABC'"> <B1> <xsl:apply-templates/> </B1> </xsl:if> </xsl:template>

Or more simply, if a Y will never have more than one B, you can do

<xsl:template match="B">

(Since you'll never even get to the B if the Y test was not passed, it's safe to just process it.)

   <C>C</C>      maps to---->                 <C1>C1</C1>
                 only if data contained
                 in <C> is "XYZ"

like B, with appropriate adjustments

  </Y>                                      </Y1>
</X>                                         </X>

According to the mapping rules for <B> and <C> the output XML should not contain <B1> and <C1> and thus <Y1> would be empty. But <Y1> should appear in the output only if <B1> or <C1> is present.

Put all these templates together in a stylesheet and try it out....

I hope this helps,

====================================================================== Wendell Piez mailto:wapiez@xxxxxxxxxxxxxxxx Mulberry Technologies, Inc. 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:

Current Thread