xsl-list
[Top] [All Lists]

RE: only display if subnodes occur more than once

2005-07-05 11:47:47
Hi,

At 01:01 PM 7/3/2005, it was written:
You could use keys to identify the elements to output.  Thus, your input:

<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>

Against this transform:

<?xml version="1.0" encoding="iso8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

   <xsl:output method="xml" indent="yes"/>
   <xsl:strip-space elements="*"/>

   <xsl:key match="/*/*" name="kElems" use="*"/>

   <xsl:template match="root">
       <xsl:copy>
           <xsl:for-each select="*">
               <xsl:if test="count(key('kElems', .)) != count(*)">
                   <xsl:copy-of select="."/>
               </xsl:if>
           </xsl:for-each>
       </xsl:copy>
   </xsl:template>

</xsl:stylesheet>

Yields:

<?xml version="1.0" encoding="UTF-8"?>
<root>
 <sub_b>
   <elem_1/>
   <elem_2/>
   <elem_2/>
   <elem_2/>
   <elem_3/>
 </sub_b>
</root>

You could again use a key to identify and eliminate elements such as <elem_1>. In fact *I* should do that, to match your required output ... but I need to use a composite key for that ... and it isn't coming to me right away. If somebody else has more ready insight, please post.

Could the requirements here be restated please?

If I am not mistaken, the code posted, given this document as input, effectively copies those children of the document element ('root') that do not have exactly three children ... because (again, given the input) the expression count(key('kElems', .)) will always return 3. This is because the value returned by "." is an empty string "" (notice whitespace is being stripped), which happens to be the value of all element children ("*") of all elements matching the key ("/*/*").

I find it hard to believe that's what was wanted. Guessing from the posted subject line, I think the best approach is rather something like

<xsl:template match="/*/*">
  <xsl:if test="*[count(../*[name()=name(current())]) > 1]">
    <xsl:copy-of select="."/>
  </xsl:if>
</xsl:template>

This says "copy an element child of the document element if it has any children whose parent has more than one element with the same name as the child"....

Note: not tested.

Cheers,
Wendell


======================================================================
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>
--~--