[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

RE: [xsl] Delete XML Node


Subject: RE: [xsl] Delete XML Node
From: "Deepak Rao" <deepaksubs@xxxxxxxxxxx>
Date: Fri, 01 Nov 2002 09:32:49 -0500

Wendell,

Thank you very much. Your solution works perfectly fine. But looking at a maintenance point of view, the rule for <B> and <C> are repeated twice, once for <Y> and once of either <B> or <C>. If there is a change to any of the rules for <B> or <C>, I would have to remember to change two places instead of just one. Do you think there is a way to avoid this?

Thanks,
Deepak






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

Deepak,

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']">
    <Y1>
      <xsl:apply-templates/>
    </Y1>
  </xsl:if>
</xsl:template>

   <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">
  <B1>
    <xsl:apply-templates/>
  </B1>
</xsl:template>

(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


====================================================================== 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


_________________________________________________________________
Surf the Web without missing calls! Get MSN Broadband. http://resourcecenter.msn.com/access/plans/freeactivation.asp



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




Current Thread
Keywords
xml