Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

<xsl:param einfacher zähler

Empfohlene Antworten

Veröffentlicht

hi und hallo,

ich möchte gerne in einer xsl datei einen zähler in einer for-each schleife hochzählen und dann ausgeben. leider funktioniert das überhaupt nicht so wie ich mir das vorstelle und für dieses recht einfache finde ich im netz nicht wirklich die lösung.

das problem ist , dass die zählervariable (bzw. param) $count nicht "sichtbar" ist.

hier mal der code


<?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="/">


<xsl:for-each select="catalog/cd">

	<xsl:call-template name="countVal">

		<!--HIER DER AUFRUF UM $COUNT HOCHZUZÄHLEN -->

		<xsl:with-param name="count" select="$count + 1"/>

	</xsl:call-template>

</xsl:for-each>


</xsl:template>


<xsl:template name="countVal">

	<!--SETZEN DER VARIABLEN COUNT -->

	<xsl:param name="count" select="1"/>

	<xsl:value-of select ="$count"/>

</xsl:template>


</xsl:stylesheet> 

kann mir da jemand helfen???

hab ein bisschen weitergeforscht und was gefunden. leider klappt es wieder nicht befriedigend


<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="html" encoding="ISO-8859-1"/>



<xsl:template match="/">

<xsl:call-template name="loop">

</xsl:call-template>

</xsl:template>


<xsl:template name="loop">

	<xsl:param name="index">0</xsl:param>

	<xsl:param name="max">0</xsl:param>

	Index: <xsl:value-of select="$index"/><br />

	<xsl:for-each select="catalog/cd">

		<xsl:call-template name="loop">

		<xsl:with-param name="index"><xsl:value-of select="$index + 1"/></xsl:with-param>

		<xsl:with-param name="max"><xsl:value-of select="$max"/></xsl:with-param>

		</xsl:call-template>

	</xsl:for-each>

</xsl:template>


</xsl:stylesheet>

ich habe in meiner xml-datei 3 <cd>

die ausgabe ist folgende:

Index: 0

Index: 1

Index: 1

Index: 1

und das kapier ich nicht, da doch in der for-each schleife immer $index + 1 übergeben wird...

wenn ich die for-each schleife weglasse, ist die ausgabe endlos (klar) und richtig:

Index: 0

Index: 1

Index: 2

Index: 3

Index: 4

Index: 5

Index: 6

Index: 7

Index: 8

Index: 9

.

.

.

was läuft da schief???

danke

Hi

Hallo Fabian,

es gibt das Element <xsl:call-template> , damit lässt sich sowas realisieren:

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="html" encoding="ISO-8859-1"/>

<xsl:template match="/">

<xsl:call-template name="loop">

<xsl:with-param name="index">0</xsl:with-param>

<xsl:with-param name="max">5</xsl:with-param>

</xsl:call-template>

</xsl:template>

<xsl:template name="loop">

<xsl:param name="index">0</xsl:param>

<xsl:param name="max">0</xsl:param>

<xsl:if test="$index < $max">

Index: <xsl:value-of select="$index"/><br />

<xsl:call-template name="loop">

<xsl:with-param name="index"><xsl:value-of select="$index + 1"/></xsl:with-param>

<xsl:with-param name="max"><xsl:value-of select="$max"/></xsl:with-param>

</xsl:call-template>

</xsl:if>

</xsl:template>

</xsl:stylesheet>

Unabhängig von der verwendeten XML-Datei sollte in diesem Fall der Output folgendermassen aussehen:

Index: 0

Index: 1

Index: 2

Index: 3

Index: 4

gruss

Stefan

Quelle:

http://www.wer-weiss-was.de/theme193/article999276.html

Gruß,

Markus

jo kills, genau daher hab ichs ja auch. nur ist bei mir der unterschied dass ich diese if-test-anweisung durch ein for-each ausgetauscht habe und erstmal alles überflüssige aussenrum weggeworfen habe.

es klappt ohne for-each perfekt, das heisst $index wird übergeben und hochgezählt.

