Domanda

I have a question regarding the numbering of nodes. I would like to re-use the numbers assigned to the nodes later on. I will show what I mean by an example.

The xml code:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="cookbook.xsl"?>
<cookbook>
<recipe>
<name>Waffles</name>
<category>Breakfast</category>
<description>Make the waffles</description>
</recipe>
<recipe>
<name>Spaghetti</name>
<category>Diner</category>
<description>Make the spaghetti</description>
</recipe>
<recipe>
<name>Pancakes</name>
<category>Breakfast</category>
<description>Make the pancakes</description>
</recipe>
</cookbook>

Output:

1 Waffles
Make the waffles
2 Spaghetti
Make the spaghetti
3 Pancakes
Make the pancakes

With xsl:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match="/">

<xsl:for-each select="cookbook/recipe">
<xsl:variable name="number">
<xsl:number level="any" count="recipe" value="position()"/>
</xsl:variable>
<xsl:value-of select="$number"/>
<xsl:value-of select="name"/>
    <xsl:for-each select="description">
    <xsl:value-of select="." />
    </xsl:for-each>
</xsl:for-each>

Later on I want to show only the recipenames and the number corresponding to it.

Output:

Breakfast
Waffles (1)
Pancakes (3)

Diner
Spaghetti (2)

xsl:

<xsl:for-each select="//category[not(preceding::category=.)]">
<xsl:sort select="." />
<xsl:value-of select="." />
<xsl:variable name="categoryname">
    <xsl:value-of select="." />
</xsl:variable>
<xsl:for-each select="//category[$categoryname=.]/..">
    <a  class="index" href="#{generate-id(name)}">
        <xsl:value-of select="name" />
        <xsl:text> (</xsl:text>
        <!--I would like to show the number right here-->
        <xsl:text>)</xsl:text>
    </a>
</xsl:for-each>

In this latter example the number is no longer a counter but a fixed number that belongs to each recipename. As I am new to working with xslt I am not sure how to search for an answer. Maybe using xsl:number is not the right thing to do here. I tried a lot already but I still can't get a grasp of this problem.

I would be very grateful for a hint in the right direction. Thanks in advance.

È stato utile?

Soluzione

Try:

<xsl:value-of select="count(preceding-sibling::recipe)+1" />

BTW, you should read up on keys in general and grouping using the Muenchian method specifically.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top