RE: [xsl] Wrapping a Condition around a literal result element

Subject: RE: [xsl] Wrapping a Condition around a literal result element
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Tue, 16 May 2006 11:14:11 +0100
I think you've made it more difficult than it is.

>name elements 
> that exist in every section (but do not contain the string 
> 'new entry') 

that's

distinct-values(/sections/section/name)
                 [not(contains(., 'new entry')]
                 [every $s in /sections/section satisfies $s/name = .])

So you assign this expression to a variable $ubiquitous-names and then do

<xsl:if test="exists($ubiquitous-names)">
  <section name="allsections">
    <xsl:for-each select="$ubiquitous-names">


Michael Kay
http://www.saxonica.com/

> -----Original Message-----
> From: Kevin Bird [mailto:kevin.bird@xxxxxxxxxxxxxxxxxxxxxxx] 
> Sent: 16 May 2006 10:55
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Wrapping a Condition around a literal result element
> 
> 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="{@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>

Current Thread