xsl-list
[Top] [All Lists]

RE: only display if subnodes occur more than once

2005-07-05 13:33:49
Aron,

Thanks.

Of course the solution I posted doesn't work either, due to limitations of XPath 1.0 and the fact that current() refers not to any node being predicated, but to the current node (the node matched).

Quite an interesting problem, actually. It should be pretty straightforward in XPath 2.0, but the only XSLT 1.0 solutions I've thought of are very ugly and un-XSLTish. That may be unavoidable, again due to limitations in XPath 1.0 -- but before posting one of those I thought I'd give time to see if others can work it out more elegantly....

Anyone?

Oh heck, here's an ugly solution:

<xsl:template match="root/*">
  <xsl:variable name="include">
    <xsl:call-template name="include"/>
  </xsl:variable>
  <xsl:if test="string($include)">
    <xsl:copy-of select="."/>
  </xsl:if>
</xsl:template>

<xsl:template name="include">
  <xsl:for-each select="*">
    <xsl:if test="following-sibling::*[name()=name(current())]">X</xsl:if>
  </xsl:for-each>
</xsl:template>

It uses a template to fake a function, which returns a null value (which fails the string() test) if there are no children that have following siblings of the same name.

Cheers,
Wendell

At 03:35 PM 7/5/2005, you wrote:
  The original problem asked (I think), given:

<root>
        <sub_a>
                <elem_1/>
                <elem_2/>
                <elem_3/>
        </sub_a>
        <sub_b>
                <elem_1/>
                <elem_2/>
                <elem_2/>
                <elem_2/>
                <elem_3/>
        </sub_b>
        <sub_c>
                <elem_1/>
                <elem_2/>
                <elem_3/>
        </sub_c>
</root>

output:

<root>
        <sub_b>
                <elem_2/>
                <elem_2/>
                <elem_2/>
        </sub_b>
</root>

Namely, output those sub_* which have more than 1 elem_* children named the same; also output the elem_* children.

--A




======================================================================
Wendell Piez                            
mailto:wapiez(_at_)mulberrytech(_dot_)com
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
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--