Selcting filtered node set based on variable

Bradford

Usenet Archive
Joined
Dec 1, 2004
Messages
2
Reaction score
0
Question for the masses...

Lets say I have variable with the following contents
"aaaa bbbb ccccc dddd". The format is not specific and the space delimiter could be changed to any other.

How would i count and/or select the nodes where a child nodes value is not contained in this list. Or the reverse scenario would be sufficent whereas I count the nodes whose child nodes value is contained.

A contrived example of my situation follows...thanks for the help.

<root>
<limbs>
<branch>
<name>a</name>
<flower>big</flower>
</branch>
<branch>
<name>b</name>
<flower>big</flower>
</branch>
<branch>
<name>c</name>
<flower>large</flower>
</branch>
<branch>
<name>d</name>
<flower>giant</flower>
</branch>
</limbs>
<root>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:eek:utput method="html" indent="yes" omit-xml-declaration="yes"/> <xsl:variable name="Exclude"> <xsl:value-of select="big large"/> </xsl:variable> <xsl:template match="/"> <xsl:variable name="DomainCount"> <xsl:value-of select="count(//branch[contains($Exclude, flower)])"/> </xsl:variable> </xsl:template> </xsl:stylesheet>
 

Joris Gillis

Usenet Archive
Joined
Sep 28, 2004
Messages
355
Reaction score
0
Question for the masses...
Panem et circenses
Lets say I have variable with the following contents
"aaaa bbbb ccccc dddd". The format is not specific and the space delimiter could be changed to any other.

How would i count and/or select the nodes where a child nodes value is not contained in this list. Or the reverse scenario would be sufficent whereas I count the nodes whose child nodes value is contained.

A contrived example of my situation follows...thanks for the help.

<root>
<limbs>
<branch>
<name>a</name>
<flower>big</flower>
</branch>
<branch>
<name>b</name>
<flower>big</flower>
</branch>
<branch>
<name>c</name>
<flower>large</flower>
</branch>
<branch>
<name>d</name>
<flower>giant</flower>
</branch>
</limbs>
</root>

Hi,

In this situation, the following stylesheet might work:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:eek:utput method="html" indent="yes" omit-xml-declaration="yes"/> <xsl:variable name="Exclude"> big large </xsl:variable>

<xsl:template match="/">
<xsl:variable name="DomainCount">
<xsl:value-of select="count(//branch[not(contains($Exclude,flower))])"/> </xsl:variable> </xsl:template>

</xsl:stylesheet>

regards,
 

David Carlisle

Usenet Archive
Joined
Jan 5, 2000
Messages
609
Reaction score
0
<xsl:variable name="Exclude">
<xsl:value-of select="big large"/>
</xsl:variable>

note it's simpler and a lot more efficient to do

<xsl:variable name="Exclude" select="' big large '"/>

(and you'd omitted the quotes around the string 'big large'.) I added a space at either end as well (used below)

<xsl:variable name="DomainCount">
<xsl:value-of select="count(//branch[contains($Exclude, flower)])"/> </xsl:variable>

again that would be better as

<xsl:variable name="DomainCount" select="count(//branch[contains($Exclude, flower)])"/>

which works but to avoid the possibility of one name being a substring of the other, you can append a space to either side:

<xsl:variable name="DomainCount" select="count(//branch[contains($Exclude, concat(' ',flower, ' '))])"/>

so:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:eek:utput method="html" indent="yes" omit-xml-declaration="yes"/> <xsl:variable name="Exclude" select="' big large '"/>

<xsl:template match="/">

<xsl:variable name="DomainCount" select="count(//branch[contains($Exclude, concat(' ',flower, ' '))])"/> [<xsl:value-of select="$DomainCount"/>] </xsl:template> </xsl:stylesheet>

<root>
<limbs>
<branch>
<name>a</name>
<flower>big</flower>
</branch>
<branch>
<name>b</name>
<flower>big</flower>
</branch>
<branch>
<name>c</name>
<flower>large</flower>
</branch>
<branch>
<name>d</name>
<flower>giant</flower>
</branch>
</limbs>
</root>

$ saxon flower.xml flower.xsl

[3]

3 nodes have flower= big or large
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
548,208
Messages
3,104,799
Members
249,594
Latest member
henrywalsh

Latest Threads

Top