Selcting filtered node set based on variable

Discussion in 'XML' started by Bradford, Dec 1, 2004.

  1. Bradford

    Bradford Guest

    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>
     
    Bradford, Dec 1, 2004
    #1
    1. Advertising

  2. Bradford

    Joris Gillis Guest

    > 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,
    --
    Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
    Ceterum censeo XML omnibus esse utendum
     
    Joris Gillis, Dec 1, 2004
    #2
    1. Advertising

  3. <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
     
    David Carlisle, Dec 1, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    1,620
  2. Tjerk Wolterink
    Replies:
    2
    Views:
    1,484
    Dimitre Novatchev
    Aug 24, 2006
  3. Replies:
    0
    Views:
    531
  4. =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?=

    Get record count from filtered datasourse (not set)

    =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?=, Jul 6, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    345
    Mark Rae [MVP]
    Jul 6, 2007
  5. njsimha
    Replies:
    0
    Views:
    792
    njsimha
    Sep 16, 2008
Loading...

Share This Page