[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: extracting and removing an element nested at diff

Subject: Re: extracting and removing an element nested at different levels
From: "Mark" <mark@xxxxxxxxxxxx>
Date: Tue, 4 Sep 2012 21:52:01 -0700
Re:  extracting and removing an element nested at  diff
Hi Gerrit,
Thanks for your suggestion. Both worked perfectly for me and produced identical results. Is there any advantage in using one technique rather than the other?
Mark


-----Original Message----- From: Imsieke, Gerrit, le-tex
Sent: Tuesday, September 04, 2012 3:24 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: extracting and removing an element nested at different levels


Two possible solutions using identity templates. The first one uses a
dedicated mode for the nested items:

  <xsl:template match="List">
    <xsl:copy>
      <xsl:apply-templates select=".//Item"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="Item">
    <xsl:copy>
      <xsl:apply-templates mode="suppress-item"/>
    </xsl:copy>
  </xsl:template>

<xsl:template match="Item" mode="suppress-item" />

  <xsl:template match="* | @*" mode="#all">
    <xsl:copy>
      <xsl:apply-templates select="@*, node()" mode="#current"/>
    </xsl:copy>
  </xsl:template>

Second solution, single mode, for-each, identity template:

  <xsl:template match="List">
    <xsl:copy>
      <xsl:for-each select=".//Item">
        <xsl:copy>
          <xsl:apply-templates/>
        </xsl:copy>
      </xsl:for-each>
    </xsl:copy>
  </xsl:template>

<xsl:template match="Item" />

  <xsl:template match="* | @*">
    <xsl:copy>
      <xsl:apply-templates select="@*, node()" mode="#current"/>
    </xsl:copy>
  </xsl:template>

Both wont leave an empty ElementA though because of the contained text
nodes.

And both wont process nested Lists in Items.

Gerrit





On 2012-09-04 23:56, Mark wrote:
I have XML consisting of <Item> elements that have nested <Item>
elements at various depths within the tree, in its simplest form
something like:

<List>
    <Item>
        <ElementA>
            <Item>
                <ElementB/>
            </Item>
        </ElementA>
    </Item>
</List>

That I want to render as:

<List>
    <Item>
        <ElementA/>
    </Item>
    <Item>
        <ElementB/>
    </Item>
</List>

Is there a general technique I can use to remove nested <Item> elements
whatever their depth and place all <Item> elements at the same level
within the <List>?
Thanks,
Mark


-- Gerrit Imsieke Geschdftsf|hrer / Managing Director le-tex publishing services GmbH Weissenfelser Str. 84, 04229 Leipzig, Germany Phone +49 341 355356 110, Fax +49 341 355356 510 gerrit.imsieke@xxxxxxxxx, http://www.le-tex.de

Registergericht / Commercial Register: Amtsgericht Leipzig
Registernummer / Registration Number: HRB 24930

Geschdftsf|hrer: Gerrit Imsieke, Svea Jelonek,
Thomas Schmidt, Dr. Reinhard Vvckler

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.