cancel
Showing results for 
Search instead for 
Did you mean: 

xslt/xpath: count preceding elements which starts-with 'S'

Former Member
0 Kudos

Hi everybody,

I got the following XML:

<?xml version="1.0" encoding="UTF-8"?>
<ROOT>
	<A1>
		<FOO>1</FOO>
	</A1>
	<A1>
		<FOO>2</FOO>
	</A1>
	<B1>
		<FOO>3</FOO>
	</B1>
	<A1>
		<FOO>4</FOO>
	</A1>
</ROOT>

I want wo count all preceding Elements of each 3

How has the experssion has to look?

count(/ROOT/A1/FOO/preceding::[starts-with(.,'A')])

DOES not work

Any suggestions?

Regards Mario

Edited by: Mario Müller on Sep 12, 2008 2:23 AM

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

Thank you all!

Former Member
0 Kudos

Hi Mario,

I give you four suggestions:

1- If you want to check if FOOs parent starts with char 'A', independently where FOO is, you can use:

1.1 - Counts all FOOs with a parent that start with char 'A'

count(//FOO[starts-with(name(parent::node()),'A')])

1.2 - Counts how many parents starting with A has one child named FOO

count(//FOO[1][starts-with(name(parent::node()),'A')])

2- If in your tree, FOO is in the third level, you can use:

2.1 - Counts all FOOs with a parent that start with char 'A'

count(/ROOT/*/FOO[starts-with(name(parent::node()),'A')])

2.2 - Counts how many parents starting with A has one child named FOO

count(/ROOT/*/FOO[1][starts-with(name(parent::node()),'A')])

Best regards,

Pedro Pereira

Note: If this helped you, give me points

Former Member
0 Kudos

Hi Pereira,

unfortunately you do not have the solution.

But a hint.

Thanks

Regards

Mario

Former Member
0 Kudos

Hi Mario,

Well, tell me if i am right.

Your intention is to produce a target with four elements. Each one will keep the current value of items found, regarding some conditions.

If is that, i don't know if xsl has any routine to generate 4 outputs when count runs. Therefore, i think it is possible to do it, using variables. The intention is to keep the variable with the current value of items found, and send the actual value to output everytime it's requested. I will try to see how this can be done.

Best regards,

Pedro Pereira

Former Member
0 Kudos

Hi Pedro,

no. I just want to know the correct XPATH expression.

With words: Count the preceding elements that start with an 'A'

Regards Mario

Former Member
0 Kudos

Hi Mario,

What are you using to test xsl expressions?

I am using XSL Tester, all sugestions that i gave you retrieve a solution. Therefore, this solution will depend from XML tree. For instance if you have something like this:

<?xml version="1.0" encoding="UTF-8"?>
<ROOT>
	<A1>
		<FOO>1</FOO>
		<FOO>5</FOO>
                        <A1000> 
                         	<FOO>46</FOO>
			        <FOO>400</FOO>
                        </A1000>
		<FOO>300</FOO>
	</A1>
	<A2>
		<FOO>2</FOO>
		<FO>6</FO>
	</A2>
	<B1>
		<FOO>3</FOO>
	</B1>
	<A1>
		<FOO>4</FOO>
		<FO>7</FO>
	</A1>
</ROOT>

Using this XSL:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:template match="/">
  		<Result>
			---- Expression ----
		</Result>
	</xsl:template>
</xsl:stylesheet>

Results using the expressions that i gave you, will be:

1.1

<xsl:value-of select="count(//FOO[1][starts-with(name(parent::node()),'A')])"/>

<?xml version="1.0" encoding="UTF-16"?>
     <Result>7</Result>

1.2

<xsl:value-of select="count(//FOO[1][starts-with(name(parent::node()),'A')])"/>

<?xml version="1.0" encoding="UTF-16"?>
    <Result>4</Result>

2.1

<xsl:value-of select="count(/ROOT/*/FOO[starts-with(name(parent::node()),'A')])"/>

<?xml version="1.0" encoding="UTF-16"?>
  <Result>5</Result>

2.2

<xsl:value-of select="count(/ROOT/*/FOO[1][starts-with(name(parent::node()),'A')])"/>

<?xml version="1.0" encoding="UTF-16"?>
    <Result>3</Result>

I think the last one, fits your solution. Give me more hints, maybe i can help you.

Best regards,

Pedro Pereira

Former Member
0 Kudos

Hi Pedro,

you gave the right hint.

The correct expression is e.g fot the third FOO

count(/ROOT/A1[3]/FOO/preceding::*[starts-with(name(.),'A')])

Regards Mario

Edited by: Mario Müller on Jan 6, 2009 6:04 AM