Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Selcting filtered node set based on variable

Reply
Thread Tools

Selcting filtered node set based on variable

 
 
Bradford
Guest
Posts: n/a
 
      12-01-2004
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"
xmlnssl="http://www.w3.org/1999/XSL/Transform">
<xslutput 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>

 
Reply With Quote
 
 
 
 
Joris Gillis
Guest
Posts: n/a
 
      12-01-2004
> 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"
xmlnssl="http://www.w3.org/1999/XSL/Transform">
<xslutput 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,
--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
Ceterum censeo XML omnibus esse utendum
 
Reply With Quote
 
 
 
 
David Carlisle
Guest
Posts: n/a
 
      12-01-2004
<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"
xmlnssl="http://www.w3.org/1999/XSL/Transform">
<xslutput 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
<need help>How to print the preceding node value based on descendant node in XSLT? njsimha XML 0 09-16-2008 12:33 PM
removing a node from a node-set jonny@drugphish.ch XML 0 12-02-2006 11:46 AM
xsl variable $node/text() but $node can non-node-set help! Tjerk Wolterink XML 2 08-24-2006 03:28 AM
How to set the node indent property between the parent node and the leaf node viveknatani@gmail.com ASP .Net 0 02-13-2006 07:11 PM
how to create a node set of elements through C++ code while executing a style sheet and process the created node set. pvssvikas@gmail.com XML 0 01-25-2006 12:48 PM



Advertisments