[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Count with embedded child elements

Subject: Re: Count with embedded child elements
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Mon, 9 Dec 2002 00:16:59 -0800 (PST)
xslt count children
--- I wrote:
> 
> "Bruce Dailey" <bruce@xxxxxxxxxxxx> wrote in message
> news:000001c29ecc$614257d0$0fcbfea9@xxxxxxxxxxx
> > I have an XML file like this
> > 
> > <tu>
> > <tuv>
> > <seg>Y<ut>X</ut>Y</seg>
> > </tuv>
> > </tu>
> > 
> > <tu>
> > <tuv>
> > <seg>X<ut>X</ut>X</seg>
> > </tuv>
> > </tu>
> > 
> > I am trying to count the occurrence of a parameter I am passing in
> > "SearchText", e.g. "X" per TU. So for the above I want 1 returned,
> but I
> > am getting 2 returned using the syntax below: (because 2 text nodes
> in
> > the second <tu> have X in it, I think). 
> > 
> > <xsl:value-of select="count(//tu/tuv/seg/text() [contains (.,
> > $SearchText)])"/>
> > 
> > So, in other words, I want to count the number of <tu>s that have
> the
> > search term in the <seg> element but I want to ignore the text in
> the
> > <ut> element.
> > 
> > Can someone help me out with the correct syntax.
> > 
> > I think my current syntax deals with ignoring the <ut> element but
> > doesn't deal with the multiple text nodes in the <seg> element.
> 
> 
> Use:
> 
> count(//tu/tuv/seg[contains (text() , $SearchText)])


The above is incorrect, it will return 0 when evaluated against the
following xml document:

<t>
  <tu>
    <tuv>
      <seg>Y
        <ut>X</ut>Y
      </seg>
    </tuv>
  </tu>

  <tu>
    <tuv>
      <seg>Y
        <ut>X</ut>X
        <ut>X</ut>X
      </seg>
    </tuv>
  </tu>
</t>

when in fact the correct result is 1 (the second of the //seg elements
has two text node children containing 'X').

The right XPath expression is:

count(
//seg/text()
      [contains(., $SearchText)]
           [not(preceding-sibling::text()[contains(., $SearchText)])]
      )

As you can see, there is a combination of searching and grouping in
this XPath expression.




=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL

__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.