You are not logged in.

  • Login

1

Monday, March 22nd 2010, 4:38pm

XSL - suchen, aufsummieren, ausgeben von gleichen Inhalten/Werten?

Hallo liebes Forum,

erst einmal herzlich Willkommen von meiner Seite aus.

Ich habe folgendes Problem:

erstmal einen Auszug aus der XML Datei:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?xml version="1.0" encoding="UTF-8"?>
<topo>
    <node>
        <id>2148063978</id>
        <name>Hans</name>
        <systemObjectId>10</systemObjectId>
    </node>    
    <node>
        <id>2148063988</id>
        <name>Paul</name>
        <systemObjectId>10</systemObjectId>
    </node>
    <node>
        <id>2148063968</id>
        <name>Peter</name>
        <systemObjectId>11</systemObjectId>
    </node>
    <node>
        <id>2148063958</id>
        <name>Franz</name>
        <systemObjectId>11</systemObjectId>
    </node>
    <node>
        <id>2148063948</id>
        <name>Willi</name>
        <systemObjectId>11</systemObjectId>
    </node>
    </node>
    <node>
        <id>2148063928</id>
        <name>Gustav</name>
        <systemObjectId>7</systemObjectId>
    </node>
</topo>


habe also eine n Anzahl von <node> Knoten.

Mit Hilfe dieses XSL Stylesheets:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">


<xsl:template match="/">
  <html>
  <body>
  <h2>Ausgabe:</h2>
    <table border="1">
      <tr bgcolor="#647563">
        <th>Zahl</th>
        <th>ID</th>
        <th>Name</th>
        <th>SysObjectID</th>
      </tr>
      <xsl:for-each select="topo/node">
        <xsl:sort select="systemObjectId"/>
          <tr>
            <td><xsl:value-of select="position()" disable-output-escaping="yes"/></td>
            <td><xsl:value-of select="id"/></td>
            <td><xsl:value-of select="name"/></td>
            <td><xsl:value-of select="systemObjectId"/></td>
          </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>


komme ich zu folgender Ausgabe im Browser:


Soweit so gut, alles schon einmal wie ich es gerne hätte. Nun soll die Ausgabe aber nochmal modifiziert wird, so dass das Endergebnis dann wie folgt ausschaut:



Es sollen alle gleichen sysObjectID's (die ja schon sortiert sind) aufsummiert werden und wie auf dem Bild gezeigt ausgegeben werden.

Ich habe mich natürlich schonmal schlau gemacht und bin auf folgenden Thread gestoßen: Wordcount

Mit Hilfe des folgenden XSL Codes:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output indent="yes" method="html"/>
<xsl:key name="only" match="//systemObjectId" use="."/>

	<xsl:template match="/">
		<html>
			<table border="1">
				<tr bgcolor="#647563">
					<th>SysObjectID</th>
					<th>Anzahl</th>
				</tr>
				<xsl:apply-templates select="topo"/>
			</table>
		</html>
	</xsl:template>
    
	<xsl:template match="topo">
		<xsl:for-each select="child::*/systemObjectId[generate-id()= generate-id(key('only', .)[1])]">
			<tr>
				<td>
					<xsl:value-of select="."/>
				</td>
				<td>
					<xsl:value-of select="count(key('only',.))"/>
				</td>
			</tr>
		</xsl:for-each>
	</xsl:template>
</xsl:stylesheet>


komme ich auch zur richtigen Ausgabe wie der folgende Screenshot zeigt:



Doch egal wie ich versuche die beiden XSL Stylesheets miteinander zu kombinieren, ich bekomme es einfach nicht hin.
Hat jmd. vllt noch ne Idee oder zumindest ein Vorschlag wie ich es weiter probieren könnte?

Schon mal vielen Dank für eure Mühen!

2

Monday, March 22nd 2010, 10:09pm

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="html" indent="yes"/>
    <xsl:key name="only" match="systemObjectId" use="."/>
    <xsl:template match="/">
        <html>
            <xsl:apply-templates select="topo"/>
        </html>
    </xsl:template>
    <xsl:template match="topo">
        <table border="3px solid">
            <xsl:for-each
                select="//node[generate-id(./systemObjectId)=generate-id(key('only', ./systemObjectId)[1])]">
                <xsl:variable name="ak" select="./systemObjectId"/>
                <xsl:for-each select="//node[systemObjectId=$ak]">
                    <tr>
                        <td>
                            <xsl:value-of select="id"/>
                        </td>
                        <td>
                            <xsl:value-of select="name"/>
                        </td>
                        <td>
                            <xsl:value-of select="$ak"/>
                        </td>
                    </tr>
                </xsl:for-each>
                <tr>
                    <td>Total</td>
                    <td> =</td>
                    <td>
                        <xsl:value-of select="count(key('only',./systemObjectId))"/>
                    </td>
                </tr>
            </xsl:for-each>
        </table>
    </xsl:template>
</xsl:stylesheet>

3

Tuesday, March 23rd 2010, 9:21am

Ich habe zu danken!

Jetzt wenn ich mir deinen Code so im Nachhinein anschaue, kann ich ihn nachvollziehen.

Es schwere ist immer nur selbst auf so etwas zu kommen! Respekt an dich und nochmal vielen Dank!

Similar threads

Social bookmarks