Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


RE: [xsl] match by arbitrary number of criteria

From: "Michael Kay" <mike@------------>
To:
Date: 6/4/2007 8:40:00 AM
You're misusing the contains() function here: instead of contains(subjects,
$included) you should do subjects/subject=$included which returns true if
any of the subject children is equal to $included.

Note that when comparing two node-sets $s1 = $s2 the result is true if there
is some node in $s1 that is equal to some node in $s2. So you want: 


<xsl:variable name="s" select="subjects/subject"/>
<xsl:variable name="i" select="$config//included/subject"/>
<xsl:variable name="e" select="$config//excluded/subject"/>

then 

... not($i[not($s = .)]) and not($e = $s)

Michael Kay
http://www.saxonica.com


> -----Original Message-----
> From: John Horner [mailto:Horner.John@xxxxxxxxxx] 
> Sent: 04 June 2007 07:11
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] match by arbitrary number of criteria
> 
> I've got XML roughly like this:
> 
> <items>
> 	<item>
> 		<name>foo</name>
> 		<subjects>
> 			<subject>Science</subject>
> 			<subject>Politics</subject>
> 			<subject>History</subject>
> 		</subjects>
> 	</item>
> 	<item>
> 		<name>bar</name>
> 		<subjects>
> 			<subject>Science</subject>
> 			<subject>History</subject>
> 			<subject>Art</subject>
> 		</subjects>
> 	</item>
> </items>
> 
> Where each item, as you can see, is categorised.
> 
> I'm transforming them, using parameters for inclusion and 
> exclusion, roughly like this:
> 
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
> 	<xsl:param name="included" select="'Science'"/>
> 	<xsl:param name="excluded" select="'Politics'"/>
> 	<xsl:template match="//item">
> 		<xsl:if test="contains(subjects,$included) and 
> not(contains(subjects,$excluded))">
> 			<xsl:value-of select="name"/>
> 		</xsl:if>
> 	</xsl:template>
> </xsl:stylesheet>
> 
> Which selects "bar", because its subjects match the included 
> string and don't match the excluded string.
> 
> So, all well and good if I only ever have one of each, but 
> now I need to add more. What if I might have any number of 
> inclusions and exclusions?
> Say I had a config file like this:
> 
> <config>
> 	<included>
> 		<subject>Science</subject>
> 		<subject>History</subject>
> 	</included>
> 	<excluded>
> 		<subject>Politics</subject>
> 	</excluded>
> </config>
> 
> Allowing an arbitrary number of inclusions and exclusions, 
> how would I efficiently test each item?
> 
> 
> 
> ==============================================================
> ================
> The information contained in this email and any attachment is 
> confidential and
> may contain legally privileged or copyright material.   It is 
> intended only for
> the use of the addressee(s).  If you are not the intended 
> recipient of this email, you are not permitted to 
> disseminate, distribute or copy this email or any 
> attachments.  If you have received this message in error, 
> please notify the sender immediately and delete this email 
> from your system.  The ABC does not
> represent or warrant that this transmission is secure or 
> virus free.   Before
> opening any attachment you should check for viruses.  The 
> ABC's liability is limited to resupplying any email and 
> attachments 
> ==============================================================
> ================


transparent
Print
Mail
Digg
delicious
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent