on 09-11-2008 6:43 AM
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
Thank you all!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
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
User | Count |
---|---|
86 | |
10 | |
10 | |
9 | |
7 | |
7 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.