xsl-list
[Top] [All Lists]

Re: Removing if no data - iterating through every child (sorry the codes a bit long)

2004-09-06 06:44:32

Hi Mukul,

Thanks for this. It appears to work really well in my test cases.
It think, it's also a very handy bit of code to have around.

Cheer

Kev


From: Mukul Gandhi <mukul_gandhi(_at_)yahoo(_dot_)com>
Reply-To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: Re: [xsl] Removing if no data - iterating through every child (sorry the codes a bit long)
Date: Fri, 3 Sep 2004 10:48:33 -0700 (PDT)

Hi Bryan,
  Please try this XSL -

<?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="node()">
   <xsl:variable name="flag">
     <xsl:for-each select="@*">
       <xsl:if test="normalize-space(.) != ''">
         1
       </xsl:if>
     </xsl:for-each>
     <xsl:if test="normalize-space(.) != ''">
       1
     </xsl:if>
     <xsl:for-each select="descendant::*">
        <xsl:for-each select="@*">
          <xsl:if test="normalize-space(.) != ''">
            1
          </xsl:if>
        </xsl:for-each>
        <xsl:if test="normalize-space(.) != ''">
          1
        </xsl:if>
      </xsl:for-each>
    </xsl:variable>
    <xsl:if test="contains(normalize-space($flag),
'1')">
      <xsl:copy>
        <xsl:for-each select="@*">
          <xsl:attribute name="{name()}">
            <xsl:value-of select="." />
          </xsl:attribute>
        </xsl:for-each>
        <xsl:apply-templates />
      </xsl:copy>
    </xsl:if>
</xsl:template>

</xsl:stylesheet>

Regards,
Mukul

--- Kevinjohn Gallagher <web_design_guru(_at_)hotmail(_dot_)com>
wrote:
>
> Initially, sorry for the code seeming long. But it's
> a bit of a follow up to
> yesterdays questions (which I solved thanks to
> y'alls help).
>
>
>
> Ok, so I have an XML file, where I can have any
> number of nodes, which can
> have any number of Children (and they can have
> children and so on).
>
> Basically, I want to strip a node if it or it's
> children has no data, or has
> no attributes with data anywhere in it's
> descendants.
>
> I sort of get the logic, but fail miserably to even
> consider how to fix it
> so that the last example works (Checkbox 6) - ie.
> Stripping a tree half way
> down.
>
> ANY help would be great.
>
> Thanks ALot
>
> Bryan.
>
> P.S. the number of children in the example is not
> overkill, there will
> usually be at least 4 - sorry.
>
>
> ORIGINAL :
>
>            <topNode>
>                    <Checkbox1>
>                            <really>
>                                    <long>
>                                            <really>
>                                                    <really>
>                                                            <long 
Attribute="0"/>
>                                                    </really>
>                                            </really>
>                                    </long>
>                            </really>
>                    </Checkbox1>
>                    <Checkbox2>
>                            <really>
>                                    <long>
>                                            <really>
>                                                    <really>
>                                                            <long> data </long>
>                                                    </really>
>                                            </really>
>                                    </long>
>                            </really>
>                    </Checkbox2>
>                    <Checkbox3>
>                            <really>
>                                    <long>
>                                            <really>
>                                                    <really>
>                                                            <long />
>                                                    </really>
>                                            </really>
>                                    </long>
>                            </really>
>                    </Checkbox3>
>                    <Checkbox4>
>                            <really>
>                                    <long>
>                                            <really>
>                                                    <really>
>                                                            <long 
Attribute=""/>
>                                                    </really>
>                                            </really>
>                                    </long>
>                            </really>
>                    </Checkbox4>
>                    <Checkbox5>
>                            <really>
>                                    <long>
>                                            <really>
>                                                    <really>
>                                                            <long Attribute="0" 
attribute=""> data
> </long>
>                                                    </really>
>                                            </really>
>                                    </long>
>                            </really>
>                    </Checkbox5>
>                    <Checkbox6>
>                            <really>
>                                    <long Attribute="data">
>                                            <really>
>                                                    <really>
>                                                            <long/>
>                                                    </really>
>                                            </really>
>                                    </long>
>                            </really>
>                    </Checkbox6>
>            </topNode>
>
>
>
> Output :
>
>            <topNode>
>                    <Checkbox1>
>                            <really>
>                                    <long>
>                                            <really>
>                                                    <really>
>                                                            <long 
Attribute="0"/>
>                                                    </really>
>                                            </really>
>                                    </long>
>                            </really>
>                    </Checkbox1>
>                    <Checkbox2>
>                            <really>
>                                    <long>
>                                            <really>
>                                                    <really>
>                                                            <long> data </long>
>                                                    </really>
>                                            </really>
>                                    </long>
>                            </really>
>                    </Checkbox2>
>                    <Checkbox5>
>                            <really>
>                                    <long>
>                                            <really>
>                                                    <really>
>                                                            <long Attribute="0"> 
data </long>
>                                                    </really>
>                                            </really>
>                                    </long>
>                            </really>
>                    </Checkbox5>
>                    <Checkbox6>
>                            <really>
>                                    <long Attribute="data" />
>                            </really>
>                    </Checkbox6>
>            </topNode>





__________________________________
Do you Yahoo!?
Yahoo! Mail Address AutoComplete - You start. We finish.
http://promotions.yahoo.com/new_mail

--+------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--+--





<Prev in Thread] Current Thread [Next in Thread>