tags:

views:

40

answers:

1

Is there a way to use data from the current context to filter nodes somewhere else in the statsource.

For instance, if I have this XML:

<root>
    <group1>
        <inst>
            <type>Foo</type>
            <value>First Foo</value>
        </inst>
        <inst>
            <type>Bar</type>
            <value>The Bar</value>
        </inst>
        <inst>
            <type>Foo</type>
            <value>Second Foo</value>
        </inst>
    </group1>
    <group2>
        <Filter>
            <FilterType>Foo</FilterType>
        </Filter>
        <Filter>
            <FilterType>Bar</FilterType>
        </Filter>
    </group2>
</root>

Assuming my context is one of the Filter tags, I want to return get the number of instances of the specified type in group1. I would like to write XPATH that looks something like this:

count(/root/group1/inst[type = **FilterType**])

Is there anything I can use to get the FilterType in the original context?

+1  A: 

This can be done easily in XPath 2.0:

for $type in /*/*/Filter[1]/FilterType
  return 
     count(/*/group1/*[type eq $type])

When this Xpath expression is evaluated against the provided XML document, the correct result is returned:

2

In XPath 1.0, if the number of group1/inst elements is known in advance, and $vType stands for the FilterType in question, then one could construct the following XPath 1.0 expression:

  ($vType = /*/group1/inst[1]/type)
 +
  ($vType = /*/group1/inst[2]/type)
 +
  ($vType = /*/group1/inst[3]/type)

which again produces:

2.

Finally, if the XPath 1.0 expression is needed in XSLT and the "Filter" is the current node,

then the following XPath expression calculates the exact number of matches:

  count(/*/group1/inst[type = curent()/FilterType])
Dimitre Novatchev