wenn mein for-each drin ist wird die schleife 3mal ausgeführt was auch richtig ist weil ich drei einträge <cd> in meiner xml hab, aber $index wird nur beim ersten mal um 1 erhöht.

ausgabe:

0

1

1

1

da liegt das problem....

Hi,

ich würd ma so versuchen:


<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="html" encoding="ISO-8859-1"/>



<xsl:template match="/">

<xsl:call-template name="loop"/>

</xsl:template>


<xsl:template name="loop">

	<xsl:param name="index">0</xsl:param>

	Index: <xsl:value-of select="$index"/><br />

	<xsl:for-each select="catalog/cd">

		<xsl:call-template name="loop">

		<xsl:with-param name="index"><xsl:value-of select="$index + 1"/></xsl:with-param>

		</xsl:call-template>

	</xsl:for-each>

</xsl:template>


</xsl:stylesheet>

Gruß,

Markus

*heul*...leider nicht.

sleber output:

Index: 0

Index: 1

Index: 1

Index: 1

die xml datei ist die typische im web auffindbare tutorial xml:

<?xml version="1.0" encoding="ISO-8859-1"?>

<!-- Edited with XML Spy v4.2 -->

<?xml-stylesheet type="text/xsl" href="cdcatalog_old.xsl"?>

<catalog>

<cd>

<title>Empire Burlesque</title>

<artist>Bob Dylan</artist>

<country>USA</country>

<company>Columbia</company>

<price>10.90</price>

<year>1985</year>

</cd>

<cd>

<title>Hide your heart</title>

<artist>Bonnie Tyler</artist>

<country>UK</country>

<company>CBS Records</company>

<price>9.90</price>

<year>1988</year>

</cd>

<cd>

<title>Greatest Hits</title>

<artist>Dolly Parton</artist>

<country>USA</country>

<company>RCA</company>

<price>9.90</price>

<year>1982</year>

</cd>

.

.

.

zeig ma das dazugehörige xml

Edit:


<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="html" encoding="ISO-8859-1"/>



<xsl:template match="/">

<xsl:call-template name="loop"/>

</xsl:template>


<xsl:template name="loop">

	<xsl:param name="index">0</xsl:param>

	Index: <xsl:value-of select="$index"/><br />

	<xsl:for-each select="/catalog">

		<xsl:call-template name="loop">

		<xsl:with-param name="index"><xsl:value-of select="$index + 1"/></xsl:with-param>

		</xsl:call-template>

	</xsl:for-each>

</xsl:template>


</xsl:stylesheet>

Oder

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="html" encoding="ISO-8859-1"/>



<xsl:template match="/">

<xsl:call-template name="loop"/>

</xsl:template>


<xsl:template name="loop">

	<xsl:param name="index">0</xsl:param>

	Index: <xsl:value-of select="$index"/><br />

	<xsl:for-each select="catalog">

		<xsl:call-template name="loop">

		<xsl:with-param name="index"><xsl:value-of select="$index + 1"/></xsl:with-param>

		</xsl:call-template>

	</xsl:for-each>

</xsl:template>


</xsl:stylesheet>

Oder

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="html" encoding="ISO-8859-1"/>



<xsl:template match="/">

<xsl:call-template name="loop"/>

</xsl:template>


<xsl:template name="loop">

	<xsl:param name="index">0</xsl:param>

	Index: <xsl:value-of select="$index"/><br />

	<xsl:for-each select="/catalog/cd">

		<xsl:call-template name="loop">

		<xsl:with-param name="index"><xsl:value-of select="$index + 1"/></xsl:with-param>

		</xsl:call-template>

	</xsl:for-each>

</xsl:template>


</xsl:stylesheet>

sry. leider nix. immer die selbe ausgabe bzw. fehlermeldung wegen dem / vor catalog. wenn ich den wegmache: das selbe bild wie vorher...

sry weiss nicht woran`s noch hängen könnte.

Hab auch noch nicht so viel mit dem Zeugs gemacht :(

is doch kein problem ;)

bis zu meinem nächsten problem das noch nie jemand hatte hehe.

vielen dank für die mühe!

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.