xsl-list
[Top] [All Lists]

[xsl] Wrapping a Condition around a literal result element

2006-05-16 02:55:54
Hi

Given the following input, I need to remove name elements that exist in
every section (but do not contain the string 'new entry') and create a
new section containing the aforementioned.

The stylesheet below gives me the desired result for this given input,
but the <section name="allsections"> element is hard coded into the
stylesheet. I will have instances where no name elements exist in every
section and therefore I do not want to output a self closing section
element. I am struggling to write a condition around <section
name="allsections">. Any help would be appreciated. 

Kind regards.

--
Kevin


----------------
Input:
----------------
<?xml version="1.0"?>
<sections>
        <section name="North">
                <name>Kevin</name>
                <name>Bill</name>
                <name>Fred</name>
                <name>Ollie</name>
                <name>Frank - new entry</name>
                <name>Bob - new entry</name>
        </section>
        <section name="South">
                <name>Fred</name>
                <name>Kevin</name>
                <name>Bill</name>
                <name>Frank - new entry</name>
                <name>Bob - new entry</name>
        </section>
        <section name="West">
                <name>Kevin</name>
                <name>Frank - new entry</name>
                <name>Bill</name>
                <name>Ollie</name>
                <name>Bob - new entry</name>
                <name>Fred</name>
        </section>
        <section name="East">
                <name>Harry</name>
                <name>Bob - new entry</name>
                <name>Bill</name>
                <name>Frank - new entry</name>
                <name>Kevin</name>
                <name>Ollie</name>
        </section>
</sections>

-----------------
Required Output
-----------------
<?xml version="1.0" encoding="UTF-8"?>
<sections>
   <section name="North">
      <name>Fred</name>
      <name>Ollie</name>
      <name>Frank - new entry</name>
      <name>Bob - new entry</name>
   </section>
   <section name="South">
      <name>Fred</name>
      <name>Frank - new entry</name>
      <name>Bob - new entry</name>
   </section>
   <section name="West">
      <name>Frank - new entry</name>
      <name>Ollie</name>
      <name>Bob - new entry</name>
      <name>Fred</name>
   </section>
   <section name="East">
      <name>Harry</name>
      <name>Bob - new entry</name>
      <name>Frank - new entry</name>
      <name>Ollie</name>
   </section>
   <section name="allsections">
      <name>Kevin</name>
      <name>Bill</name>
   </section>
</sections>


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

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

        <xsl:template match="sections">
                <sections>
                        <xsl:variable name="section-count"
select="count(section)"/>

                        <xsl:for-each select="section">
                                <section name="{(_at_)name}">
                                        <xsl:variable
name="other-sections"
select="preceding-sibling::section|following-sibling::section"/>
                                        <xsl:for-each select="name">
                                                <xsl:choose>
                                                        <xsl:when
test="contains(.,'new entry')">
                                                                <name>
        
<xsl:value-of select="."/>
                                                                </name>
                                                        </xsl:when>
                                                        <xsl:when
test="count($other-sections/name[lower-case(.) = lower-case(current())])
= ($section-count -1)"/>
                                                        <xsl:otherwise>
                                                                <name>
        
<xsl:value-of select="."/>
                                                                </name>
                                                        </xsl:otherwise>
                                                </xsl:choose>
                                        </xsl:for-each>
                                </section>
                        </xsl:for-each>
                        
                        <!-- This is hard coded, need to add a condition
-->
                        <section name="allsections">
                                <xsl:for-each select="section[1]/name">
                                        <xsl:variable
name="other-sections" select="../following-sibling::section"/>
                                        <xsl:choose>
                                                <xsl:when
test="contains(.,'new entry')"/>
                                                <xsl:when
test="count($other-sections/name[lower-case(.) = lower-case(current())])
= ($section-count -1)">
                                                        <xsl:copy-of
select="."/>
                                                </xsl:when>
                                                <xsl:otherwise/>
                                        </xsl:choose>
                                </xsl:for-each>
                        </section>

                </sections>
        </xsl:template>
</xsl:stylesheet>

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