Subject: Re: Re: [xsl] How to do an 'existence' test in XSL? - Simple but Generalised From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx> Date: Thu, 23 Dec 2004 14:42:13 -0500 |
I wondered if Mukul's suggestion could be generalised to function on cases where the desired tags in the input xsl may be at any level of nesting e.g. with 'alertBox' we might have :
<root> <othertag> <gui type="alertBox"></gui> </othertag> <gui type="tooltip"></gui> <gui type="help"></gui> <gui type="tooltip"></gui> <othertag> <deepertag> <gui type="alertBox"></gui> </deepertag> </othertag> <gui type="tooltip"></gui> <gui type="help"></gui> </root>
By changing the gui select in the for-each and the if test from preceeding-sibling to preceeding it (I think) simply scans backwards in the document to see if it has already encountered the type or not.
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:template match="/root"> <root> <xsl:for-each select="//gui"> <xsl:if test="not(@type = preceding::gui/@type)"> <xsl:element name="{@type}" /> </xsl:if> </xsl:for-each> </root> </xsl:template>
</xsl:stylesheet>
I have not tested it exhaustively, but it seems to work perfectly, and does not depend on knowing what types are available beforehand.
+In what ways might this +not+ be the general solution to the problem?+
It is certainly logical, and elegant.
However, the scenario I outlined was overly simplistic.
+It should not be assumed that the gui tags are are siblings. They may be scattered throughout the document at various nestings+
One of my additional problems is that I am using PHP5 and thus James Clark's expat parser, which doesn't support keys?
In this case I wonder which of the various proposed techniques will work!
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 ======================================================================
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: Re: [xsl] How to do an 'existen, Mukul Gandhi | Thread | [xsl] Namespace issue when using Xa, Andrew Male |
Re: Re: [xsl] How to do an 'existen, Dimtre Novatchev | Date | RE: [xsl] Dynamic Table, Michael Kay |
Month |