Re: [xsl] Delete element except one child

Subject: Re: [xsl] Delete element except one child
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Fri, 12 May 2006 10:57:18 +0200
At 2006-05-12 00:11 -0700, Hind Al-Hakami wrote:
How can I do the following:
If "UML:Package" has "xmi.idref" attribute then delete
its grand-Pa element bolck. Otherwise
delete "UML:Package" element except the children of
its child element "UML:Namespase.ownedElement"
XSLT Code:
        <xsl:template match="*[name(descendant::*[2]) =
&gt; 0"/>

A few things about the above:

(1) the predicate "[2]" will check the second of all of the descendants *in document order*, not in depth, so the test of "grandchildren" would be done directly with XPath steps
(2) it is unsafe and bad practice to check the names of namespace-qualified information items because that incorporates the use of an arbitrary prefix which may not match the input
(3) you have not written a test that would keep the element even if there is no attribute on the grandchild ... your template rule removes the element regardless of whether there is an attribute or not
(4) an attribute's presence is not best checked by checking its string length; it is sufficient merely to address the attribute node and the predicate will be false if the attribute is absent

I would rewrite your above statement, though I do not have time to work with your test data, as:

<xsl:template match="*[*/UML:Package/@xmi.idref]"/>

You should also review the appropriateness of using "//" where you have, as that is a big hit against performance and a common inappropriate use of XPath by new users of XSLT.

I hope this helps.

. . . . . . Ken

Registration open for XSLT/XSL-FO training: Wash.,DC 2006-06-12/16
Also for XSLT/XSL-FO training:    Minneapolis, MN 2006-07-31/08-04
Also for XML/XSLT/XSL-FO training:Birmingham,England 2006-05-22/25
World-wide on-site corporate, govt. & user group XML/XSL training.
G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.
Box 266, Kars, Ontario CANADA K0A-2E0    +1(613)489-0999 (F:-0995)
Male Cancer Awareness Aug'05
Legal business disclaimers:

Current Thread